aboutsummaryrefslogtreecommitdiff
path: root/test/lua
diff options
context:
space:
mode:
authorAlbert Krewinkel <albert@zeitkraut.de>2020-01-15 23:26:00 +0100
committerJohn MacFarlane <jgm@berkeley.edu>2020-01-15 14:26:00 -0800
commit672a4bdd1d4a587feaa38613fce64335adaad76d (patch)
tree17e58c93a24eedba8fc06aa28f661395e1057096 /test/lua
parent400b29d10e9ba20479692ff2e2a482bb27bfe09f (diff)
downloadpandoc-672a4bdd1d4a587feaa38613fce64335adaad76d.tar.gz
Lua filters: allow filtering of element lists (#6040)
Lists of Inline and Block elements can now be filtered via `Inlines` and `Blocks` functions, respectively. This is helpful if a filter conversion depends on the order of elements rather than a single element. For example, the following filter can be used to remove all spaces before a citation: function isSpaceBeforeCite (spc, cite) return spc and spc.t == 'Space' and cite and cite.t == 'Cite' end function Inlines (inlines) for i = #inlines-1,1,-1 do if isSpaceBeforeCite(inlines[i], inlines[i+1]) then inlines:remove(i) end end return inlines end Closes: #6038
Diffstat (limited to 'test/lua')
-rw-r--r--test/lua/blocks-filter.lua8
-rw-r--r--test/lua/inlines-filter.lua19
-rw-r--r--test/lua/meta.lua6
3 files changed, 33 insertions, 0 deletions
diff --git a/test/lua/blocks-filter.lua b/test/lua/blocks-filter.lua
new file mode 100644
index 000000000..4e944e922
--- /dev/null
+++ b/test/lua/blocks-filter.lua
@@ -0,0 +1,8 @@
+function Blocks (blks)
+ -- verify that this looks like a `pandoc.List`
+ if not blks.find or not blks.map or not blks.filter then
+ error("table doesn't seem to be an instance of pandoc.List")
+ end
+ -- return plain block containing the number of elements in the list
+ return {pandoc.Plain {pandoc.Str(tostring(#blks))}}
+end
diff --git a/test/lua/inlines-filter.lua b/test/lua/inlines-filter.lua
new file mode 100644
index 000000000..69608bd77
--- /dev/null
+++ b/test/lua/inlines-filter.lua
@@ -0,0 +1,19 @@
+function isWorldAfterSpace (fst, snd)
+ return fst and fst.t == 'LineBreak'
+ and snd and snd.t == 'Str' and snd.text == 'World!'
+end
+
+function Inlines (inlns)
+ -- verify that this looks like a `pandoc.List`
+ if not inlns.find or not inlns.map or not inlns.filter then
+ error("table doesn't seem to be an instance of pandoc.List")
+ end
+
+ -- Remove spaces before string "World"
+ for i = #inlns-1,1,-1 do
+ if isWorldAfterSpace(inlns[i], inlns[i+1]) then
+ inlns[i] = pandoc.Space()
+ end
+ end
+ return inlns
+end
diff --git a/test/lua/meta.lua b/test/lua/meta.lua
new file mode 100644
index 000000000..5e2946203
--- /dev/null
+++ b/test/lua/meta.lua
@@ -0,0 +1,6 @@
+function Meta (meta)
+ meta.old = nil
+ meta.new = "new"
+ meta.bool = (meta.bool == false)
+ return meta
+end