git.iko.soy/typeable/pandoc
on master help sign in
Lua module: provide multi-param Inline constructors
97dfe78Albert Krewinkel
data/pandoc.lua+160−4
@@ -26,7 +26,10 @@ function M.Attributes(id, classes, key_values)
26 return {id, classes, key_values}
26 return {id, classes, key_values}
27end
27end
28
28
29+------------------------------------------------------------------------
30+--- Document AST elements
29local Element = {}
31local Element = {}
32+
30--- Create a new element subtype
33--- Create a new element subtype
31function Element:make_subtype(o)
34function Element:make_subtype(o)
32 o = o or {}
35 o = o or {}
@@ -34,6 +37,7 @@ function Element:make_subtype(o)
34 self.__index = self
37 self.__index = self
35 return o
38 return o
36end
39end
40+
37--- Create a new element given its tag and arguments
41--- Create a new element given its tag and arguments
38function Element:new(tag, ...)
42function Element:new(tag, ...)
39 local element = { t = tag }
43 local element = { t = tag }
@@ -51,6 +55,35 @@ function Element:new(tag, ...)
51 return element
55 return element
52end
56end
53
57
58+--- Create a new constructor
59+-- @param tag Tag used to identify the constructor
60+-- @param fn Function to be called when constructing a new element
61+-- @return function that constructs a new element
62+function Element:create_constructor(tag, fn)
63+ local constr = self:make_subtype({tag = tag})
64+ function constr:new(...)
65+ local obj = fn(...)
66+ setmetatable(obj, self)
67+ self.__index = function(t, k)
68+ if k == "c" then
69+ return t["content"]
70+ elseif k == "t" then
71+ return getmetatable(t)["tag"]
72+ else
73+ return getmetatable(t)[k]
74+ end
75+ end
76+ return obj
77+ end
78+ return constr
79+end
80+
81+function Element.__call(t, ...)
82+ return t:new(...)
83+end
84+
85+------------------------------------------------------------------------
86+-- Document
54local function Doc(blocks, meta)
87local function Doc(blocks, meta)
55 return {
88 return {
56 ["blocks"] = blocks,
89 ["blocks"] = blocks,
@@ -58,10 +91,132 @@ local function Doc(blocks, meta)
58 ["pandoc-api-version"] = {1,17,0,5},
91 ["pandoc-api-version"] = {1,17,0,5},
59 }
92 }
60end
93end
61−
62local Inline = Element:make_subtype{}
94local Inline = Element:make_subtype{}
95+function Inline.__call(t, ...)
96+ return t:new(...)
97+end
98+
63local Block = Element:make_subtype{}
99local Block = Element:make_subtype{}
64
100
101+------------------------------------------------------------------------
102+-- Inline element constructors
103+-- @section
104+
105+--- Create a Cite inline element
106+-- @function Inline.Cite
107+Inline.Cite = Inline:create_constructor(
108+ "Cite",
109+ function(lst, cs) return {c = {cs, lst}} end
110+)
111+--- Create a Code inline element
112+-- @function Inline.Code
113+Inline.Code = Inline:create_constructor(
114+ "Code",
115+ function(code, attr) return {c = {attr, code}} end
116+)
117+--- Create a Emph inline element
118+-- @function Inline.Emph
119+Inline.Emph = Inline:create_constructor(
120+ "Emph",
121+ function(xs) return {c = xs} end
122+)
123+--- Create a Image inline element
124+-- @function Inline.Image
125+Inline.Image = Inline:create_constructor(
126+ "Image",
127+ function(capt, src, tit, attr) return {c = {attr, capt, {src, tit}}} end
128+)
129+--- Create a LineBreak inline element
130+-- @function Inline.LineBreak
131+Inline.LineBreak = Inline:create_constructor(
132+ "LineBreak",
133+ function() return {} end
134+)
135+--- Create a Link inline element
136+-- @function Inline.Link
137+Inline.Link = Inline:create_constructor(
138+ "Link",
139+ function(txt, src, tit, attr) return {c = {attr, txt, {src, tit}}} end
140+)
141+--- Create a Math inline element
142+-- @function Inline.Math
143+Inline.Math = Inline:create_constructor(
144+ "Math",
145+ function(m, str) return {c = {m, str}} end
146+)
147+--- Create a Note inline element
148+-- @function Inline.Note
149+Inline.Note = Inline:create_constructor(
150+ "Note",
151+ function(contents) return {c = contents} end
152+)
153+--- Create a Quoted inline element
154+-- @function Inline.Quoted
155+Inline.Quoted = Inline:create_constructor(
156+ "Quoted",
157+ function(qt, lst) return {c = {qt, lst}} end
158+)
159+--- Create a RawInline inline element
160+-- @function Inline.RawInline
161+Inline.RawInline = Inline:create_constructor(
162+ "RawInline",
163+ function(f, xs) return {c = {f, xs}} end
164+)
165+--- Create a SmallCaps inline element
166+-- @function Inline.SmallCaps
167+Inline.SmallCaps = Inline:create_constructor(
168+ "SmallCaps",
169+ function(xs) return {c = xs} end
170+)
171+--- Create a SoftBreak inline element
172+-- @function Inline.SoftBreak
173+Inline.SoftBreak = Inline:create_constructor(
174+ "SoftBreak",
175+ function() return {} end
176+)
177+--- Create a Space inline element
178+-- @function Inline.Space
179+Inline.Space = Inline:create_constructor(
180+ "Space",
181+ function() return {} end
182+)
183+--- Create a Span inline element
184+-- @function Inline.Span
185+Inline.Span = Inline:create_constructor(
186+ "Span",
187+ function(ls, attr) return {c = {attr, xs}} end
188+)
189+--- Create a Str inline element
190+-- @function Inline.Str
191+Inline.Str = Inline:create_constructor(
192+ "Str",
193+ function(str) return {c = str} end
194+)
195+--- Create a Strikeout inline element
196+-- @function Inline.Strikeout
197+Inline.Strikeout = Inline:create_constructor(
198+ "Strikeout",
199+ function(xs) return {c = xs} end
200+)
201+--- Create a Strong inline element
202+-- @function Inline.Strong
203+Inline.Strong = Inline:create_constructor(
204+ "Strong",
205+ function(xs) return {c = xs} end
206+)
207+--- Create a Subscript inline element
208+-- @function Inline.Subscript
209+Inline.Subscript = Inline:create_constructor(
210+ "Subscript",
211+ function(xs) return {c = xs} end
212+)
213+--- Create a Superscript inline element
214+-- @function Inline.Superscript
215+Inline.Superscript = Inline:create_constructor(
216+ "Superscript",
217+ function(xs) return {c = xs} end
218+)
219+
65M.block_types = {
220M.block_types = {
66 "BlockQuote",
221 "BlockQuote",
67 "BulletList",
222 "BulletList",
@@ -102,6 +257,7 @@ M.inline_types = {
102 "Superscript"
257 "Superscript"
103}
258}
104
259
260+
105for _, block_type in pairs(M.block_types) do
261for _, block_type in pairs(M.block_types) do
106 M[block_type] = function(...)
262 M[block_type] = function(...)
107 return Block:new(block_type, ...)
263 return Block:new(block_type, ...)
@@ -109,9 +265,7 @@ for _, block_type in pairs(M.block_types) do
109end
265end
110
266
111for _, inline_type in pairs(M.inline_types) do
267for _, inline_type in pairs(M.inline_types) do
112− M[inline_type] = function(...)
113− return Inline:new(inline_type, ...)
268+ M[inline_type] = Inline[inline_type]
114− end
115end
269end
116
270
117--- Arrays to provide fast lookup of element types
271--- Arrays to provide fast lookup of element types
@@ -136,5 +290,7 @@ function M.global_filter()
136end
290end
137
291
138M["Doc"] = Doc
292M["Doc"] = Doc
293+M["Inline"] = Inline
294+M["Block"] = Block
139
295
140return M
296return M