Skip to content

Commit fc59d82

Browse files
committed
Filter for variable replacement in code blocks
- Replacing of `${namespace:var}` in code blocks with values from the environment or from the meta data block.
1 parent 386ec08 commit fc59d82

File tree

5 files changed

+129
-0
lines changed

5 files changed

+129
-0
lines changed

codeblock-var-replace/Makefile

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
DIFF ?= diff --strip-trailing-cr -u
2+
3+
.EXPORT_ALL_VARIABLES:
4+
5+
BANANA = eat a banana
6+
7+
8+
test: sample.md
9+
@pandoc --lua-filter=codeblock-var-replace.lua --to=native $< \
10+
| $(DIFF) expected.native -
11+
12+
expected.native: sample.md
13+
pandoc --lua-filter=codeblock-var-replace.lua --output $@ $<
14+
15+
expected.json: sample.md
16+
pandoc --lua-filter=codeblock-var-replace.lua -t json --output $@ $<
17+
18+
19+
.PHONY: test

codeblock-var-replace/README.md

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# codeblock-var-replace
2+
3+
Filter to replace variables in code blocks with values from environment
4+
or meta data. All variables in the form `${namespace:name}` in a code
5+
block with a class `.var-replace` are replaced.
6+
7+
## Variables
8+
9+
A variables needs to be of the form `${namespace:name}` where
10+
`namespace` is currently one of `env`,`meta` with the following replacement behavior:
11+
12+
- `env` : Substituting the environment variable `name`.
13+
- `meta` : Substituting the **stringified** variable `name` from the meta data block.
14+
15+
## Example
16+
17+
Note that meta data is parsed as markdown, therefore use a
18+
general code blocks `` `text` ``:
19+
20+
---
21+
author: "`The fearful bear`"
22+
title: Thesis
23+
24+
monkey: "`Hello: I am a monkey`"
25+
"giraffe and zebra" : "`cool and humble !`"
26+
mypath: "`chapters: 1/A B.txt`"
27+
food: "chocolate"
28+
---
29+
30+
## Replace
31+
32+
``` {.var-replace}
33+
${meta:monkey} and ${env:BANANA}
34+
35+
Zebras and giraffes are ${meta:giraffe and zebra}
36+
37+
${meta:author} thanks for everything in '${meta:mypath}'
38+
and of course eat some ${meta:food}
39+
```
40+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
--- codeblock-var-replace.lua – replace variables in code blocks
2+
---
3+
--- Copyright: © 2019–2020 Gabriel Nützi
4+
--- License: MIT – see LICENSE file for details
5+
6+
-- pandoc's List type
7+
local List = require 'pandoc.List'
8+
local sys = require 'pandoc.system'
9+
local utils = require 'pandoc.utils'
10+
11+
-- Save env. variables
12+
local env = sys.environment()
13+
14+
-- Save meta table and metadata
15+
local meta
16+
function save_meta (m)
17+
meta = m
18+
end
19+
20+
--- Replace variable with values from environment
21+
--- and meta data (stringifing).
22+
local function replace(what, var)
23+
if what == "env" then
24+
return env[var]
25+
elseif what == "meta" then
26+
local v = meta[var]
27+
if v then
28+
return utils.stringify(v)
29+
end
30+
end
31+
return nil
32+
end
33+
34+
--- Replace variables in code blocks
35+
function var_replace_codeblocks (cb)
36+
-- ignore code blocks which are not of class "var-replace".
37+
if not cb.classes:includes 'var-replace' then
38+
return
39+
end
40+
41+
cb.text = cb.text:gsub("%${(%l+):([^}]+)}", replace)
42+
return cb
43+
end
44+
45+
return {
46+
{ Meta = save_meta },
47+
{ CodeBlock = var_replace_codeblocks }
48+
}

codeblock-var-replace/expected.native

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[Header 2 ("replace",[],[]) [Str "Replace"]
2+
,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"]

codeblock-var-replace/sample.md

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
---
2+
author: "`The fearful bear`"
3+
title: Thesis
4+
5+
monkey: "`Hello: I am a monkey`"
6+
"giraffe and zebra" : "`cool and humble !`"
7+
mypath: "`chapters: 1/A B.txt`"
8+
food: "chocolate"
9+
---
10+
11+
## Replace
12+
13+
``` {.var-replace}
14+
${meta:monkey} and ${env:BANANA}
15+
16+
Zebras and giraffes are ${meta:giraffe and zebra}
17+
18+
${meta:author} thanks for everything in '${meta:mypath}'
19+
and of course eat some ${meta:food}
20+
```

0 commit comments

Comments
 (0)