-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathpandoc-codeblock-var-replace.lua
executable file
·52 lines (45 loc) · 1.13 KB
/
pandoc-codeblock-var-replace.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
--- codeblock-var-replace.lua – replace variables in code blocks
---
--- Copyright: © 2019–2020 Gabriel Nützi
--- License: MIT – see LICENSE file for details
-- pandoc's List type
local List = require 'pandoc.List'
local sys = require 'pandoc.system'
local utils = require 'pandoc.utils'
-- Save env. variables
local env = sys.environment()
-- Save meta table and metadata
local meta
local metadata
local vars
function save_meta (m)
meta = m
metadata = m['metadata']
vars = m['variables']
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 }
}