@@ -26,7 +26,10 @@ function M.Attributes(id, classes, key_values)
26 return {id, classes, key_values}
26 return {id, classes, key_values}
29+------------------------------------------------------------------------
30+--- Document AST elements
29local Element = {}
31local Element = {}
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
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
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(...)
66+ setmetatable(obj, self)
67+ self.__index = function(t, k)
71+ return getmetatable(t)["tag"]
73+ return getmetatable(t)[k]
81+function Element.__call(t, ...)
85+------------------------------------------------------------------------
54local function Doc(blocks, meta)
87local function Doc(blocks, meta)
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},
62local Inline = Element:make_subtype{}
94local Inline = Element:make_subtype{}
95+function Inline.__call(t, ...)
63local Block = Element:make_subtype{}
99local Block = Element:make_subtype{}
101+------------------------------------------------------------------------
102+-- Inline element constructors
105+--- Create a Cite inline element
106+-- @function Inline.Cite
107+Inline.Cite = Inline:create_constructor(
109+ function(lst, cs) return {c = {cs, lst}} end
111+--- Create a Code inline element
112+-- @function Inline.Code
113+Inline.Code = Inline:create_constructor(
115+ function(code, attr) return {c = {attr, code}} end
117+--- Create a Emph inline element
118+-- @function Inline.Emph
119+Inline.Emph = Inline:create_constructor(
121+ function(xs) return {c = xs} end
123+--- Create a Image inline element
124+-- @function Inline.Image
125+Inline.Image = Inline:create_constructor(
127+ function(capt, src, tit, attr) return {c = {attr, capt, {src, tit}}} end
129+--- Create a LineBreak inline element
130+-- @function Inline.LineBreak
131+Inline.LineBreak = Inline:create_constructor(
133+ function() return {} end
135+--- Create a Link inline element
136+-- @function Inline.Link
137+Inline.Link = Inline:create_constructor(
139+ function(txt, src, tit, attr) return {c = {attr, txt, {src, tit}}} end
141+--- Create a Math inline element
142+-- @function Inline.Math
143+Inline.Math = Inline:create_constructor(
145+ function(m, str) return {c = {m, str}} end
147+--- Create a Note inline element
148+-- @function Inline.Note
149+Inline.Note = Inline:create_constructor(
151+ function(contents) return {c = contents} end
153+--- Create a Quoted inline element
154+-- @function Inline.Quoted
155+Inline.Quoted = Inline:create_constructor(
157+ function(qt, lst) return {c = {qt, lst}} end
159+--- Create a RawInline inline element
160+-- @function Inline.RawInline
161+Inline.RawInline = Inline:create_constructor(
163+ function(f, xs) return {c = {f, xs}} end
165+--- Create a SmallCaps inline element
166+-- @function Inline.SmallCaps
167+Inline.SmallCaps = Inline:create_constructor(
169+ function(xs) return {c = xs} end
171+--- Create a SoftBreak inline element
172+-- @function Inline.SoftBreak
173+Inline.SoftBreak = Inline:create_constructor(
175+ function() return {} end
177+--- Create a Space inline element
178+-- @function Inline.Space
179+Inline.Space = Inline:create_constructor(
181+ function() return {} end
183+--- Create a Span inline element
184+-- @function Inline.Span
185+Inline.Span = Inline:create_constructor(
187+ function(ls, attr) return {c = {attr, xs}} end
189+--- Create a Str inline element
190+-- @function Inline.Str
191+Inline.Str = Inline:create_constructor(
193+ function(str) return {c = str} end
195+--- Create a Strikeout inline element
196+-- @function Inline.Strikeout
197+Inline.Strikeout = Inline:create_constructor(
199+ function(xs) return {c = xs} end
201+--- Create a Strong inline element
202+-- @function Inline.Strong
203+Inline.Strong = Inline:create_constructor(
205+ function(xs) return {c = xs} end
207+--- Create a Subscript inline element
208+-- @function Inline.Subscript
209+Inline.Subscript = Inline:create_constructor(
211+ function(xs) return {c = xs} end
213+--- Create a Superscript inline element
214+-- @function Inline.Superscript
215+Inline.Superscript = Inline:create_constructor(
217+ function(xs) return {c = xs} end
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"
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
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]
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()
138M["Doc"] = Doc
292M["Doc"] = Doc