diff options
Diffstat (limited to 'data/pandoc.lua')
| -rw-r--r-- | data/pandoc.lua | 105 |
1 files changed, 64 insertions, 41 deletions
diff --git a/data/pandoc.lua b/data/pandoc.lua index b3cee4670..512b2919c 100644 --- a/data/pandoc.lua +++ b/data/pandoc.lua @@ -22,9 +22,7 @@ THIS SOFTWARE. -- @author Albert Krewinkel -- @copyright © 2017–2018 Albert Krewinkel -- @license MIT -local M = { - _VERSION = "0.4.0" -} +local M = {} local List = require 'pandoc.List' @@ -59,8 +57,12 @@ end local function create_accessor_functions (fn_template, accessors) local res = {} function add_accessors(acc, ...) - if type(acc) == "string" then + if type(acc) == 'string' then res[acc] = make_indexing_function(fn_template, {...}) + elseif type(acc) == 'table' and #acc == 0 and next(acc) then + local name, substructure = next(acc) + res[name] = make_indexing_function(fn_template, {...}) + add_accessors(substructure, ...) else for i = 1, #(acc or {}) do add_accessors(acc[i], i, ...) @@ -187,6 +189,27 @@ function AstElement:create_constructor(tag, fn, accessors) return constr end +--- Convert AstElement input into a list if necessary. +-- @local +local function ensureList (x) + if x.tag then + -- Lists are not tagged, but all elements are + return List:new{x} + else + return List:new(x) + end +end + +--- Ensure a given object is an Inline element, or convert it into one. +-- @local +local function ensureInlineList (x) + if type(x) == 'string' then + return List:new{M.Str(x)} + else + return ensureList(x) + end +end + ------------------------------------------------------------------------ --- Pandoc Document -- @section document @@ -198,7 +221,7 @@ end M.Pandoc = AstElement:make_subtype'Pandoc' function M.Pandoc:new (blocks, meta) return { - blocks = List:new(blocks), + blocks = ensureList(blocks), meta = meta or {}, } end @@ -228,7 +251,7 @@ M.MetaValue = AstElement:make_subtype('MetaValue') -- @tparam {Block,...} blocks blocks M.MetaBlocks = M.MetaValue:create_constructor( 'MetaBlocks', - function (content) return List:new(content) end + function (content) return ensureList(content) end ) --- Meta inlines @@ -236,7 +259,7 @@ M.MetaBlocks = M.MetaValue:create_constructor( -- @tparam {Inline,...} inlines inlines M.MetaInlines = M.MetaValue:create_constructor( 'MetaInlines', - function (content) return List:new(content) end + function (content) return ensureInlineList(content) end ) --- Meta list @@ -244,7 +267,7 @@ M.MetaInlines = M.MetaValue:create_constructor( -- @tparam {MetaValue,...} meta_values list of meta values M.MetaList = M.MetaValue:create_constructor( 'MetaList', - function (content) return List:new(content) end + function (content) return ensureList(content) end ) --- Meta map @@ -284,7 +307,7 @@ M.Block = AstElement:make_subtype'Block' -- @treturn Block block quote element M.BlockQuote = M.Block:create_constructor( "BlockQuote", - function(content) return {c = content} end, + function(content) return {c = ensureList(content)} end, "content" ) @@ -294,7 +317,7 @@ M.BlockQuote = M.Block:create_constructor( -- @treturn Block bullet list element M.BulletList = M.Block:create_constructor( "BulletList", - function(content) return {c = content} end, + function(content) return {c = ensureList(content)} end, "content" ) @@ -306,7 +329,7 @@ M.BulletList = M.Block:create_constructor( M.CodeBlock = M.Block:create_constructor( "CodeBlock", function(text, attr) return {c = {attr or M.Attr(), text}} end, - {{"identifier", "classes", "attributes"}, "text"} + {{attr = {"identifier", "classes", "attributes"}}, "text"} ) --- Creates a definition list, containing terms and their explanation. @@ -315,7 +338,7 @@ M.CodeBlock = M.Block:create_constructor( -- @treturn Block definition list element M.DefinitionList = M.Block:create_constructor( "DefinitionList", - function(content) return {c = List:new(content)} end, + function(content) return {c = ensureList(content)} end, "content" ) @@ -327,9 +350,9 @@ M.DefinitionList = M.Block:create_constructor( M.Div = M.Block:create_constructor( "Div", function(content, attr) - return {c = {attr or M.Attr(), List:new(content)}} + return {c = {attr or M.Attr(), ensureList(content)}} end, - {{"identifier", "classes", "attributes"}, "content"} + {{attr = {"identifier", "classes", "attributes"}}, "content"} ) --- Creates a header element. @@ -341,9 +364,9 @@ M.Div = M.Block:create_constructor( M.Header = M.Block:create_constructor( "Header", function(level, content, attr) - return {c = {level, attr or M.Attr(), content}} + return {c = {level, attr or M.Attr(), ensureInlineList(content)}} end, - {"level", {"identifier", "classes", "attributes"}, "content"} + {"level", {attr = {"identifier", "classes", "attributes"}}, "content"} ) --- Creates a horizontal rule. @@ -360,7 +383,7 @@ M.HorizontalRule = M.Block:create_constructor( -- @treturn Block line block element M.LineBlock = M.Block:create_constructor( "LineBlock", - function(content) return {c = List:new(content)} end, + function(content) return {c = ensureList(content)} end, "content" ) @@ -381,9 +404,9 @@ M.OrderedList = M.Block:create_constructor( "OrderedList", function(items, listAttributes) listAttributes = listAttributes or {1, M.DefaultStyle, M.DefaultDelim} - return {c = {listAttributes, List:new(items)}} + return {c = {listAttributes, ensureList(items)}} end, - {{"start", "style", "delimiter"}, "content"} + {{listAttributes = {"start", "style", "delimiter"}}, "content"} ) --- Creates a para element. @@ -392,7 +415,7 @@ M.OrderedList = M.Block:create_constructor( -- @treturn Block paragraph element M.Para = M.Block:create_constructor( "Para", - function(content) return {c = List:new(content)} end, + function(content) return {c = ensureInlineList(content)} end, "content" ) @@ -402,7 +425,7 @@ M.Para = M.Block:create_constructor( -- @treturn Block plain element M.Plain = M.Block:create_constructor( "Plain", - function(content) return {c = List:new(content)} end, + function(content) return {c = ensureInlineList(content)} end, "content" ) @@ -430,7 +453,7 @@ M.Table = M.Block:create_constructor( function(caption, aligns, widths, headers, rows) return { c = { - List:new(caption), + ensureInlineList(caption), List:new(aligns), List:new(widths), List:new(headers), @@ -457,7 +480,7 @@ M.Inline = AstElement:make_subtype'Inline' M.Cite = M.Inline:create_constructor( "Cite", function(content, citations) - return {c = {List:new(citations), List:new(content)}} + return {c = {ensureList(citations), ensureInlineList(content)}} end, {"citations", "content"} ) @@ -470,7 +493,7 @@ M.Cite = M.Inline:create_constructor( M.Code = M.Inline:create_constructor( "Code", function(text, attr) return {c = {attr or M.Attr(), text}} end, - {{"identifier", "classes", "attributes"}, "text"} + {{attr = {"identifier", "classes", "attributes"}}, "text"} ) --- Creates an inline element representing emphasised text. @@ -479,7 +502,7 @@ M.Code = M.Inline:create_constructor( -- @treturn Inline emphasis element M.Emph = M.Inline:create_constructor( "Emph", - function(content) return {c = List:new(content)} end, + function(content) return {c = ensureInlineList(content)} end, "content" ) @@ -495,9 +518,9 @@ M.Image = M.Inline:create_constructor( function(caption, src, title, attr) title = title or "" attr = attr or M.Attr() - return {c = {attr, List:new(caption), {src, title}}} + return {c = {attr, ensureInlineList(caption), {src, title}}} end, - {{"identifier", "classes", "attributes"}, "caption", {"src", "title"}} + {{attr = {"identifier", "classes", "attributes"}}, "caption", {"src", "title"}} ) --- Create a LineBreak inline element @@ -520,9 +543,9 @@ M.Link = M.Inline:create_constructor( function(content, target, title, attr) title = title or "" attr = attr or M.Attr() - return {c = {attr, List:new(content), {target, title}}} + return {c = {attr, ensureInlineList(content), {target, title}}} end, - {{"identifier", "classes", "attributes"}, "content", {"target", "title"}} + {{attr = {"identifier", "classes", "attributes"}}, "content", {"target", "title"}} ) --- Creates a Math element, either inline or displayed. @@ -561,7 +584,7 @@ M.InlineMath = M.Inline:create_constructor( -- @tparam {Block,...} content footnote block content M.Note = M.Inline:create_constructor( "Note", - function(content) return {c = List:new(content)} end, + function(content) return {c = ensureList(content)} end, "content" ) @@ -572,7 +595,7 @@ M.Note = M.Inline:create_constructor( -- @treturn Inline quoted element M.Quoted = M.Inline:create_constructor( "Quoted", - function(quotetype, content) return {c = {quotetype, List:new(content)}} end, + function(quotetype, content) return {c = {quotetype, ensureInlineList(content)}} end, {"quotetype", "content"} ) --- Creates a single-quoted inline element (DEPRECATED). @@ -613,7 +636,7 @@ M.RawInline = M.Inline:create_constructor( -- @treturn Inline smallcaps element M.SmallCaps = M.Inline:create_constructor( "SmallCaps", - function(content) return {c = List:new(content)} end, + function(content) return {c = ensureInlineList(content)} end, "content" ) @@ -641,9 +664,9 @@ M.Space = M.Inline:create_constructor( M.Span = M.Inline:create_constructor( "Span", function(content, attr) - return {c = {attr or M.Attr(), List:new(content)}} + return {c = {attr or M.Attr(), ensureInlineList(content)}} end, - {{"identifier", "classes", "attributes"}, "content"} + {{attr = {"identifier", "classes", "attributes"}}, "content"} ) --- Creates a Str inline element @@ -662,7 +685,7 @@ M.Str = M.Inline:create_constructor( -- @treturn Inline strikeout element M.Strikeout = M.Inline:create_constructor( "Strikeout", - function(content) return {c = List:new(content)} end, + function(content) return {c = ensureInlineList(content)} end, "content" ) @@ -672,7 +695,7 @@ M.Strikeout = M.Inline:create_constructor( -- @treturn Inline strong element M.Strong = M.Inline:create_constructor( "Strong", - function(content) return {c = List:new(content)} end, + function(content) return {c = ensureInlineList(content)} end, "content" ) @@ -682,7 +705,7 @@ M.Strong = M.Inline:create_constructor( -- @treturn Inline subscript element M.Subscript = M.Inline:create_constructor( "Subscript", - function(content) return {c = List:new(content)} end, + function(content) return {c = ensureInlineList(content)} end, "content" ) @@ -692,7 +715,7 @@ M.Subscript = M.Inline:create_constructor( -- @treturn Inline strong element M.Superscript = M.Inline:create_constructor( "Superscript", - function(content) return {c = List:new(content)} end, + function(content) return {c = ensureInlineList(content)} end, "content" ) @@ -795,7 +818,7 @@ end M.Attr = AstElement:make_subtype'Attr' function M.Attr:new (identifier, classes, attributes) identifier = identifier or '' - classes = List:new(classes or {}) + classes = ensureList(classes or {}) attributes = setmetatable(to_alist(attributes or {}), AttributeList) return {identifier, classes, attributes} end @@ -827,8 +850,8 @@ function M.Citation:new (id, mode, prefix, suffix, note_num, hash) return { id = id, mode = mode, - prefix = prefix or {}, - suffix = suffix or {}, + prefix = ensureList(prefix or {}), + suffix = ensureList(suffix or {}), note_num = note_num or 0, hash = hash or 0, } |
