-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
djot-reader.lua
459 lines (404 loc) · 10.7 KB
/
djot-reader.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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
local djot = require("djot")
local ast = require("djot.ast")
local insert_attribute, copy_attributes =
ast.insert_attribute, ast.copy_attributes
local emoji -- require this later, only if emoji encountered
local format = string.format
local find, gsub = string.find, string.gsub
-- Produce a copy of a table.
local function copy(tbl)
local result = {}
if tbl then
for k,v in pairs(tbl) do
local newv = v
if type(v) == "table" then
newv = copy(v)
end
result[k] = newv
end
end
return result
end
local Renderer = {}
function Renderer:new()
local state = {
tight = false,
footnotes = nil,
references = nil
}
setmetatable(state, self)
self.__index = self
return state
end
local function words(s)
if s then
local res = {}
string.gsub(s, "(%S+)", function(x) table.insert(res, x) end)
return res
else
return {}
end
end
local function to_attr(attr)
if not attr then
return nil
end
local result = copy(attr)
result.id = nil
result.class = nil
return pandoc.Attr(attr.id or "", words(attr.class), result)
end
function Renderer:with_optional_span(node, f)
local base = f(self:render_children(node))
if node.attr then
return pandoc.Span(base, to_attr(node.attr))
else
return base
end
end
function Renderer:with_optional_div(node, f)
local base = f(self:render_children(node))
if node.attr then
return pandoc.Div(base, to_attr(node.attr))
else
return base
end
end
function Renderer:render_node(node)
return self[node.tag](self, node)
end
function Renderer:render_children(node)
local buff = {}
local inline = false
if node.children and #node.children > 0 then
local oldtight
if node.tight ~= nil then
oldtight = self.tight
self.tight = node.tight
end
local function integrate_elt(elt)
if elt.__name == "Inlines" or elt.__name == "Blocks" then
for i=1,#elt do
integrate_elt(elt[i])
end
else
buff[#buff + 1] = elt
end
end
for _,child in ipairs(node.children) do
local elt = self:render_node(child)
integrate_elt(elt)
end
if node.tight ~= nil then
self.tight = oldtight
end
end
return buff
end
function Renderer:doc(node)
self.footnotes = node.footnotes
self.references = node.references
return pandoc.Pandoc(self:render_children(node))
end
function Renderer:section(node)
local attrs = to_attr(node.attr)
table.insert(attrs.classes, 1, "section")
return pandoc.Div(self:render_children(node), attrs)
end
function Renderer:raw_block(node)
return pandoc.RawBlock(node.format, node.text)
end
function Renderer:para(node)
local constructor = pandoc.Para
if self.tight then
constructor = pandoc.Plain
end
return self:with_optional_div(node, constructor)
end
function Renderer:blockquote(node)
return self:with_optional_div(node, pandoc.BlockQuote)
end
function Renderer:div(node)
return pandoc.Div(self:render_children(node), to_attr(node.attr))
end
function Renderer:heading(node)
return pandoc.Header(node.level,
self:render_children(node),
to_attr(node.attr))
end
function Renderer:thematic_break(node)
if node.attr then
return pandoc.Div(pandoc.HorizontalRule(), to_attr(node.attr))
else
return pandoc.HorizontalRule()
end
end
function Renderer:code_block(node)
local attr = copy(to_attr(node.attr))
if not attr.class then
attr.class = node.lang
else
attr.class = node.lang .. " " .. attr.class
end
return pandoc.CodeBlock(node.text:gsub("\n$",""), attr)
end
function Renderer:table(node)
local rows = {}
local headers = {}
local caption = {}
local aligns = {}
local widths = {}
local content = node.c
for i=1,#content do
local row = content[i]
if row.t == "caption" then
caption = self:render_children(row)
elseif row.t == "row" then
local cells = {}
for j=1,#row.c do
cells[j] = self:render_node(row.c[j])
if not aligns[j] then
local align = row.c[j].align
if not align then
aligns[j] = "AlignDefault"
elseif align == "center" then
aligns[j] = "AlignCenter"
elseif align == "left" then
aligns[j] = "AlignLeft"
elseif align == "right" then
aligns[j] = "AlignRight"
end
widths[j] = 0
end
end
if row.head then
headers = cells
else
rows[#rows + 1] = cells
end
end
end
return pandoc.utils.from_simple_table(
pandoc.SimpleTable(caption, aligns, widths, headers, rows))
end
function Renderer:cell(node)
return { pandoc.Plain(self:render_children(node)) }
end
function Renderer:list(node)
local sty = node.style
if sty == "*" or sty == "+" or sty == "-" then
return self:with_optional_div(node, pandoc.BulletList)
elseif sty == "X" then
return self:with_optional_div(node, pandoc.BulletList)
elseif sty == ":" then
return self:with_optional_div(node, pandoc.DefinitionList)
else
local start = 1
local sty = "DefaultStyle"
local delim = "DefaultDelim"
if node.start and node.start > 1 then
start = node.start
end
local list_type = gsub(node.style, "%p", "")
if list_type == "a" then
sty = "LowerAlpha"
elseif list_type == "A" then
sty = "UpperAlpha"
elseif list_type == "i" then
sty = "LowerRoman"
elseif list_type == "I" then
sty = "UpperRoman"
end
local list_delim = gsub(node.style, "%P", "")
if list_delim == ")" then
delim = "OneParen"
elseif list_delim == "()" then
delim = "TwoParens"
end
return self:with_optional_div(node, function(x)
return pandoc.OrderedList(x,
pandoc.ListAttributes(start, sty, delim))
end)
end
end
function Renderer:list_item(node)
local children = self:render_children(node)
if node.checkbox then
local box = (node.checkbox == "checked" and "☒") or "☐"
local tag = children[1].tag
if tag == "Para" or tag == "Plain" then
children[1].content:insert(1, pandoc.Space())
children[1].content:insert(1, pandoc.Str(box))
else
children:insert(1, pandoc.Para{pandoc.Str(box), pandoc.Space()})
end
end
return children
end
function Renderer:definition_list_item(node)
local term = self:render_node(node.children[1])
local defn = self:render_node(node.children[2])
return { term, defn }
end
function Renderer:term(node)
return self:render_children(node)
end
function Renderer:definition(node)
return self:render_children(node)
end
function Renderer:reference_definition()
return ""
end
function Renderer:footnote_reference(node)
local label = node.text
local note = self.footnotes[label]
if note then
return pandoc.Note(self:render_children(note))
else
io.stderr:write("Note " .. label .. " not found.")
return pandoc.Str("[^" .. label .. "]")
end
end
function Renderer:raw_inline(node)
return pandoc.RawInline(node.format, node.text)
end
function Renderer:str(node)
-- add a span, if needed, to contain attribute on a bare string:
if node.attr then
return pandoc.Span(pandoc.Inlines(node.text), to_attr(node.attr))
else
return pandoc.Inlines(node.text)
end
end
function Renderer:softbreak()
return pandoc.SoftBreak()
end
function Renderer:hardbreak()
return pandoc.LineBreak()
end
function Renderer:nbsp()
return pandoc.Str(" ")
end
function Renderer:verbatim(node)
return pandoc.Code(node.text, to_attr(node.attr))
end
function Renderer:link(node)
local attrs = {}
local dest = node.destination
if node.reference then
local ref = self.references[node.reference]
if ref then
if ref.attributes then
attrs = copy(ref.attributes)
end
dest = ref.destination
else
dest = "#" -- empty href is illegal
end
end
-- link's attributes override reference's:
copy_attributes(attrs, node.attr)
local title = attrs.title
attrs.title = nil
return pandoc.Link(self:render_children(node), dest,
title, to_attr(attrs))
end
function Renderer:image(node)
local attrs = {}
local dest = node.destination
if node.reference then
local ref = self.references[node.reference]
if ref then
if ref.attributes then
attrs = copy(ref.attributes)
end
dest = ref.destination
else
dest = "#" -- empty href is illegal
end
end
-- image's attributes override reference's:
copy_attributes(attrs, node.attr)
return pandoc.Image(self:render_children(node), dest,
title, to_attr(attrs))
end
function Renderer:span(node)
return pandoc.Span(self:render_children(node), to_attr(node.attr))
end
function Renderer:mark(node)
local attr = copy(node.attr)
if attr.class then
attr.class = "mark " .. attr.class
else
attr = { class = "mark" }
end
return pandoc.Span(self:render_children(node), to_attr(attr))
end
function Renderer:insert(node)
local attr = copy(node.attr)
if attr.class then
attr.class = "insert " .. attr.class
else
attr = { class = "insert" }
end
return pandoc.Span(self:render_children(node), to_attr(attr))
end
function Renderer:delete(node)
return self:with_optional_span(node, pandoc.Strikeout)
end
function Renderer:subscript(node)
return self:with_optional_span(node, pandoc.Subscript)
end
function Renderer:superscript(node)
return self:with_optional_span(node, pandoc.Superscript)
end
function Renderer:emph(node)
return self:with_optional_span(node, pandoc.Emph)
end
function Renderer:strong(node)
return self:with_optional_span(node, pandoc.Strong)
end
function Renderer:double_quoted(node)
return self:with_optional_span(node,
function(x) return pandoc.Quoted("DoubleQuote", x) end)
end
function Renderer:single_quoted(node)
return self:with_optional_span(node,
function(x) return pandoc.Quoted("SingleQuote", x) end)
end
function Renderer:left_double_quote()
return "“"
end
function Renderer:right_double_quote()
return "”"
end
function Renderer:left_single_quote()
return "‘"
end
function Renderer:right_single_quote()
return "’"
end
function Renderer:ellipses()
return "…"
end
function Renderer:em_dash()
return "—"
end
function Renderer:en_dash()
return "–"
end
function Renderer:symbol(node)
return pandoc.Span(":" .. node.alias .. ":",
pandoc.Attr("",{"symbol"},{["alias"] = node.alias}))
end
function Renderer:math(node)
local math_type = "InlineMath"
if find(node.attr.class, "display") then
math_type = "DisplayMath"
end
return pandoc.Math(math_type, node.text)
end
function Reader(input)
local doc = djot.parse(tostring(input))
return Renderer:render_node(doc)
end