git.iko.soy/typeable/pandoc
on master help sign in
Lua module: provide multi-param Inline constructors

Instead of taking only a single argument containing the pre-packed element contents, `Inline` constructors now take the same arguments as the respective filter and `Custom` writer function

commit97dfe782cfc46c51e9585afdc5853cbd0e9e0234 authorAlbert Krewinkel <albert@zeitkraut.de> dateWed 12 Apr 2017, 21:21 (10 years ago) parent2761a38
1 file changed+160−4
data/pandoc.lua+160−4
data/pandoc.lua+160−4
@@ -26,7 +26,10 @@ function M.Attributes(id, classes, key_values)
2626 return {id, classes, key_values}
2727end
2828
29+------------------------------------------------------------------------
30+--- Document AST elements
2931local Element = {}
32+
3033--- Create a new element subtype
3134function Element:make_subtype(o)
3235 o = o or {}
@@ -34,6 +37,7 @@ function Element:make_subtype(o)
3437 self.__index = self
3538 return o
3639end
40+
3741--- Create a new element given its tag and arguments
3842function Element:new(tag, ...)
3943 local element = { t = tag }
@@ -51,6 +55,35 @@ function Element:new(tag, ...)
5155 return element
5256end
5357
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
5487local function Doc(blocks, meta)
5588 return {
5689 ["blocks"] = blocks,
@@ -58,10 +91,132 @@ local function Doc(blocks, meta)
5891 ["pandoc-api-version"] = {1,17,0,5},
5992 }
6093end
61−
6294local Inline = Element:make_subtype{}
95+function Inline.__call(t, ...)
96+ return t:new(...)
97+end
98+
6399local Block = Element:make_subtype{}
64100
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+
65220M.block_types = {
66221 "BlockQuote",
67222 "BulletList",
@@ -102,6 +257,7 @@ M.inline_types = {
102257 "Superscript"
103258}
104259
260+
105261for _, block_type in pairs(M.block_types) do
106262 M[block_type] = function(...)
107263 return Block:new(block_type, ...)
@@ -109,9 +265,7 @@ for _, block_type in pairs(M.block_types) do
109265end
110266
111267for _, inline_type in pairs(M.inline_types) do
112− M[inline_type] = function(...)
113− return Inline:new(inline_type, ...)
114− end
268+ M[inline_type] = Inline[inline_type]
115269end
116270
117271--- Arrays to provide fast lookup of element types
@@ -136,5 +290,7 @@ function M.global_filter()
136290end
137291
138292M["Doc"] = Doc
293+M["Inline"] = Inline
294+M["Block"] = Block
139295
140296return M