git.iko.soy/typeable/pandoc
on master help sign in
data/pandoc.lua: accept single block as singleton list

Every constructor which accepts a list of blocks now also accepts a single block element for convenience. Furthermore, strings are accepted as shorthand for `{pandoc.Str "text"}` in constructors.

commite0cb0dab1821e93e50e8b8006ef569bdeee9a248 authorAlbert Krewinkel <albert@zeitkraut.de> dateSat 13 Jan 2018, 22:29 (9 years ago) parente5e69e6
2 files changed+42−23
data/pandoc.lua+28−19 test/Tests/Lua.hs+14−4
data/pandoc.lua+28−19
@@ -186,15 +186,24 @@ function AstElement:create_constructor(tag, fn, accessors)
186186 return constr
187187end
188188
189+--- Convert AstElement input into a list if necessary.
190+-- @local
191+local function ensureList (x)
192+ if x.tag then
193+ -- Lists are not tagged, but all elements are
194+ return List:new{x}
195+ else
196+ return List:new(x)
197+ end
198+end
199+
189200--- Ensure a given object is an Inline element, or convert it into one.
201+-- @local
190202local function ensureInlineList (x)
191− if type(x) == string then
203+ if type(x) == 'string' then
192204 return List:new{M.Str(x)}
193− elseif x.tag then
194− -- Lists are not tagged, but all elements are
195− return List:new{x}
196205 else
197− return x
206+ return ensureList(x)
198207 end
199208end
200209
@@ -209,7 +218,7 @@ end
209218M.Pandoc = AstElement:make_subtype'Pandoc'
210219function M.Pandoc:new (blocks, meta)
211220 return {
212− blocks = List:new(blocks),
221+ blocks = ensureList(blocks),
213222 meta = meta or {},
214223 }
215224end
@@ -239,7 +248,7 @@ M.MetaValue = AstElement:make_subtype('MetaValue')
239248-- @tparam {Block,...} blocks blocks
240249M.MetaBlocks = M.MetaValue:create_constructor(
241250 'MetaBlocks',
242− function (content) return List:new(content) end
251+ function (content) return ensureList(content) end
243252)
244253
245254--- Meta inlines
@@ -255,7 +264,7 @@ M.MetaInlines = M.MetaValue:create_constructor(
255264-- @tparam {MetaValue,...} meta_values list of meta values
256265M.MetaList = M.MetaValue:create_constructor(
257266 'MetaList',
258− function (content) return List:new(content) end
267+ function (content) return ensureList(content) end
259268)
260269
261270--- Meta map
@@ -295,7 +304,7 @@ M.Block = AstElement:make_subtype'Block'
295304-- @treturn Block block quote element
296305M.BlockQuote = M.Block:create_constructor(
297306 "BlockQuote",
298− function(content) return {c = content} end,
307+ function(content) return {c = ensureList(content)} end,
299308 "content"
300309)
301310
@@ -305,7 +314,7 @@ M.BlockQuote = M.Block:create_constructor(
305314-- @treturn Block bullet list element
306315M.BulletList = M.Block:create_constructor(
307316 "BulletList",
308− function(content) return {c = content} end,
317+ function(content) return {c = ensureList(content)} end,
309318 "content"
310319)
311320
@@ -326,7 +335,7 @@ M.CodeBlock = M.Block:create_constructor(
326335-- @treturn Block definition list element
327336M.DefinitionList = M.Block:create_constructor(
328337 "DefinitionList",
329− function(content) return {c = List:new(content)} end,
338+ function(content) return {c = ensureList(content)} end,
330339 "content"
331340)
332341
@@ -338,7 +347,7 @@ M.DefinitionList = M.Block:create_constructor(
338347M.Div = M.Block:create_constructor(
339348 "Div",
340349 function(content, attr)
341− return {c = {attr or M.Attr(), List:new(content)}}
350+ return {c = {attr or M.Attr(), ensureList(content)}}
342351 end,
343352 {{"identifier", "classes", "attributes"}, "content"}
344353)
@@ -371,7 +380,7 @@ M.HorizontalRule = M.Block:create_constructor(
371380-- @treturn Block line block element
372381M.LineBlock = M.Block:create_constructor(
373382 "LineBlock",
374− function(content) return {c = List:new(content)} end,
383+ function(content) return {c = ensureList(content)} end,
375384 "content"
376385)
377386
@@ -392,7 +401,7 @@ M.OrderedList = M.Block:create_constructor(
392401 "OrderedList",
393402 function(items, listAttributes)
394403 listAttributes = listAttributes or {1, M.DefaultStyle, M.DefaultDelim}
395− return {c = {listAttributes, List:new(items)}}
404+ return {c = {listAttributes, ensureList(items)}}
396405 end,
397406 {{"start", "style", "delimiter"}, "content"}
398407)
@@ -468,7 +477,7 @@ M.Inline = AstElement:make_subtype'Inline'
468477M.Cite = M.Inline:create_constructor(
469478 "Cite",
470479 function(content, citations)
471− return {c = {List:new(citations), ensureInlineList(content)}}
480+ return {c = {ensureList(citations), ensureInlineList(content)}}
472481 end,
473482 {"citations", "content"}
474483)
@@ -572,7 +581,7 @@ M.InlineMath = M.Inline:create_constructor(
572581-- @tparam {Block,...} content footnote block content
573582M.Note = M.Inline:create_constructor(
574583 "Note",
575− function(content) return {c = List:new(content)} end,
584+ function(content) return {c = ensureList(content)} end,
576585 "content"
577586)
578587
@@ -806,7 +815,7 @@ end
806815M.Attr = AstElement:make_subtype'Attr'
807816function M.Attr:new (identifier, classes, attributes)
808817 identifier = identifier or ''
809− classes = List:new(classes or {})
818+ classes = ensureList(classes or {})
810819 attributes = setmetatable(to_alist(attributes or {}), AttributeList)
811820 return {identifier, classes, attributes}
812821end
@@ -838,8 +847,8 @@ function M.Citation:new (id, mode, prefix, suffix, note_num, hash)
838847 return {
839848 id = id,
840849 mode = mode,
841− prefix = prefix or {},
842− suffix = suffix or {},
850+ prefix = ensureList(prefix or {}),
851+ suffix = ensureList(suffix or {}),
843852 note_num = note_num or 0,
844853 hash = hash or 0,
845854 }
test/Tests/Lua.hs+14−4
@@ -12,8 +12,8 @@ import Text.Pandoc.Builder (bulletList, divWith, doc, doubleQuoted, emph,
1212 header, linebreak, para, plain, rawBlock,
1313 singleQuoted, space, str, strong, (<>))
1414import Text.Pandoc.Class (runIOorExplode, setUserDataDir)
15−import Text.Pandoc.Definition (Block, Inline (Emph, Str), Meta, Pandoc,
16− pandocTypesVersion)
15+import Text.Pandoc.Definition (Block (BlockQuote, Para), Inline (Emph, Str),
16+ Meta, Pandoc, pandocTypesVersion)
1717import Text.Pandoc.Lua (runLuaFilter, runPandocLua)
1818import Text.Pandoc.Options (def)
1919import Text.Pandoc.Shared (pandocVersion)
@@ -126,8 +126,18 @@ tests = map (localOption (QuickCheckTests 20))
126126 =<< Lua.peek Lua.stackTop
127127
128128 , testCase "Allow singleton inline in constructors" . runPandocLua' $ do
129− res <- Lua.callFunc "pandoc.Emph" (Str "test")
130− Lua.liftIO $ assertEqual "Not the exptected Emph" (Emph [Str "test"]) res
129+ Lua.liftIO . assertEqual "Not the exptected Emph" (Emph [Str "test"])
130+ =<< Lua.callFunc "pandoc.Emph" (Str "test")
131+ Lua.liftIO . assertEqual "Unexpected element" (Para [Str "test"])
132+ =<< Lua.callFunc "pandoc.Para" ("test" :: String)
133+ Lua.liftIO . assertEqual "Unexptected element"
134+ (BlockQuote [Para [Str "foo"]]) =<< (
135+ do
136+ Lua.getglobal' "pandoc.BlockQuote"
137+ Lua.push (Para [Str "foo"])
138+ _ <- Lua.call 1 1
139+ Lua.peek Lua.stackTop
140+ )
131141
132142 , testCase "informative error messages" . runPandocLua' $ do
133143 Lua.pushboolean True