Skip to content

Commit 19b7839

Browse files
committed
Remove unn
1 parent 386ec08 commit 19b7839

File tree

5 files changed

+128
-0
lines changed

5 files changed

+128
-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

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

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)