aboutsummaryrefslogtreecommitdiff
path: root/doc
diff options
context:
space:
mode:
authorAlbert Krewinkel <albert@zeitkraut.de>2017-08-21 19:00:51 +0200
committerAlbert Krewinkel <albert@zeitkraut.de>2017-08-21 19:00:51 +0200
commit4567105f6c3574c94a368df7da34bac5643ba624 (patch)
treecbf96ead4e0ae75c1b09d41ca5d746211c55b4a2 /doc
parent8ef200b80fa4947b4a89040cc417ea6667448c48 (diff)
downloadpandoc-4567105f6c3574c94a368df7da34bac5643ba624.tar.gz
doc/lua-filter.md: Add metadata variable replacment example
Diffstat (limited to 'doc')
-rw-r--r--doc/lua-filters.md58
1 files changed, 58 insertions, 0 deletions
diff --git a/doc/lua-filters.md b/doc/lua-filters.md
index 78025fd03..320e983ad 100644
--- a/doc/lua-filters.md
+++ b/doc/lua-filters.md
@@ -167,3 +167,61 @@ function Doc (blocks, meta)
)
end
```
+
+### Replacing placeholders with their metadata value
+
+Lua filter functions are run in the order *Inlines → Blocks → Meta → Pandoc*.
+Passing information from a higher level (e.g., metadata) to a lower level (e.g.,
+inlines) is still possible by using two filters living in the same file:
+
+``` lua
+local vars = {}
+
+function get_vars (meta)
+ for k, v in pairs(meta) do
+ if v.t == 'MetaInlines' then
+ vars["$" .. k .. "$"] = v
+ end
+ end
+end
+
+function replace (el)
+ if vars[el.text] then
+ return pandoc.Span(vars[el.text])
+ else
+ return el
+ end
+end
+
+return {{Meta = get_vars}, {Str = replace}}
+```
+
+If the contents of file `occupations.md` is
+
+``` markdown
+---
+name: John MacFarlane
+occupation: Professor of Philosophy
+---
+
+Name
+
+: \$name\$
+
+Occupation
+
+: \$occupation\$
+```
+
+then running `pandoc --lua-filter=meta-vars.lua occupations.md` will output:
+
+``` html
+<dl>
+<dt>Name</dt>
+<dd><p><span>John MacFarlane</span></p>
+</dd>
+<dt>Occupation</dt>
+<dd><p><span>Professor of Philosophy</span></p>
+</dd>
+</dl>
+```