diff --git a/codeblock-var-replace/Makefile b/codeblock-var-replace/Makefile new file mode 100644 index 00000000..3ffd965b --- /dev/null +++ b/codeblock-var-replace/Makefile @@ -0,0 +1,19 @@ +DIFF ?= diff --strip-trailing-cr -u + +.EXPORT_ALL_VARIABLES: + +BANANA = eat a banana + + +test: sample.md + @pandoc --lua-filter=codeblock-var-replace.lua --to=native $< \ + | $(DIFF) expected.native - + +expected.native: sample.md + pandoc --lua-filter=codeblock-var-replace.lua --output $@ $< + +expected.json: sample.md + pandoc --lua-filter=codeblock-var-replace.lua -t json --output $@ $< + + +.PHONY: test diff --git a/codeblock-var-replace/README.md b/codeblock-var-replace/README.md new file mode 100644 index 00000000..19780769 --- /dev/null +++ b/codeblock-var-replace/README.md @@ -0,0 +1,41 @@ +# codeblock-var-replace + +Filter to replace variables in code blocks with values from environment +or meta data. All variables in the form `${namespace:name}` in a code +block with a class `.var-replace` are replaced. +This is very useful in conjuction with a downstream filter `include-files.lua` filter. + +## Variables + +A variables needs to be of the form `${namespace:name}` where +`namespace` is currently one of `env`,`meta` with the following replacement behavior: + +- `env` : Substituting the environment variable `name`. +- `meta` : Substituting the **stringified** variable `name` from the meta data block. + +## Example + +Note that meta data is parsed as markdown, therefore use a +general code blocks `` `text` ``: + + --- + author: "`The fearful bear`" + title: Thesis + + monkey: "`Hello: I am a monkey`" + "giraffe and zebra" : "`cool and humble !`" + mypath: "`chapters: 1/A B.txt`" + food: "chocolate" + --- + + ## Replace + + ``` {.var-replace} + ${meta:monkey} and ${env:BANANA} + + Zebras and giraffes are ${meta:giraffe and zebra} + + ${meta:author} thanks for everything in '${meta:mypath}' + and of course eat some ${meta:food} + ``` + diff --git a/codeblock-var-replace/codeblock-var-replace.lua b/codeblock-var-replace/codeblock-var-replace.lua new file mode 100644 index 00000000..dbb8b0c8 --- /dev/null +++ b/codeblock-var-replace/codeblock-var-replace.lua @@ -0,0 +1,46 @@ +--- codeblock-var-replace.lua – replace variables in code blocks +--- +--- Copyright: © 2019–2020 Gabriel Nützi +--- License: MIT – see LICENSE file for details + +local sys = require 'pandoc.system' +local utils = require 'pandoc.utils' + +-- Save env. variables +local env = sys.environment() + +-- Save meta table and metadata +local meta +function save_meta (m) + meta = m +end + +--- Replace variable with values from environment +--- and meta data (stringifing). +local function replace(what, var) + if what == "env" then + return env[var] + elseif what == "meta" then + local v = meta[var] + if v then + return utils.stringify(v) + end + end + return nil +end + +--- Replace variables in code blocks +function var_replace_codeblocks (cb) + -- ignore code blocks which are not of class "var-replace". + if not cb.classes:includes 'var-replace' then + return + end + + cb.text = cb.text:gsub("%${(%l+):([^}]+)}", replace) + return cb +end + +return { + { Meta = save_meta }, + { CodeBlock = var_replace_codeblocks } +} diff --git a/codeblock-var-replace/expected.native b/codeblock-var-replace/expected.native new file mode 100644 index 00000000..835a311c --- /dev/null +++ b/codeblock-var-replace/expected.native @@ -0,0 +1,2 @@ +[Header 2 ("replace",[],[]) [Str "Replace"] +,CodeBlock ("",["var-replace"],[]) "Hello: I am a monkey and eat a banana\n\nZebras and giraffes are cool and humble !\n\nThe fearful bear thanks for everything in 'chapters: 1/A B.txt'\nand of course eat some chocolate"] diff --git a/codeblock-var-replace/sample.md b/codeblock-var-replace/sample.md new file mode 100644 index 00000000..7ef5b9be --- /dev/null +++ b/codeblock-var-replace/sample.md @@ -0,0 +1,20 @@ +--- +author: "`The fearful bear`" +title: Thesis + +monkey: "`Hello: I am a monkey`" +"giraffe and zebra" : "`cool and humble !`" +mypath: "`chapters: 1/A B.txt`" +food: "chocolate" +--- + +## Replace + +``` {.var-replace} +${meta:monkey} and ${env:BANANA} + +Zebras and giraffes are ${meta:giraffe and zebra} + +${meta:author} thanks for everything in '${meta:mypath}' +and of course eat some ${meta:food} +```