git.iko.soy/typeable/pandoc
on 9575dfc help sign in
typeable/pandoc/data/pandoc.lua
144 lines 3.1 kB history raw
1--[[
2pandoc.lua
4Copyright (c) 2017 Albert Krewinkel
6Permission to use, copy, modify, and/or distribute this software for any purpose
7with or without fee is hereby granted, provided that the above copyright notice
8and this permission notice appear in all copies.
10THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
11REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
12FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
13INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
14OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
15TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF
16THIS SOFTWARE.
17]]
19--- The module
20local M = {
21 _version = "0.1.0"
24--- Create a new set of attributes (Attr).
25function M.Attributes(id, classes, key_values)
26 return {id, classes, key_values}
27end
29local Element = {}
30--- Create a new element subtype
31function Element:make_subtype(o)
32 o = o or {}
33 setmetatable(o, self)
34 self.__index = self
35 return o
36end
37--- Create a new element given its tag and arguments
38function Element:new(tag, ...)
39 local element = { t = tag }
40 local content = {...}
41 -- special case for unary constructors
42 if #content == 1 then
43 element.c = content[1]
44 -- Don't set 'c' field if no further arguments were given. This is important
45 -- for nullary constructors like `Space` and `HorizontalRule`.
46 elseif #content > 0 then
47 element.c = content
48 end
49 setmetatable(element, self)
50 self.__index = self
51 return element
52end
54local function Doc(blocks, meta)
55 return {
56 ["blocks"] = blocks,
57 ["meta"] = meta,
58 ["pandoc-api-version"] = {1,17,0,5},
59 }
60end
62local Inline = Element:make_subtype{}
63local Block = Element:make_subtype{}
65M.block_types = {
66 "BlockQuote",
67 "BulletList",
68 "CodeBlock",
69 "DefinitionList",
70 "Div",
71 "Header",
72 "HorizontalRule",
73 "HorizontalRule",
74 "LineBlock",
75 "Null",
76 "OrderedList",
77 "Para",
78 "Plain",
79 "RawBlock",
80 "Table",
83M.inline_types = {
84 "Cite",
85 "Code",
86 "DisplayMath",
87 "DoubleQuoted",
88 "Emph",
89 "Image",
90 "InlineMath",
91 "LineBreak",
92 "Link",
93 "Math",
94 "Note",
95 "Quoted",
96 "RawInline",
97 "SingleQuoted",
98 "SmallCaps",
99 "SoftBreak",
100 "Space",
101 "Span",
102 "Str",
103 "Strikeout",
104 "Strong",
105 "Subscript",
106 "Superscript"
109for _, block_type in pairs(M.block_types) do
110 M[block_type] = function(...)
111 return Block:new(block_type, ...)
112 end
113end
115for _, inline_type in pairs(M.inline_types) do
116 M[inline_type] = function(...)
117 return Inline:new(inline_type, ...)
118 end
119end
121--- Arrays to provide fast lookup of element types
122local set_of_inline_types = {}
123local set_of_block_types = {}
125for i = 1, #M.inline_types do
126 set_of_inline_types[M.inline_types[i]] = true
127end
128for i = 1, #M.block_types do
129 set_of_block_types[M.block_types[i]] = true
130end
132function M.global_filter()
133 local res = {}
134 for k, v in pairs(_G) do
135 if set_of_inline_types[k] or set_of_block_types[k] or k == "Doc" then
136 res[k] = v
137 end
138 end
139 return res
140end
142M["Doc"] = Doc
144return M