aboutsummaryrefslogtreecommitdiff
path: root/data/pandoc.lua
blob: 79729fc3570584d476ad3baf2e12fdd2e4f206b1 (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
--[[
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

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

local function Doc(blocks, meta)
  return {
    ["blocks"] = blocks,
    ["meta"] = meta,
    ["pandoc-api-version"] = {1,17,0,5},
  }
end

local Inline = Element:make_subtype{}
local Block = Element:make_subtype{}

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

M.inline_types = {
  "Cite",
  "Code",
  "DisplayMath",
  "DoubleQuoted",
  "Emph",
  "Image",
  "InlineMath",
  "LineBreak",
  "Link",
  "Math",
  "Note",
  "Quoted",
  "RawInline",
  "SingleQuoted",
  "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] = function(...)
    return Inline:new(inline_type, ...)
  end
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

return M