git.iko.soy/typeable/pandoc
on master help sign in
Lua module: provide accessors to element properties
commit291e4f39e8a118a723f2981f1eb731a061b0f6c0 authorAlbert Krewinkel <albert@zeitkraut.de> dateSat 15 Apr 2017, 20:00 (10 years ago) parent629c649
1 file changed+109−39
data/pandoc.lua+109−39
data/pandoc.lua+109−39
@@ -64,20 +64,58 @@ end
6464-- @param tag Tag used to identify the constructor
6565-- @param fn Function to be called when constructing a new element
6666-- @return function that constructs a new element
67−function Element:create_constructor(tag, fn)
68− local constr = self:make_subtype({tag = tag})
67+function Element:create_constructor(tag, fn, accessors)
68+ local constr = self:make_subtype({tag = tag, getters = {}, setters = {}})
69+
70+ -- Add accessors to the metatable
71+ if type(accessors) == "string" then
72+ constr.getters[accessors] = function(elem)
73+ return elem.c
74+ end
75+ constr.setters[accessors] = function(elem, v)
76+ elem.c = v
77+ end
78+ else
79+ for i = 1, #(accessors or {}) do
80+ if type(accessors[i]) == "string" then
81+ constr.getters[accessors[i]] = function(elem)
82+ return elem.c[i]
83+ end
84+ constr.setters[accessors[i]] = function(elem, v)
85+ elem.c[i] = v
86+ end
87+ else -- only two levels of nesting are supported
88+ for k, v in ipairs(accessors[i]) do
89+ constr.getters[v] = function(elem)
90+ return elem.c[i][k]
91+ end
92+ constr.setters[v] = function(elem, v)
93+ elem.c[i][k] = v
94+ end
95+ end
96+ end
97+ end
98+ end
99+
69100 function constr:new(...)
70101 local obj = fn(...)
71102 setmetatable(obj, self)
72103 self.__index = function(t, k)
73− if k == "c" then
74− return t["content"]
104+ if getmetatable(t).getters[k] then
105+ return getmetatable(t).getters[k](t)
75106 elseif k == "t" then
76107 return getmetatable(t)["tag"]
77108 else
78109 return getmetatable(t)[k]
79110 end
80111 end
112+ self.__newindex = function(t, k, v)
113+ if getmetatable(t).setters[k] then
114+ getmetatable(t).setters[k](t, v)
115+ else
116+ rawset(t, k, v)
117+ end
118+ end
81119 return obj
82120 end
83121 self.constructor = self.constructor or {}
@@ -152,7 +190,8 @@ for i = 1, #M.meta_value_types do
152190 M.meta_value_types[i],
153191 function(content)
154192 return {c = content}
155− end
193+ end,
194+ "content"
156195 )
157196end
158197
@@ -172,7 +211,8 @@ end
172211-- @treturn Block block quote element
173212M.BlockQuote = M.Block:create_constructor(
174213 "BlockQuote",
175− function(content) return {c = content} end
214+ function(content) return {c = content} end,
215+ "content"
176216)
177217
178218--- Creates a bullet (i.e. unordered) list.
@@ -181,17 +221,19 @@ M.BlockQuote = M.Block:create_constructor(
181221-- @treturn Block block quote element
182222M.BulletList = M.Block:create_constructor(
183223 "BulletList",
184− function(content) return {c = content} end
224+ function(content) return {c = content} end,
225+ "content"
185226)
186227
187228--- Creates a code block element
188229-- @function CodeBlock
189−-- @tparam string code code string
230+-- @tparam string text code string
190231-- @tparam[opt] Attributes attributes element attributes
191232-- @treturn Block code block element
192233M.CodeBlock = M.Block:create_constructor(
193234 "CodeBlock",
194− function(code, attributes) return {c = {attributes, code}} end
235+ function(text, attributes) return {c = {attributes, text}} end,
236+ {{"identifier", "classes", "attributes"}, "text"}
195237)
196238
197239--- Creates a definition list, containing terms and their explanation.
@@ -200,7 +242,8 @@ M.CodeBlock = M.Block:create_constructor(
200242-- @treturn Block block quote element
201243M.DefinitionList = M.Block:create_constructor(
202244 "DefinitionList",
203− function(content) return {c = content} end
245+ function(content) return {c = content} end,
246+ "content"
204247)
205248
206249--- Creates a div element
@@ -210,7 +253,8 @@ M.DefinitionList = M.Block:create_constructor(
210253-- @treturn Block code block element
211254M.Div = M.Block:create_constructor(
212255 "Div",
213− function(content, attributes) return {c = {attributes, content}} end
256+ function(content, attributes) return {c = {attributes, content}} end,
257+ {{"identifier", "classes", "attributes"}, "content"}
214258)
215259
216260--- Creates a block quote element.
@@ -223,7 +267,8 @@ M.Header = M.Block:create_constructor(
223267 "Header",
224268 function(level, attributes, content)
225269 return {c = {level, attributes, content}}
226− end
270+ end,
271+ {"level", {"identifier", "classes", "attributes"}, "content"}
227272)
228273
229274--- Creates a horizontal rule.
@@ -240,7 +285,8 @@ M.HorizontalRule = M.Block:create_constructor(
240285-- @treturn Block block quote element
241286M.LineBlock = M.Block:create_constructor(
242287 "LineBlock",
243− function(content) return {c = content} end
288+ function(content) return {c = content} end,
289+ "content"
244290)
245291
246292--- Creates a null element.
@@ -260,7 +306,8 @@ M.OrderedList = M.Block:create_constructor(
260306 "OrderedList",
261307 function(items, listAttributes)
262308 return {c = {listAttributes,items}}
263− end
309+ end,
310+ {{"start", "style", "delimiter"}, "content"}
264311)
265312
266313--- Creates a para element.
@@ -269,7 +316,8 @@ M.OrderedList = M.Block:create_constructor(
269316-- @treturn Block block quote element
270317M.Para = M.Block:create_constructor(
271318 "Para",
272− function(content) return {c = content} end
319+ function(content) return {c = content} end,
320+ "content"
273321)
274322
275323--- Creates a plain element.
@@ -278,17 +326,19 @@ M.Para = M.Block:create_constructor(
278326-- @treturn Block block quote element
279327M.Plain = M.Block:create_constructor(
280328 "Plain",
281− function(content) return {c = content} end
329+ function(content) return {c = content} end,
330+ "content"
282331)
283332
284333--- Creates a raw content block of the specified format.
285334-- @function RawBlock
286335-- @tparam string format format of content
287−-- @tparam string content string content
336+-- @tparam string text string content
288337-- @treturn Block block quote element
289338M.RawBlock = M.Block:create_constructor(
290339 "RawBlock",
291− function(format, content) return {c = {format, content}} end
340+ function(format, text) return {c = {format, text}} end,
341+ {"format", "text"}
292342)
293343
294344--- Creates a table element.
@@ -324,17 +374,19 @@ end
324374-- @treturn Inline citations element
325375M.Cite = M.Inline:create_constructor(
326376 "Cite",
327− function(content, citations) return {c = {citations, content}} end
377+ function(content, citations) return {c = {citations, content}} end,
378+ {"citations", "content"}
328379)
329380
330381--- Creates a Code inline element
331382-- @function Code
332−-- @tparam string code brief image description
383+-- @tparam string text brief image description
333384-- @tparam[opt] Attributes attributes additional attributes
334385-- @treturn Inline code element
335386M.Code = M.Inline:create_constructor(
336387 "Code",
337− function(code, attributes) return {c = {attributes, code}} end
388+ function(text, attributes) return {c = {attributes, text}} end,
389+ {{"identifier", "classes", "attributes"}, "text"}
338390)
339391
340392--- Creates an inline element representing emphasised text.
@@ -343,7 +395,8 @@ M.Code = M.Inline:create_constructor(
343395-- @treturn Inline emphasis element
344396M.Emph = M.Inline:create_constructor(
345397 "Emph",
346− function(content) return {c = content} end
398+ function(content) return {c = content} end,
399+ "content"
347400)
348401
349402--- Creates a Image inline element
@@ -359,7 +412,8 @@ M.Image = M.Inline:create_constructor(
359412 title = title or ""
360413 attributes = attributes or Attribute.empty
361414 return {c = {attributes, caption, {src, title}}}
362− end
415+ end,
416+ {"attributes", "caption", {"src", "title"}}
363417)
364418
365419--- Create a LineBreak inline element
@@ -383,7 +437,8 @@ M.Link = M.Inline:create_constructor(
383437 title = title or ""
384438 attributes = attributes or Attribute.empty
385439 return {c = {attributes, content, {target, title}}}
386− end
440+ end,
441+ {"attributes", "content", {"target", "title"}}
387442)
388443
389444--- Creates a Math element, either inline or displayed. It is usually simpler to
@@ -396,7 +451,8 @@ M.Math = M.Inline:create_constructor(
396451 "Math",
397452 function(mathtype, text)
398453 return {c = {mathtype, text}}
399− end
454+ end,
455+ {"mathtype", "text"}
400456)
401457--- Creates a DisplayMath element.
402458-- @function DisplayMath
@@ -404,7 +460,8 @@ M.Math = M.Inline:create_constructor(
404460-- @treturn Inline Math element
405461M.DisplayMath = M.Inline:create_constructor(
406462 "DisplayMath",
407− function(text) return M.Math("DisplayMath", text) end
463+ function(text) return M.Math("DisplayMath", text) end,
464+ {"mathtype", "text"}
408465)
409466--- Creates an InlineMath inline element.
410467-- @function InlineMath
@@ -412,7 +469,8 @@ M.DisplayMath = M.Inline:create_constructor(
412469-- @treturn Inline Math element
413470M.InlineMath = M.Inline:create_constructor(
414471 "InlineMath",
415− function(text) return M.Math("InlineMath", text) end
472+ function(text) return M.Math("InlineMath", text) end,
473+ {"mathtype", "text"}
416474)
417475
418476--- Creates a Note inline element
@@ -420,7 +478,8 @@ M.InlineMath = M.Inline:create_constructor(
420478-- @tparam {Block,...} content footnote block content
421479M.Note = M.Inline:create_constructor(
422480 "Note",
423− function(contents) return {c = contents} end
481+ function(content) return {c = content} end,
482+ "content"
424483)
425484
426485--- Creates a Quoted inline element given the quote type and quoted content. It
@@ -432,7 +491,8 @@ M.Note = M.Inline:create_constructor(
432491-- @treturn Inline quoted element
433492M.Quoted = M.Inline:create_constructor(
434493 "Quoted",
435− function(quotetype, content) return {c = {quotetype, content}} end
494+ function(quotetype, content) return {c = {quotetype, content}} end,
495+ {"quotetype", "content"}
436496)
437497--- Creates a single-quoted inline element.
438498-- @function SingleQuoted
@@ -441,7 +501,8 @@ M.Quoted = M.Inline:create_constructor(
441501-- @see Quoted
442502M.SingleQuoted = M.Inline:create_constructor(
443503 "SingleQuoted",
444− function(content) return M.Quoted(M.SingleQuote, content) end
504+ function(content) return M.Quoted(M.SingleQuote, content) end,
505+ {"quotetype", "content"}
445506)
446507--- Creates a single-quoted inline element.
447508-- @function DoubleQuoted
@@ -450,7 +511,8 @@ M.SingleQuoted = M.Inline:create_constructor(
450511-- @see Quoted
451512M.DoubleQuoted = M.Inline:create_constructor(
452513 "DoubleQuoted",
453− function(content) return M.Quoted("DoubleQuote", content) end
514+ function(content) return M.Quoted("DoubleQuote", content) end,
515+ {"quotetype", "content"}
454516)
455517
456518--- Creates a RawInline inline element
@@ -460,7 +522,8 @@ M.DoubleQuoted = M.Inline:create_constructor(
460522-- @treturn Inline raw inline element
461523M.RawInline = M.Inline:create_constructor(
462524 "RawInline",
463− function(format, text) return {c = {format, text}} end
525+ function(format, text) return {c = {format, text}} end,
526+ {"format", "text"}
464527)
465528
466529--- Creates text rendered in small caps
@@ -469,7 +532,8 @@ M.RawInline = M.Inline:create_constructor(
469532-- @treturn Inline smallcaps element
470533M.SmallCaps = M.Inline:create_constructor(
471534 "SmallCaps",
472− function(content) return {c = content} end
535+ function(content) return {c = content} end,
536+ "content"
473537)
474538
475539--- Creates a SoftBreak inline element.
@@ -495,7 +559,8 @@ M.Space = M.Inline:create_constructor(
495559-- @treturn Inline span element
496560M.Span = M.Inline:create_constructor(
497561 "Span",
498− function(content, attributes) return {c = {attributes, content}} end
562+ function(content, attributes) return {c = {attributes, content}} end,
563+ {{"identifier", "classes", "attributes"}, "content"}
499564)
500565
501566--- Creates a Str inline element
@@ -504,7 +569,8 @@ M.Span = M.Inline:create_constructor(
504569-- @treturn Inline string element
505570M.Str = M.Inline:create_constructor(
506571 "Str",
507− function(text) return {c = text} end
572+ function(text) return {c = text} end,
573+ "text"
508574)
509575
510576--- Creates text which is striked out.
@@ -513,7 +579,8 @@ M.Str = M.Inline:create_constructor(
513579-- @treturn Inline strikeout element
514580M.Strikeout = M.Inline:create_constructor(
515581 "Strikeout",
516− function(content) return {c = content} end
582+ function(content) return {c = content} end,
583+ "content"
517584)
518585
519586--- Creates a Strong element, whose text is usually displayed in a bold font.
@@ -522,7 +589,8 @@ M.Strikeout = M.Inline:create_constructor(
522589-- @treturn Inline strong element
523590M.Strong = M.Inline:create_constructor(
524591 "Strong",
525− function(content) return {c = content} end
592+ function(content) return {c = content} end,
593+ "content"
526594)
527595
528596--- Creates a Subscript inline element
@@ -531,7 +599,8 @@ M.Strong = M.Inline:create_constructor(
531599-- @treturn Inline subscript element
532600M.Subscript = M.Inline:create_constructor(
533601 "Subscript",
534− function(content) return {c = content} end
602+ function(content) return {c = content} end,
603+ "content"
535604)
536605
537606--- Creates a Superscript inline element
@@ -540,7 +609,8 @@ M.Subscript = M.Inline:create_constructor(
540609-- @treturn Inline strong element
541610M.Superscript = M.Inline:create_constructor(
542611 "Superscript",
543− function(content) return {c = content} end
612+ function(content) return {c = content} end,
613+ "content"
544614)
545615
546616