Skip to content

Commit 165ae22

Browse files
committed
Always substitute variables
1 parent 594d236 commit 165ae22

File tree

2 files changed

+20
-36
lines changed

2 files changed

+20
-36
lines changed

Diff for: include-files/README.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -80,12 +80,12 @@ subdir/file-i.md
8080

8181
### Variable Substitution in Paths
8282

83-
If attribute `var-replace` is used, the patterns `${meta:<meta-var>}` or `${env:<env-var>}`
84-
will be replaced by the corresponding meta data variable `<meta-var>` in the document or the
83+
The patterns `${meta:<meta-var>}` or `${env:<env-var>}` inside include paths
84+
will be replaced by the corresponding meta data variable `<meta-var>` or the
8585
environment variable `<env-var>`, e.g.
8686

8787
````md
88-
```{.include .var-replace}
88+
```{.include}
8989
${meta:subdir-name}/file-h.md
9090
${env:SUBDIR_NAME}/file-h.md
9191
```

Diff for: include-files/include-files.lua

+17-33
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,8 @@ local path = require 'pandoc.path'
1111
local system = require 'pandoc.system'
1212
local cs = PANDOC_STATE
1313

14-
-- This is the codeblock-var-replace
15-
-- filter directly copied, since we
16-
-- cannot run Lua filters inside this filter
17-
-- https://github.com/jgm/pandoc/issues/6830
18-
-- We replace variables in include blocks.
19-
20-
local sys = require 'pandoc.system'
21-
local utils = require 'pandoc.utils'
22-
-- local ut = require "module-lua.utils"
23-
24-
-- Save env. variables and root working dir
25-
local env = sys.environment()
14+
-- Save env. variables and root working dir.
15+
local env = system.environment()
2616
local cwd = system.get_working_directory()
2717

2818
--- Replace variables in code blocks
@@ -48,11 +38,6 @@ local function var_replace_codeblocks (cb)
4838
return repl
4939
end
5040

51-
-- ignore code blocks which are not of class "var-replace".
52-
if not cb.classes:includes 'var-replace' then
53-
return
54-
end
55-
5641
cb.text = cb.text:gsub("%${(%l+):([^}]+)}", replace)
5742
end
5843

@@ -97,12 +82,11 @@ function get_vars (meta)
9782
-- to to selectively choose if the include is relative to the current document.
9883
includes_relative_to_cwd = meta['include-paths-relative-to-cwd']
9984

100-
-- Save meta table for var_replace
85+
-- Save meta table for var_replace.
10186
metaMap = meta
10287
end
10388

104-
105-
--- Keep last heading level found
89+
--- Keep last heading level found.
10690
local last_heading_level = 0
10791
function update_last_level(header)
10892
last_heading_level = header.level
@@ -111,14 +95,14 @@ end
11195
--- Update contents of included file
11296
local function update_contents(blocks, shift_by, include_path)
11397
local update_contents_filter = {
114-
-- Shift headings in block list by given number
98+
-- Shift headings in block list by given number.
11599
Header = function (header)
116100
if shift_by then
117101
header.level = header.level + shift_by
118102
end
119103
return header
120104
end,
121-
-- If image paths are relative then prepend include file path
105+
-- If image paths are relative then prepend include file path.
122106
Image = function (image)
123107
if (not includes_relative_to_cwd or image.classes:includes("relative-to-current")) and
124108
path.is_relative(image.src) then
@@ -140,20 +124,20 @@ local function update_contents(blocks, shift_by, include_path)
140124
return pandoc.walk_block(pandoc.Div(blocks), update_contents_filter).content
141125
end
142126

143-
--- Filter function for code blocks
127+
--- Filter function for code blocks.
144128
local transclude
145129
function transclude (cb)
146130
-- ignore code blocks which are not of class "include".
147131
if not cb.classes:includes 'include' then
148132
return
149133
end
150134

151-
-- Filter by includes and excludes
135+
-- Filter by includes and excludes.
152136
if not is_included(cb) then
153137
return List{} -- remove block
154138
end
155139

156-
-- Variable substitution
140+
-- Variable substitution.
157141
var_replace_codeblocks(cb)
158142

159143
local format = cb.attributes['format']
@@ -162,7 +146,7 @@ function transclude (cb)
162146
format = default_format
163147
end
164148

165-
-- Check if we include the file as raw inline
149+
-- Check if we include the file as raw inline.
166150
local raw = cb.attributes['raw']
167151
raw = raw == "true"
168152

@@ -173,12 +157,12 @@ function transclude (cb)
173157
shift_heading_level_by = tonumber(shift_input)
174158
else
175159
if include_auto then
176-
-- Auto shift headings
160+
-- Auto shift headings.
177161
shift_heading_level_by = last_heading_level
178162
end
179163
end
180164

181-
--- Keep track of level before recursion
165+
--- Keep track of level before recursion.
182166
local buffer_last_heading_level = last_heading_level
183167

184168
local blocks = List:new()
@@ -194,7 +178,7 @@ function transclude (cb)
194178
end
195179

196180
-- Make relative include path relative to pandoc's working
197-
-- dir and make it absolute
181+
-- dir and make it absolute.
198182
if (includes_relative_to_cwd and not cb.classes:includes("relative-to-current")) and
199183
path.is_relative(line) then
200184
line = path.normalize(path.join({cwd, line}))
@@ -212,19 +196,19 @@ function transclude (cb)
212196
end
213197
end
214198

215-
-- Read the file
199+
-- Read the file.
216200
local text = fh:read('*a')
217201
fh:close()
218202

219203
if raw then
220-
-- Include as raw inline element
204+
-- Include as raw inline element.
221205
blocks:extend({pandoc.RawBlock(format, text)})
222206
else
223207
-- Inlcude as parsed AST
224208
local contents = pandoc.read(text, format).blocks
225209
last_heading_level = 0
226210

227-
-- Recursive transclusion
211+
-- Recursive transclusion.
228212
contents = system.with_working_directory(
229213
path.directory(line),
230214
function ()
@@ -234,7 +218,7 @@ function transclude (cb)
234218
)
235219
end).content
236220

237-
--- Reset to level before recursion
221+
--- Reset to level before recursion.
238222
last_heading_level = buffer_last_heading_level
239223
blocks:extend(update_contents(contents, shift_heading_level_by,
240224
path.directory(line)))

0 commit comments

Comments
 (0)