-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrevno.lua
216 lines (202 loc) · 5.73 KB
/
revno.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
--- revno.lua – eager conversion of pandoc AST elements to Lua tables.
---
--- Copyright: © 2024 Albert Krewinkel
--- License: MIT – see LICENSE for details
local pandoc = require 'pandoc'
-- Debugging functions
local registry = debug.getregistry()
local debug_getmetatable = debug.getmetatable
local debug_setuservalue = debug.setuservalue
--- Get the element type; like pandoc.utils.type, but faster.
local function get_metatable_name (x)
local mt = debug_getmetatable(x)
return mt and mt.__name
end
--- Return the tag (e.g., `Str`) or type (e.g. `Meta`) of an element.
local function tag_or_type (x)
return x.tag or get_metatable_name(x) or type(x)
end
local lazy_th = pandoc.TableHead({})
local lazy_tf = pandoc.TableFoot({})
local lazy_userdata = {
BlockQuote = pandoc.BlockQuote{},
BulletList = pandoc.BulletList{},
Cite = pandoc.Cite({}, {}),
Code = pandoc.Code(""),
CodeBlock = pandoc.CodeBlock(""),
DefinitionList = pandoc.DefinitionList{},
Div = pandoc.Div{},
Emph = pandoc.Emph{},
Header = pandoc.Header(0, {}),
HorizontalRule = pandoc.HorizontalRule(),
Image = pandoc.Image({}, ''),
LineBlock = pandoc.LineBlock{},
LineBreak = pandoc.LineBreak(),
Link = pandoc.Link({}, ''),
Math = pandoc.Math("DisplayMath", ""),
Note = pandoc.Note{},
OrderedList = pandoc.OrderedList{},
Pandoc = pandoc.Pandoc{},
Para = pandoc.Para{},
Plain = pandoc.Plain{},
Quoted = pandoc.Quoted('SingleQuote', {}),
RawBlock = pandoc.RawBlock("placeholder", ""),
RawInline = pandoc.RawInline("placeholder", ""),
SmallCaps = pandoc.SmallCaps{},
SoftBreak = pandoc.SoftBreak(),
Space = pandoc.Space(),
Span = pandoc.Span{},
Str = pandoc.Str "",
Strikeout = pandoc.Strikeout{},
Strong = pandoc.Strong{},
Subscript = pandoc.Subscript{},
Superscript = pandoc.Superscript{},
Table = pandoc.Table({}, {}, lazy_th, {}, lazy_tf),
TableHead = lazy_th,
TableFoot = lazy_tf,
Underline = pandoc.Underline{},
}
--- Make a strict element lazy (if possible).
local function tolazy (element)
local tp = type(element)
if tp ~= 'table' or not element.strict then
-- already lazy
return element
end
local name = tag_or_type(element)
local ud = name and lazy_userdata[name]
if ud then
local new = {}
for key, value in pairs(element) do
new[key] = tolazy(value)
end
debug_setuservalue(ud, new, 1)
local lazy = ud:clone()
return lazy
elseif type(element) == 'table' then
local new = {}
for key, value in pairs(element) do
new[key] = tolazy(value)
end
return setmetatable(new, debug_getmetatable(element))
else
-- don't know what to do, let's just return it unchanged
return element
end
end
local metamethods = {
__tojson = true,
__eq = true,
__tostring = true,
}
local listy_type = {
List = true,
Inlines = true,
Blocks = true,
}
local function make_metatable (name)
local orig =
assert(registry[name], "No such metatable in the registry: " .. name)
local new = {__name = name}
local islisty = listy_type[name]
for key, value in pairs(orig) do
if islisty then
new[key] = value
else
new[key] = (metamethods[key] or listy_type[name]) and
function(obj, ...)
return value(tolazy(obj), ...)
end
end
end
for methodname, fn in pairs(orig.methods or {}) do
new[methodname] = function(obj, ...)
return fn(tolazy(obj), ...)
end
end
new.__name = name -- .. ' (strict)'
new.__index = new
new.strict = true
return new
end
local strict_metatables = {
Blocks = registry['Blocks'],
Inlines = registry['Inlines'],
List = registry['List'],
}
local function get_new_metatable (element)
local name = get_metatable_name(element)
local mt = name and strict_metatables[name]
if name and not mt then
mt = make_metatable(name)
strict_metatables[name] = mt
end
return mt
end
local element_fields = {
-- Blocks with Blocks content
BlockQuote = {'content'},
Div = {'content', 'attr'},
Header = {'attr', 'content', 'level'},
-- Blocks with Inlines content
Para = {'content'},
Plain = {'content'},
-- Blocks with List content
LineBlock = {'content'},
BulletList = {'content'},
OrderedList = {'content'},
DefinitionList = {'content'},
-- Inlines with Inlines content
Cite = {'citations', 'content'},
Emph = {'content'},
Link = {'content', 'attr', 'target', 'title'},
Image = {'caption', 'attr', 'target', 'title'},
Quoted = {'content'},
SmallCaps = {'content'},
Span = {'content'},
Strikeout = {'content'},
Strong = {'content'},
Subscript = {'content'},
Superscript = {'content'},
Underline = {'content'},
-- Inline with Blocks content
Note = {'content'},
Code = {'text', 'attr'},
CodeBlock = {'text', 'attr'},
HorizontalRule = {},
LineBreak = {},
Math = {},
RawBlock = {'text'},
RawInline = {'text'},
Space = {},
SoftBreak = {},
Str = {'text'},
Pandoc = {'blocks', 'meta'}
}
local function tostrict (element)
local tp = type(element)
if (tp ~= 'table' and tp ~= 'userdata') or element.strict then
return element
end
local new = {}
local fields = element_fields[element.tag]
if fields then
new.tag = element.tag
for _, field in pairs(fields) do
-- ignore methods
new[field] = tostrict(element[field])
end
else
for key, value in pairs(element) do
-- ignore methods
new[key] = tostrict(value)
end
end
local mt = get_new_metatable(element)
return setmetatable(new, mt)
end
local M = {
tostrict = tostrict,
tolazy = tolazy,
}
return M