Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Filter for variable replacement in code blocks #139

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions codeblock-var-replace/Makefile
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
41 changes: 41 additions & 0 deletions codeblock-var-replace/README.md
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}
```

46 changes: 46 additions & 0 deletions codeblock-var-replace/codeblock-var-replace.lua
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 }
}
2 changes: 2 additions & 0 deletions codeblock-var-replace/expected.native
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"]
20 changes: 20 additions & 0 deletions codeblock-var-replace/sample.md
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}
```