aboutsummaryrefslogtreecommitdiff
path: root/tools/make_spec.lua
blob: c247142490ad8d3f59681516c7ca051d8524286d (plain)
  1. local lcmark = require('lcmark')
  2. local cmark = require('cmark')
  3. local format = arg[1] or 'html'
  4. local trim = function(s)
  5. return s:gsub("^%s+",""):gsub("%s+$","")
  6. end
  7. local warn = function(s)
  8. io.stderr:write('WARNING: ' .. s .. '\n')
  9. end
  10. local to_identifier = function(s)
  11. return trim(s):lower():gsub('[^%w]+', ' '):gsub('[%s]+', '-')
  12. end
  13. local render_number = function(tbl)
  14. local buf = {}
  15. for i,x in ipairs(tbl) do
  16. buf[i] = tostring(x)
  17. end
  18. return table.concat(buf, '.')
  19. end
  20. local extract_label = function(cur)
  21. local label = ""
  22. for subcur, subentering, subnode_type in cmark.walk(cur) do
  23. if subentering and subnode_type == cmark.NODE_TEXT then
  24. label = label .. cmark.node_get_literal(subcur)
  25. elseif subentering and subnode_type == cmark.NODE_SOFTBREAK then
  26. label = label .. " "
  27. end
  28. end
  29. return label
  30. end
  31. local extract_references = function(doc)
  32. local cur, entering, node_type
  33. local refs = {}
  34. for cur, entering, node_type in cmark.walk(doc) do
  35. if not entering and
  36. ((node_type == cmark.NODE_LINK and cmark.node_get_url(cur) == '@') or
  37. node_type == cmark.NODE_HEADING) then
  38. local label = extract_label(cur)
  39. local ident = to_identifier(label)
  40. if refs[label] then
  41. warn("duplicate reference " .. label)
  42. end
  43. refs[label] = ident
  44. if not refs[label .. 's'] then
  45. -- plural too
  46. refs[label .. 's'] = ident
  47. end
  48. end
  49. end
  50. -- check for duplicate IDs
  51. local idents = {}
  52. for _,id in ipairs(refs) do
  53. if idents[id] then
  54. warn("duplicate identifier " .. id)
  55. end
  56. idents[#idents + 1] = id
  57. end
  58. return refs
  59. end
  60. local make_toc = function(toc)
  61. -- we create a commonmark string, then parse it
  62. local toclines = {}
  63. for _,entry in ipairs(toc) do
  64. if entry.level <= 2 then
  65. local indent = string.rep(' ', entry.level - 1)
  66. toclines[#toclines + 1] = indent .. '* [' ..
  67. (entry.number == '' and ''
  68. or '<span class="number">' .. entry.number .. '</span>') ..
  69. entry.label .. '](#' .. entry.ident .. ')'
  70. end
  71. end
  72. -- now parse our cm list and return the resulting list node:
  73. local doc = cmark.parse_string(table.concat(toclines, '\n'), cmark.OPT_SMART)
  74. return cmark.node_first_child(doc)
  75. end
  76. local make_html_element = function(block, tagname, attrs)
  77. local div = cmark.node_new(block and cmark.NODE_CUSTOM_BLOCK or
  78. cmark.NODE_CUSTOM_INLINE)
  79. local attribs = {}
  80. for _,attr in ipairs(attrs) do
  81. attribs[#attribs + 1] = ' ' .. attr[1] .. '="' .. attr[2] .. '"'
  82. end
  83. local opentag = '<' .. tagname .. table.concat(attribs, '') .. '>'
  84. local closetag = '</' .. tagname .. '>'
  85. cmark.node_set_on_enter(div, opentag)
  86. cmark.node_set_on_exit(div, closetag)
  87. return div
  88. end
  89. local make_html_block = function(tagname, attrs)
  90. return make_html_element(true, tagname, attrs)
  91. end
  92. local make_html_inline = function(tagname, attrs)
  93. return make_html_element(false, tagname, attrs)
  94. end
  95. local make_latex = function(spec)
  96. local latex = cmark.node_new(spec.block and cmark.NODE_CUSTOM_BLOCK or
  97. cmark.NODE_CUSTOM_INLINE)
  98. cmark.node_set_on_enter(latex, spec.start)
  99. cmark.node_set_on_exit(latex, spec.stop)
  100. return latex
  101. end
  102. local make_text = function(s)
  103. local text = cmark.node_new(cmark.NODE_TEXT)
  104. cmark.node_set_literal(text, s)
  105. return text
  106. end
  107. local create_anchors = function(doc, meta, to)
  108. local cur, entering, node_type
  109. local toc = {}
  110. local number = {0}
  111. local example = 0
  112. for cur, entering, node_type in cmark.walk(doc) do
  113. if not entering and
  114. ((node_type == cmark.NODE_LINK and cmark.node_get_url(cur) == '@') or
  115. node_type == cmark.NODE_HEADING) then
  116. local anchor
  117. local label = extract_label(cur)
  118. local ident = to_identifier(label)
  119. if node_type == cmark.NODE_LINK then
  120. if format == 'latex' then
  121. anchor = make_latex({start="\\hypertarget{" .. ident .. "}{",
  122. stop="\\label{" .. ident .. "}}",
  123. block = true})
  124. else
  125. anchor = make_html_inline('a', {{'id', ident}, {'href', '#'..ident},
  126. {'class', 'definition'}})
  127. end
  128. else -- NODE_HEADING
  129. local level = cmark.node_get_heading_level(cur)
  130. local last_level = #toc == 0 and 1 or toc[#toc].level
  131. if #number > 0 then
  132. if level > last_level then -- subhead
  133. number[level] = 1
  134. else
  135. while last_level > level do
  136. number[last_level] = nil
  137. last_level = last_level - 1
  138. end
  139. number[level] = number[level] + 1
  140. end
  141. end
  142. table.insert(toc, { label = label, ident = ident, level = level, number = render_number(number) })
  143. local num = render_number(number)
  144. local section_cmds = {"\\section", "\\subsection",
  145. "\\subsubsection", "\\chapter"}
  146. if format == 'latex' then
  147. anchor = make_latex({start="\\hypertarget{" .. ident .. "}{" ..
  148. section_cmds[level] .. "{",
  149. stop="}\\label{" .. ident .. "}}",
  150. block = true})
  151. else
  152. anchor = make_html_block('h' .. tostring(math.floor(level)),
  153. {{'id', ident},
  154. {'class', 'definition'}})
  155. if num ~= '' then
  156. local numspan = make_html_inline('span', {{'class','number'}})
  157. node_append_child(numspan, make_text(num))
  158. node_append_child(anchor, numspan)
  159. end
  160. end
  161. end
  162. local children = {}
  163. local child = cmark.node_first_child(cur)
  164. while child do
  165. children[#children + 1] = child
  166. child = cmark.node_next(child)
  167. end
  168. for _,child in ipairs(children) do
  169. node_append_child(anchor, child)
  170. end
  171. cmark.node_insert_before(cur, anchor)
  172. cmark.node_unlink(cur)
  173. elseif entering and node_type == cmark.NODE_CODE_BLOCK and
  174. cmark.node_get_fence_info(cur) == 'example' then
  175. example = example + 1
  176. -- split into two code blocks
  177. local code = cmark.node_get_literal(cur)
  178. local sepstart, sepend = code:find("[\n\r]+%.[\n\r]+")
  179. if not sepstart then
  180. warn("Could not find separator in:\n" .. contents)
  181. end
  182. local markdown_code = cmark.node_new(cmark.NODE_CODE_BLOCK)
  183. local html_code = cmark.node_new(cmark.NODE_CODE_BLOCK)
  184. -- note: we replace the ␣ with a special span after rendering
  185. local markdown_code_string = code:sub(1, sepstart):gsub(' ', '␣')
  186. local html_code_string = code:sub(sepend + 1):gsub(' ', '␣')
  187. cmark.node_set_literal(markdown_code, markdown_code_string)
  188. cmark.node_set_fence_info(markdown_code, 'markdown')
  189. cmark.node_set_literal(html_code, html_code_string)
  190. cmark.node_set_fence_info(html_code, 'html')
  191. local example_div, leftcol_div, rightcol_div
  192. if format == 'latex' then
  193. example_div = make_latex({start = '\\begin{minipage}[t]{\\textwidth}\n{\\scriptsize Example ' .. tostring(example) .. '}\n\n\\vspace{-0.4em}\n', stop = '\\end{minipage}', block = true})
  194. leftcol_div = make_latex({start = "\\begin{minipage}[t]{0.49\\textwidth}\n\\definecolor{shadecolor}{gray}{0.85}\n\\begin{snugshade}\\small\n", stop = "\\end{snugshade}\n\\end{minipage}\n\\hfill", block = true})
  195. rightcol_div = make_latex({start = "\\begin{minipage}[t]{0.49\\textwidth}\n\\definecolor{shadecolor}{gray}{0.95}\n\\begin{snugshade}\\small\n", stop = "\\end{snugshade}\n\\end{minipage}\n\\vspace{0.8em}", block = true})
  196. cmark.node_append_child(leftcol_div, markdown_code)
  197. cmark.node_append_child(rightcol_div, html_code)
  198. cmark.node_append_child(example_div, leftcol_div)
  199. cmark.node_append_child(example_div, rightcol_div)
  200. else
  201. leftcol_div = make_html_block('div', {{'class','column'}})
  202. rightcol_div = make_html_block('div', {{'class', 'column'}})
  203. cmark.node_append_child(leftcol_div, markdown_code)
  204. cmark.node_append_child(rightcol_div, html_code)
  205. local examplenum_div = make_html_block('div', {{'class', 'examplenum'}})
  206. local interact_link = make_html_inline('a', {{'class', 'dingus'},
  207. {'title', 'open in interactive dingus'}})
  208. cmark.node_append_child(interact_link, make_text("Try It"))
  209. local examplenum_link = cmark.node_new(cmark.NODE_LINK)
  210. cmark.node_set_url(examplenum_link, '#example-' .. tostring(example))
  211. cmark.node_append_child(examplenum_link,
  212. make_text("Example " .. tostring(example)))
  213. cmark.node_append_child(examplenum_div, examplenum_link)
  214. if format == 'html' then
  215. cmark.node_append_child(examplenum_div, interact_link)
  216. end
  217. example_div = make_html_block('div', {{'class', 'example'},
  218. {'id','example-' .. tostring(example)}})
  219. cmark.node_append_child(example_div, examplenum_div)
  220. cmark.node_append_child(example_div, leftcol_div)
  221. cmark.node_append_child(example_div, rightcol_div)
  222. end
  223. cmark.node_insert_before(cur, example_div)
  224. cmark.node_unlink(cur)
  225. cmark.node_free(cur)
  226. elseif node_type == cmark.NODE_HTML_BLOCK and
  227. cmark.node_get_literal(cur) == '<!-- END TESTS -->\n' then
  228. -- change numbering
  229. number = {}
  230. if format ~= 'latex' then
  231. local appendices = make_html_block('div', {{'class','appendices'}})
  232. cmark.node_insert_after(cur, appendices)
  233. -- put the remaining sections in an appendix
  234. local tmp = cmark.node_next(appendices)
  235. while tmp do
  236. cmark.node_append_child(appendices, tmp)
  237. tmp = cmark.node_next(tmp)
  238. end
  239. end
  240. end
  241. end
  242. meta.toc = make_toc(toc)
  243. end
  244. local to_ref = function(ref)
  245. return '[' .. ref.label .. ']: #' .. ref.indent .. '\n'
  246. end
  247. local inp = io.read("*a")
  248. local doc1 = cmark.parse_string(inp, cmark.OPT_DEFAULT)
  249. local refs = extract_references(doc1)
  250. local refblock = '\n'
  251. for lab,ident in pairs(refs) do
  252. refblock = refblock .. '[' .. lab .. ']: #' .. ident .. '\n'
  253. -- refblock = refblock .. '[' .. lab .. 's]: #' .. ident .. '\n'
  254. end
  255. -- append references and parse again
  256. local contents, meta, msg = lcmark.convert(inp .. refblock, format,
  257. { smart = true,
  258. yaml_metadata = true,
  259. safe = false,
  260. filters = { create_anchors }
  261. })
  262. if contents then
  263. local f = io.open("tools/template." .. format, 'r')
  264. if not f then
  265. io.stderr:write("Could not find template!")
  266. os.exit(1)
  267. end
  268. local template = f:read("*a")
  269. if format == 'html' then
  270. contents = contents:gsub('␣', '<span class="space"> </span>')
  271. end
  272. meta.body = contents
  273. local rendered, msg = lcmark.render_template(template, meta)
  274. if not rendered then
  275. io.stderr:write(msg)
  276. os.exit(1)
  277. end
  278. io.write(rendered)
  279. os.exit(0)
  280. else
  281. io.stderr:write(msg)
  282. os.exit(1)
  283. end