git.iko.soy/typeable/pandoc
on master help sign in
Add basic lua List module (#4099)

The List module is automatically loaded, but not assigned to a global variable. It can be included in filters by calling `List = require 'List'`. Lists of blocks, lists of inlines, and lists of classes are now given `List` as a metatable, making working with them more convenient. E.g., it is now possible to concatenate lists of inlines using Lua's concatenation operator `..` (requires at least one of the operants to have `List` as a metatable): function Emph (emph) local s = {pandoc.Space(), pandoc.Str 'emphasized'} return pandoc.Span(emph.content .. s) end Closes: #4081

commit0105a3c2930675971ea83da3b987c6b62f97ce26 authorAlbert Krewinkel <albert+github@zeitkraut.de> dateWed 29 Nov 2017, 01:20 (9 years ago) parent5a225aa
4 files changed+174−46
data/List.lua+110 data/pandoc.lua+39−41 pandoc.cabal+2 src/Text/Pandoc/Lua/PandocModule.hs+23−5
data/List.luanew file+110
@@ -0,0 +1,110 @@
1+--[[
2+List.lua
3+
4+Copyright © 2017 Albert Krewinkel
5+
6+Permission to use, copy, modify, and/or distribute this software for any purpose
7+with or without fee is hereby granted, provided that the above copyright notice
8+and this permission notice appear in all copies.
9+
10+THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
11+REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
12+FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
13+INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
14+OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
15+TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF
16+THIS SOFTWARE.
17+]]
18+
19+---
20+-- Lua functions for pandoc scripts.
21+--
22+-- @author Albert Krewinkel
23+-- @copyright © 2017 Albert Krewinkel
24+-- @license MIT
25+local M = {
26+ _VERSION = "0.1.0"
27+}
28+
29+------------------------------------------------------------------------
30+-- Metatable for lists
31+-- @type List
32+local List = {}
33+
34+function List:new (o)
35+ o = o or {}
36+ setmetatable(o, self)
37+ self.__index = self
38+ return o
39+end
40+
41+function List:__call (o)
42+ return self:new(o)
43+end
44+
45+--- Concatenates two lists.
46+function List:__concat (list)
47+ local res = List.clone(self)
48+ List.extend(res, list)
49+ return res
50+end
51+
52+--- Returns a (shallow) copy of the list.
53+function List:clone ()
54+ local lst = setmetatable({}, getmetatable(self))
55+ List.extend(lst, self)
56+ return lst
57+end
58+
59+--- Appends the given list to the end of this list.
60+function List:includes (needle)
61+ for i = 1, #self do
62+ if self[i] == needle then
63+ return true
64+ end
65+ end
66+ return false
67+end
68+
69+--- Returns the value and index of the first occurrence of the given item.
70+-- @param needle item to search for
71+-- @return first item equal to the needle, or nil if no such item exists.
72+-- @return index of that element
73+function List:find (needle, init)
74+ return List.find_if(self, function(x) return x == needle end, init)
75+end
76+
77+--- Returns the value and index of the first element for which test returns true.
78+-- @param test the test function
79+-- @param init index at which the search is started
80+-- @return first item for which `test` succeeds, or nil if no such item exists.
81+-- @return index of that element
82+function List:find_if (test, init)
83+ init = (init == nil and 1) or (init < 0 and #self - init) or init
84+ for i = init, #self do
85+ if test(self[i], i) then
86+ return self[i], i
87+ end
88+ end
89+ return nil
90+end
91+
92+--- Add the given list to the end of this list.
93+-- @param list list to appended
94+function List:extend (list)
95+ for i = 1, #list do
96+ self[#self + 1] = list[i]
97+ end
98+end
99+
100+-- Returns a copy of the current list by applying the given function to all
101+-- elements.
102+function List:map (fn)
103+ local res = setmetatable({}, getmetatable(self))
104+ for i = 1, #self do
105+ res[i] = fn(self[i])
106+ end
107+ return res
108+end
109+
110+return List
data/pandoc.lua+39−41
@@ -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)
pandoc.cabal+2
@@ -110,6 +110,8 @@ data-files:
110110 data/sample.lua
111111 -- pandoc lua module
112112 data/pandoc.lua
113+ -- lua List module
114+ data/List.lua
113115 -- sample highlighting theme
114116 data/default.theme
115117 -- bash completion template
src/Text/Pandoc/Lua/PandocModule.hs+23−5
@@ -59,10 +59,12 @@ import qualified Foreign.Lua as Lua
5959import qualified Text.Pandoc.MediaBag as MB
6060import Text.Pandoc.Lua.Filter (walkInlines, walkBlocks, LuaFilter)
6161
62−-- | Push the "pandoc" on the lua stack.
62+-- | Push the "pandoc" on the lua stack. Requires the `list` module to be
63+-- loaded.
6364pushPandocModule :: Maybe FilePath -> Lua ()
6465pushPandocModule datadir = do
65− script <- liftIO (pandocModuleScript datadir)
66+ loadListModule datadir
67+ script <- liftIO (moduleScript datadir "pandoc.lua")
6668 status <- Lua.loadstring script
6769 unless (status /= Lua.OK) $ Lua.call 0 1
6870 addFunction "_pipe" pipeFn
@@ -72,9 +74,25 @@ pushPandocModule datadir = do
7274 addFunction "walk_inline" walkInline
7375
7476-- | Get the string representation of the pandoc module
75−pandocModuleScript :: Maybe FilePath -> IO String
76−pandocModuleScript datadir = unpack <$>
77− runIOorExplode (setUserDataDir datadir >> readDataFile "pandoc.lua")
77+moduleScript :: Maybe FilePath -> FilePath -> IO String
78+moduleScript datadir moduleFile = unpack <$>
79+ runIOorExplode (setUserDataDir datadir >> readDataFile moduleFile)
80+
81+-- Loads pandoc's list module without assigning it to a variable.
82+pushListModule :: Maybe FilePath -> Lua ()
83+pushListModule datadir = do
84+ script <- liftIO (moduleScript datadir "List.lua")
85+ status <- Lua.loadstring script
86+ if status == Lua.OK
87+ then Lua.call 0 1
88+ else Lua.throwTopMessageAsError' ("Error while loading module `list`\n" ++)
89+
90+loadListModule :: Maybe FilePath -> Lua ()
91+loadListModule datadir = do
92+ Lua.getglobal' "package.loaded"
93+ pushListModule datadir
94+ Lua.setfield (-2) "List"
95+ Lua.pop 1
7896
7997walkElement :: (ToLuaStack a, Walkable [Inline] a, Walkable [Block] a)
8098 => a -> LuaFilter -> Lua NumResults