@@ -26,6 +26,8 @@ local M = {
26 _VERSION = "0.3.0"
26 _VERSION = "0.3.0"
29+local List = require 'List'
29------------------------------------------------------------------------
31------------------------------------------------------------------------
30-- The base class for pandoc's AST elements.
32-- The base class for pandoc's AST elements.
31-- @type Element
33-- @type Element
@@ -141,7 +143,7 @@ end
141function M.Pandoc(blocks, meta)
143function M.Pandoc(blocks, meta)
142 meta = meta or {}
144 meta = meta or {}
144− ["blocks"] = blocks,
146+ ["blocks"] = List:new(blocks),
145 ["meta"] = meta,
147 ["meta"] = meta,
146 ["pandoc-api-version"] = {1,17,0,5},
148 ["pandoc-api-version"] = {1,17,0,5},
@@ -250,7 +252,7 @@ M.CodeBlock = M.Block:create_constructor(
250-- @treturn Block block quote element
252-- @treturn Block block quote element
251M.DefinitionList = M.Block:create_constructor(
253M.DefinitionList = M.Block:create_constructor(
252 "DefinitionList",
254 "DefinitionList",
253− function(content) return {c = content} end,
255+ function(content) return {c = List:new(content)} end,
254 "content"
256 "content"
@@ -262,7 +264,7 @@ M.DefinitionList = M.Block:create_constructor(
262M.Div = M.Block:create_constructor(
264M.Div = M.Block:create_constructor(
264 function(content, attr)
266 function(content, attr)
265− return {c = {attr or M.Attr(), content}}
267+ return {c = {attr or M.Attr(), List:new(content)}}
267 {{"identifier", "classes", "attributes"}, "content"}
269 {{"identifier", "classes", "attributes"}, "content"}
@@ -295,7 +297,7 @@ M.HorizontalRule = M.Block:create_constructor(
295-- @treturn Block block quote element
297-- @treturn Block block quote element
296M.LineBlock = M.Block:create_constructor(
298M.LineBlock = M.Block:create_constructor(
297 "LineBlock",
299 "LineBlock",
298− function(content) return {c = content} end,
300+ function(content) return {c = List:new(content)} end,
299 "content"
301 "content"
@@ -316,7 +318,7 @@ M.OrderedList = M.Block:create_constructor(
316 "OrderedList",
318 "OrderedList",
317 function(items, listAttributes)
319 function(items, listAttributes)
318 listAttributes = listAttributes or {1, M.DefaultStyle, M.DefaultDelim}
320 listAttributes = listAttributes or {1, M.DefaultStyle, M.DefaultDelim}
319− return {c = {listAttributes, items}}
321+ return {c = {listAttributes, List:new(items)}}
321 {{"start", "style", "delimiter"}, "content"}
323 {{"start", "style", "delimiter"}, "content"}
@@ -327,7 +329,7 @@ M.OrderedList = M.Block:create_constructor(
327-- @treturn Block block quote element
329-- @treturn Block block quote element
328M.Para = M.Block:create_constructor(
330M.Para = M.Block:create_constructor(
330− function(content) return {c = content} end,
332+ function(content) return {c = List:new(content)} end,
331 "content"
333 "content"
@@ -337,7 +339,7 @@ M.Para = M.Block:create_constructor(
337-- @treturn Block block quote element
339-- @treturn Block block quote element
338M.Plain = M.Block:create_constructor(
340M.Plain = M.Block:create_constructor(
340− function(content) return {c = content} end,
342+ function(content) return {c = List:new(content)} end,
341 "content"
343 "content"
@@ -363,7 +365,15 @@ M.RawBlock = M.Block:create_constructor(
363M.Table = M.Block:create_constructor(
365M.Table = M.Block:create_constructor(
365 function(caption, aligns, widths, headers, rows)
367 function(caption, aligns, widths, headers, rows)
366− return {c = {caption, aligns, widths, headers, rows}}
368+ return {
368 {"caption", "aligns", "widths", "headers", "rows"}
378 {"caption", "aligns", "widths", "headers", "rows"}
@@ -386,7 +396,9 @@ end
386-- @treturn Inline citations element
396-- @treturn Inline citations element
387M.Cite = M.Inline:create_constructor(
397M.Cite = M.Inline:create_constructor(
389− function(content, citations) return {c = {citations, content}} end,
399+ function(content, citations)
400+ return {c = {List:new(citations), List:new(content)}}
390 {"citations", "content"}
402 {"citations", "content"}
@@ -407,7 +419,7 @@ M.Code = M.Inline:create_constructor(
407-- @treturn Inline emphasis element
419-- @treturn Inline emphasis element
408M.Emph = M.Inline:create_constructor(
420M.Emph = M.Inline:create_constructor(
410− function(content) return {c = content} end,
422+ function(content) return {c = List:new(content)} end,
411 "content"
423 "content"
@@ -423,7 +435,7 @@ M.Image = M.Inline:create_constructor(
423 function(caption, src, title, attr)
435 function(caption, src, title, attr)
424 title = title or ""
436 title = title or ""
425 attr = attr or M.Attr()
437 attr = attr or M.Attr()
426− return {c = {attr, caption, {src, title}}}
438+ return {c = {attr, List:new(caption), {src, title}}}
428 {{"identifier", "classes", "attributes"}, "caption", {"src", "title"}}
440 {{"identifier", "classes", "attributes"}, "caption", {"src", "title"}}
@@ -448,7 +460,7 @@ M.Link = M.Inline:create_constructor(
448 function(content, target, title, attr)
460 function(content, target, title, attr)
449 title = title or ""
461 title = title or ""
450 attr = attr or M.Attr()
462 attr = attr or M.Attr()
451− return {c = {attr, content, {target, title}}}
463+ return {c = {attr, List:new(content), {target, title}}}
453 {{"identifier", "classes", "attributes"}, "content", {"target", "title"}}
465 {{"identifier", "classes", "attributes"}, "content", {"target", "title"}}
@@ -489,7 +501,7 @@ M.InlineMath = M.Inline:create_constructor(
489-- @tparam {Block,...} content footnote block content
501-- @tparam {Block,...} content footnote block content
490M.Note = M.Inline:create_constructor(
502M.Note = M.Inline:create_constructor(
492− function(content) return {c = content} end,
504+ function(content) return {c = List:new(content)} end,
493 "content"
505 "content"
@@ -500,7 +512,7 @@ M.Note = M.Inline:create_constructor(
500-- @treturn Inline quoted element
512-- @treturn Inline quoted element
501M.Quoted = M.Inline:create_constructor(
513M.Quoted = M.Inline:create_constructor(
502 "Quoted",
514 "Quoted",
503− function(quotetype, content) return {c = {quotetype, content}} end,
515+ function(quotetype, content) return {c = {quotetype, List:new(content)}} end,
504 {"quotetype", "content"}
516 {"quotetype", "content"}
506--- Creates a single-quoted inline element (DEPRECATED).
518--- Creates a single-quoted inline element (DEPRECATED).
@@ -541,7 +553,7 @@ M.RawInline = M.Inline:create_constructor(
541-- @treturn Inline smallcaps element
553-- @treturn Inline smallcaps element
542M.SmallCaps = M.Inline:create_constructor(
554M.SmallCaps = M.Inline:create_constructor(
543 "SmallCaps",
555 "SmallCaps",
544− function(content) return {c = content} end,
556+ function(content) return {c = List:new(content)} end,
545 "content"
557 "content"
@@ -568,7 +580,9 @@ M.Space = M.Inline:create_constructor(
568-- @treturn Inline span element
580-- @treturn Inline span element
569M.Span = M.Inline:create_constructor(
581M.Span = M.Inline:create_constructor(
583+ function(content, attr)
571− function(content, attr) return {c = {attr or M.Attr(), content}} end,
584+ return {c = {attr or M.Attr(), List:new(content)}}
572 {{"identifier", "classes", "attributes"}, "content"}
586 {{"identifier", "classes", "attributes"}, "content"}
@@ -588,7 +602,7 @@ M.Str = M.Inline:create_constructor(
588-- @treturn Inline strikeout element
602-- @treturn Inline strikeout element
589M.Strikeout = M.Inline:create_constructor(
603M.Strikeout = M.Inline:create_constructor(
590 "Strikeout",
604 "Strikeout",
591− function(content) return {c = content} end,
605+ function(content) return {c = List:new(content)} end,
592 "content"
606 "content"
@@ -598,7 +612,7 @@ M.Strikeout = M.Inline:create_constructor(
598-- @treturn Inline strong element
612-- @treturn Inline strong element
599M.Strong = M.Inline:create_constructor(
613M.Strong = M.Inline:create_constructor(
600 "Strong",
614 "Strong",
601− function(content) return {c = content} end,
615+ function(content) return {c = List:new(content)} end,
602 "content"
616 "content"
@@ -608,7 +622,7 @@ M.Strong = M.Inline:create_constructor(
608-- @treturn Inline subscript element
622-- @treturn Inline subscript element
609M.Subscript = M.Inline:create_constructor(
623M.Subscript = M.Inline:create_constructor(
610 "Subscript",
624 "Subscript",
611− function(content) return {c = content} end,
625+ function(content) return {c = List:new(content)} end,
612 "content"
626 "content"
@@ -618,7 +632,7 @@ M.Subscript = M.Inline:create_constructor(
618-- @treturn Inline strong element
632-- @treturn Inline strong element
619M.Superscript = M.Inline:create_constructor(
633M.Superscript = M.Inline:create_constructor(
620 "Superscript",
634 "Superscript",
621− function(content) return {c = content} end,
635+ function(content) return {c = List:new(content)} end,
622 "content"
636 "content"
@@ -627,24 +641,8 @@ M.Superscript = M.Inline:create_constructor(
627-- Helpers
641-- Helpers
628-- @section helpers
642-- @section helpers
630−-- Find a value pair in a list.
632−-- @tparam table list to be searched
633−-- @param needle element to search for
634−-- @param[opt] key when non-nil, compare on this field of each list element
635−local function find (alist, needle, key)
644+local function assoc_key_equals (x)
636− local test
645+ return function (y) return y[1] == x end
638− test = function(x) return x[key] == needle end
640− test = function(x) return x == needle end
642− for i, k in ipairs(alist) do
650-- Lookup a value in an associative list
648-- Lookup a value in an associative list
@@ -652,7 +650,7 @@ end
652-- @tparam {{key, value},...} alist associative list
650-- @tparam {{key, value},...} alist associative list
653-- @param key key for which the associated value is to be looked up
651-- @param key key for which the associated value is to be looked up
654local function lookup(alist, key)
652local function lookup(alist, key)
655− return (select(2, find(alist, key, 1)) or {})[2]
653+ return (List.find_if(alist, assoc_key_equals(key)) or {})[2]
658--- Return an iterator which returns key-value pairs of an associative list.
656--- Return an iterator which returns key-value pairs of an associative list.
@@ -684,7 +682,7 @@ local AttributeList = {
686 __newindex = function (t, k, v)
684 __newindex = function (t, k, v)
687− local idx, cur = find(t, k, 1)
685+ local cur, idx = List.find_if(t, assoc_key_equals(k))
688 if v == nil then
686 if v == nil then
689 table.remove(t, idx)
687 table.remove(t, idx)
690 elseif cur then
688 elseif cur then
@@ -729,7 +727,7 @@ M.Attr._field_names = {identifier = 1, classes = 2, attributes = 3}
729-- @return element attributes
727-- @return element attributes
730M.Attr.__call = function(t, identifier, classes, attributes)
728M.Attr.__call = function(t, identifier, classes, attributes)
731 identifier = identifier or ''
729 identifier = identifier or ''
732− classes = classes or {}
730+ classes = List:new(classes or {})
733 attributes = setmetatable(to_alist(attributes or {}), AttributeList)
731 attributes = setmetatable(to_alist(attributes or {}), AttributeList)
734 local attr = {identifier, classes, attributes}
732 local attr = {identifier, classes, attributes}
735 setmetatable(attr, t)
733 setmetatable(attr, t)