summaryrefslogtreecommitdiff
path: root/data/pandoc.lua
blob: 78e2fa07a8c8c92addf42e807e6ade7c880dcd49 (plain)
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
--[[
pandoc.lua

Copyright (c) 2017 Albert Krewinkel

Permission to use, copy, modify, and/or distribute this software for any purpose
with or without fee is hereby granted, provided that the above copyright notice
and this permission notice appear in all copies.

THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF
THIS SOFTWARE.
]]

--- The module
local M = {
  _version = "0.1.0"
}

--- Create a new set of attributes (Attr).
function M.Attributes(id, classes, key_values)
  return {id, classes, key_values}
end

------------------------------------------------------------------------
--- Document AST elements
local Element = {}

--- Create a new element subtype
function Element:make_subtype(o)
  o = o or {}
  setmetatable(o, self)
  self.__index = self
  return o
end

--- Create a new element given its tag and arguments
function Element:new(tag, ...)
  local element = { t = tag }
  local content = {...}
  -- special case for unary constructors
  if #content == 1 then
    element.c = content[1]
  -- Don't set 'c' field if no further arguments were given. This is important
  -- for nullary constructors like `Space` and `HorizontalRule`.
  elseif #content > 0 then
    element.c = content
  end
  setmetatable(element, self)
  self.__index = self
  return element
end

--- Create a new constructor
-- @param tag Tag used to identify the constructor
-- @param fn Function to be called when constructing a new element
-- @return function that constructs a new element
function Element:create_constructor(tag, fn)
  local constr = self:make_subtype({tag = tag})
  function constr:new(...)
    local obj = fn(...)
    setmetatable(obj, self)
    self.__index = function(t, k)
      if k == "c" then
        return t["content"]
      elseif k == "t" then
        return getmetatable(t)["tag"]
      else
        return getmetatable(t)[k]
      end
    end
    return obj
  end
  return constr
end

function Element.__call(t, ...)
  return t:new(...)
end

------------------------------------------------------------------------
-- Document
local function Doc(blocks, meta)
  return {
    ["blocks"] = blocks,
    ["meta"] = meta,
    ["pandoc-api-version"] = {1,17,0,5},
  }
end
local Inline = Element:make_subtype{}
function Inline.__call(t, ...)
  return t:new(...)
end

local Block = Element:make_subtype{}

------------------------------------------------------------------------
-- Inline element constructors
-- @section

--- Create a Cite inline element
-- @function Inline.Cite
Inline.Cite = Inline:create_constructor(
  "Cite",
  function(lst, cs) return {c = {cs, lst}} end
)
--- Create a Code inline element
-- @function Inline.Code
Inline.Code = Inline:create_constructor(
  "Code",
  function(code, attr) return {c = {attr, code}} end
)
--- Create a Emph inline element
-- @function Inline.Emph
Inline.Emph = Inline:create_constructor(
  "Emph",
  function(xs) return {c = xs} end
)
--- Create a Image inline element
-- @function Inline.Image
Inline.Image = Inline:create_constructor(
  "Image",
  function(capt, src, tit, attr) return {c = {attr, capt, {src, tit}}} end
)
--- Create a LineBreak inline element
-- @function Inline.LineBreak
Inline.LineBreak = Inline:create_constructor(
  "LineBreak",
  function() return {} end
)
--- Create a Link inline element
-- @function Inline.Link
Inline.Link = Inline:create_constructor(
  "Link",
  function(txt, src, tit, attr) return {c = {attr, txt, {src, tit}}} end
)
--- Create a Math inline element
-- @function Inline.Math
Inline.Math = Inline:create_constructor(
  "Math",
  function(m, str) return {c = {m, str}} end
)
--- Create a Note inline element
-- @function Inline.Note
Inline.Note = Inline:create_constructor(
  "Note",
  function(contents) return {c = contents} end
)
--- Create a Quoted inline element
-- @function Inline.Quoted
Inline.Quoted = Inline:create_constructor(
  "Quoted",
  function(qt, lst) return {c = {qt, lst}} end
)
--- Create a RawInline inline element
-- @function Inline.RawInline
Inline.RawInline = Inline:create_constructor(
  "RawInline",
  function(f, xs) return {c = {f, xs}} end
)
--- Create a SmallCaps inline element
-- @function Inline.SmallCaps
Inline.SmallCaps = Inline:create_constructor(
  "SmallCaps",
  function(xs) return {c = xs} end
)
--- Create a SoftBreak inline element
-- @function Inline.SoftBreak
Inline.SoftBreak = Inline:create_constructor(
  "SoftBreak",
  function() return {} end
)
--- Create a Space inline element
-- @function Inline.Space
Inline.Space = Inline:create_constructor(
  "Space",
  function() return {} end
)
--- Create a Span inline element
-- @function Inline.Span
Inline.Span = Inline:create_constructor(
  "Span",
  function(ls, attr) return {c = {attr, xs}} end
)
--- Create a Str inline element
-- @function Inline.Str
Inline.Str = Inline:create_constructor(
  "Str",
  function(str) return {c = str} end
)
--- Create a Strikeout inline element
-- @function Inline.Strikeout
Inline.Strikeout = Inline:create_constructor(
  "Strikeout",
  function(xs) return {c = xs} end
)
--- Create a Strong inline element
-- @function Inline.Strong
Inline.Strong = Inline:create_constructor(
  "Strong",
  function(xs) return {c = xs} end
)
--- Create a Subscript inline element
-- @function Inline.Subscript
Inline.Subscript = Inline:create_constructor(
  "Subscript",
  function(xs) return {c = xs} end
)
--- Create a Superscript inline element
-- @function Inline.Superscript
Inline.Superscript = Inline:create_constructor(
  "Superscript",
  function(xs) return {c = xs} end
)

M.block_types = {
  "BlockQuote",
  "BulletList",
  "CodeBlock",
  "DefinitionList",
  "Div",
  "Header",
  "HorizontalRule",
  "HorizontalRule",
  "LineBlock",
  "Null",
  "OrderedList",
  "Para",
  "Plain",
  "RawBlock",
  "Table",
}

M.inline_types = {
  "Cite",
  "Code",
  "Emph",
  "Image",
  "LineBreak",
  "Link",
  "Math",
  "Note",
  "Quoted",
  "RawInline",
  "SmallCaps",
  "SoftBreak",
  "Space",
  "Span",
  "Str",
  "Strikeout",
  "Strong",
  "Subscript",
  "Superscript"
}


for _, block_type in pairs(M.block_types) do
  M[block_type] = function(...)
    return Block:new(block_type, ...)
  end
end

for _, inline_type in pairs(M.inline_types) do
  M[inline_type] = Inline[inline_type]
end

--- Arrays to provide fast lookup of element types
local set_of_inline_types = {}
local set_of_block_types = {}

for i = 1, #M.inline_types do
  set_of_inline_types[M.inline_types[i]] = true
end
for i = 1, #M.block_types do
  set_of_block_types[M.block_types[i]] = true
end

function M.global_filter()
  local res = {}
  for k, v in pairs(_G) do
    if set_of_inline_types[k] or set_of_block_types[k] or k == "Doc" then
      res[k] = v
    end
  end
  return res
end

M["Doc"] = Doc
M["Inline"] = Inline
M["Block"] = Block

return M