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