@@ -26,6 +26,8 @@ local M = {
2626 _VERSION = "0.3.0"
2727}
2828
29+local List = require 'List'
30+
2931------------------------------------------------------------------------
3032-- The base class for pandoc's AST elements.
3133-- @type Element
@@ -141,7 +143,7 @@ end
141143function M.Pandoc(blocks, meta)
142144 meta = meta or {}
143145 return {
144− ["blocks"] = blocks,
146+ ["blocks"] = List:new(blocks),
145147 ["meta"] = meta,
146148 ["pandoc-api-version"] = {1,17,0,5},
147149 }
@@ -250,7 +252,7 @@ M.CodeBlock = M.Block:create_constructor(
250252-- @treturn Block block quote element
251253M.DefinitionList = M.Block:create_constructor(
252254 "DefinitionList",
253− function(content) return {c = content} end,
255+ function(content) return {c = List:new(content)} end,
254256 "content"
255257)
256258
@@ -262,7 +264,7 @@ M.DefinitionList = M.Block:create_constructor(
262264M.Div = M.Block:create_constructor(
263265 "Div",
264266 function(content, attr)
265− return {c = {attr or M.Attr(), content}}
267+ return {c = {attr or M.Attr(), List:new(content)}}
266268 end,
267269 {{"identifier", "classes", "attributes"}, "content"}
268270)
@@ -295,7 +297,7 @@ M.HorizontalRule = M.Block:create_constructor(
295297-- @treturn Block block quote element
296298M.LineBlock = M.Block:create_constructor(
297299 "LineBlock",
298− function(content) return {c = content} end,
300+ function(content) return {c = List:new(content)} end,
299301 "content"
300302)
301303
@@ -316,7 +318,7 @@ M.OrderedList = M.Block:create_constructor(
316318 "OrderedList",
317319 function(items, listAttributes)
318320 listAttributes = listAttributes or {1, M.DefaultStyle, M.DefaultDelim}
319− return {c = {listAttributes, items}}
321+ return {c = {listAttributes, List:new(items)}}
320322 end,
321323 {{"start", "style", "delimiter"}, "content"}
322324)
@@ -327,7 +329,7 @@ M.OrderedList = M.Block:create_constructor(
327329-- @treturn Block block quote element
328330M.Para = M.Block:create_constructor(
329331 "Para",
330− function(content) return {c = content} end,
332+ function(content) return {c = List:new(content)} end,
331333 "content"
332334)
333335
@@ -337,7 +339,7 @@ M.Para = M.Block:create_constructor(
337339-- @treturn Block block quote element
338340M.Plain = M.Block:create_constructor(
339341 "Plain",
340− function(content) return {c = content} end,
342+ function(content) return {c = List:new(content)} end,
341343 "content"
342344)
343345
@@ -363,7 +365,15 @@ M.RawBlock = M.Block:create_constructor(
363365M.Table = M.Block:create_constructor(
364366 "Table",
365367 function(caption, aligns, widths, headers, rows)
366− return {c = {caption, aligns, widths, headers, rows}}
368+ return {
369+ c = {
370+ List:new(caption),
371+ List:new(aligns),
372+ List:new(widths),
373+ List:new(headers),
374+ List:new(rows)
375+ }
376+ }
367377 end,
368378 {"caption", "aligns", "widths", "headers", "rows"}
369379)
@@ -386,7 +396,9 @@ end
386396-- @treturn Inline citations element
387397M.Cite = M.Inline:create_constructor(
388398 "Cite",
389− function(content, citations) return {c = {citations, content}} end,
399+ function(content, citations)
400+ return {c = {List:new(citations), List:new(content)}}
401+ end,
390402 {"citations", "content"}
391403)
392404
@@ -407,7 +419,7 @@ M.Code = M.Inline:create_constructor(
407419-- @treturn Inline emphasis element
408420M.Emph = M.Inline:create_constructor(
409421 "Emph",
410− function(content) return {c = content} end,
422+ function(content) return {c = List:new(content)} end,
411423 "content"
412424)
413425
@@ -423,7 +435,7 @@ M.Image = M.Inline:create_constructor(
423435 function(caption, src, title, attr)
424436 title = title or ""
425437 attr = attr or M.Attr()
426− return {c = {attr, caption, {src, title}}}
438+ return {c = {attr, List:new(caption), {src, title}}}
427439 end,
428440 {{"identifier", "classes", "attributes"}, "caption", {"src", "title"}}
429441)
@@ -448,7 +460,7 @@ M.Link = M.Inline:create_constructor(
448460 function(content, target, title, attr)
449461 title = title or ""
450462 attr = attr or M.Attr()
451− return {c = {attr, content, {target, title}}}
463+ return {c = {attr, List:new(content), {target, title}}}
452464 end,
453465 {{"identifier", "classes", "attributes"}, "content", {"target", "title"}}
454466)
@@ -489,7 +501,7 @@ M.InlineMath = M.Inline:create_constructor(
489501-- @tparam {Block,...} content footnote block content
490502M.Note = M.Inline:create_constructor(
491503 "Note",
492− function(content) return {c = content} end,
504+ function(content) return {c = List:new(content)} end,
493505 "content"
494506)
495507
@@ -500,7 +512,7 @@ M.Note = M.Inline:create_constructor(
500512-- @treturn Inline quoted element
501513M.Quoted = M.Inline:create_constructor(
502514 "Quoted",
503− function(quotetype, content) return {c = {quotetype, content}} end,
515+ function(quotetype, content) return {c = {quotetype, List:new(content)}} end,
504516 {"quotetype", "content"}
505517)
506518--- Creates a single-quoted inline element (DEPRECATED).
@@ -541,7 +553,7 @@ M.RawInline = M.Inline:create_constructor(
541553-- @treturn Inline smallcaps element
542554M.SmallCaps = M.Inline:create_constructor(
543555 "SmallCaps",
544− function(content) return {c = content} end,
556+ function(content) return {c = List:new(content)} end,
545557 "content"
546558)
547559
@@ -568,7 +580,9 @@ M.Space = M.Inline:create_constructor(
568580-- @treturn Inline span element
569581M.Span = M.Inline:create_constructor(
570582 "Span",
571− function(content, attr) return {c = {attr or M.Attr(), content}} end,
583+ function(content, attr)
584+ return {c = {attr or M.Attr(), List:new(content)}}
585+ end,
572586 {{"identifier", "classes", "attributes"}, "content"}
573587)
574588
@@ -588,7 +602,7 @@ M.Str = M.Inline:create_constructor(
588602-- @treturn Inline strikeout element
589603M.Strikeout = M.Inline:create_constructor(
590604 "Strikeout",
591− function(content) return {c = content} end,
605+ function(content) return {c = List:new(content)} end,
592606 "content"
593607)
594608
@@ -598,7 +612,7 @@ M.Strikeout = M.Inline:create_constructor(
598612-- @treturn Inline strong element
599613M.Strong = M.Inline:create_constructor(
600614 "Strong",
601− function(content) return {c = content} end,
615+ function(content) return {c = List:new(content)} end,
602616 "content"
603617)
604618
@@ -608,7 +622,7 @@ M.Strong = M.Inline:create_constructor(
608622-- @treturn Inline subscript element
609623M.Subscript = M.Inline:create_constructor(
610624 "Subscript",
611− function(content) return {c = content} end,
625+ function(content) return {c = List:new(content)} end,
612626 "content"
613627)
614628
@@ -618,7 +632,7 @@ M.Subscript = M.Inline:create_constructor(
618632-- @treturn Inline strong element
619633M.Superscript = M.Inline:create_constructor(
620634 "Superscript",
621− function(content) return {c = content} end,
635+ function(content) return {c = List:new(content)} end,
622636 "content"
623637)
624638
@@ -627,24 +641,8 @@ M.Superscript = M.Inline:create_constructor(
627641-- Helpers
628642-- @section helpers
629643
630−-- Find a value pair in a list.
631−-- @function find
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)
636− local test
637− if key then
638− test = function(x) return x[key] == needle end
639− else
640− test = function(x) return x == needle end
641− end
642− for i, k in ipairs(alist) do
643− if test(k) then
644− return i, k
645− end
646− end
647− return nil
644+local function assoc_key_equals (x)
645+ return function (y) return y[1] == x end
648646end
649647
650648-- Lookup a value in an associative list
@@ -652,7 +650,7 @@ end
652650-- @tparam {{key, value},...} alist associative list
653651-- @param key key for which the associated value is to be looked up
654652local 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]
656654end
657655
658656--- Return an iterator which returns key-value pairs of an associative list.
@@ -684,7 +682,7 @@ local AttributeList = {
684682 end,
685683
686684 __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))
688686 if v == nil then
689687 table.remove(t, idx)
690688 elseif cur then
@@ -729,7 +727,7 @@ M.Attr._field_names = {identifier = 1, classes = 2, attributes = 3}
729727-- @return element attributes
730728M.Attr.__call = function(t, identifier, classes, attributes)
731729 identifier = identifier or ''
732− classes = classes or {}
730+ classes = List:new(classes or {})
733731 attributes = setmetatable(to_alist(attributes or {}), AttributeList)
734732 local attr = {identifier, classes, attributes}
735733 setmetatable(attr, t)