-
-
Notifications
You must be signed in to change notification settings - Fork 167
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
128 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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} | ||
``` | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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} | ||
``` |