Skip to content

Commit f671e39

Browse files
committed
Always substitute variables
1 parent 594d236 commit f671e39

File tree

2 files changed

+26
-36
lines changed

2 files changed

+26
-36
lines changed

include-files/README.md

+8-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
```
@@ -136,6 +136,11 @@ some additional information in the main file `main.md`:
136136
---
137137
author: me
138138
title: Thesis
139+
140+
include-format: markdown+markdown_in_html_blocks+link_attributes+tex_math_dollars+fenced_divs+bracketed_spans
141+
include-auto: true
142+
include-fail-if-read-error: true
143+
include-paths-relative-to-cwd: false
139144
---
140145

141146
# Frontmatter

include-files/include-files.lua

+18-33
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,11 @@ PANDOC_VERSION:must_be_at_least '2.12'
99
local List = require 'pandoc.List'
1010
local path = require 'pandoc.path'
1111
local system = require 'pandoc.system'
12-
local cs = PANDOC_STATE
13-
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'
2112
local utils = require 'pandoc.utils'
22-
-- local ut = require "module-lua.utils"
13+
local cs = PANDOC_STATE
2314

24-
-- Save env. variables and root working dir
25-
local env = sys.environment()
15+
-- Save env. variables and root working dir.
16+
local env = system.environment()
2617
local cwd = system.get_working_directory()
2718

2819
--- Replace variables in code blocks
@@ -48,11 +39,6 @@ local function var_replace_codeblocks (cb)
4839
return repl
4940
end
5041

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-
5642
cb.text = cb.text:gsub("%${(%l+):([^}]+)}", replace)
5743
end
5844

@@ -97,12 +83,11 @@ function get_vars (meta)
9783
-- to to selectively choose if the include is relative to the current document.
9884
includes_relative_to_cwd = meta['include-paths-relative-to-cwd']
9985

100-
-- Save meta table for var_replace
86+
-- Save meta table for var_replace.
10187
metaMap = meta
10288
end
10389

104-
105-
--- Keep last heading level found
90+
--- Keep last heading level found.
10691
local last_heading_level = 0
10792
function update_last_level(header)
10893
last_heading_level = header.level
@@ -111,14 +96,14 @@ end
11196
--- Update contents of included file
11297
local function update_contents(blocks, shift_by, include_path)
11398
local update_contents_filter = {
114-
-- Shift headings in block list by given number
99+
-- Shift headings in block list by given number.
115100
Header = function (header)
116101
if shift_by then
117102
header.level = header.level + shift_by
118103
end
119104
return header
120105
end,
121-
-- If image paths are relative then prepend include file path
106+
-- If image paths are relative then prepend include file path.
122107
Image = function (image)
123108
if (not includes_relative_to_cwd or image.classes:includes("relative-to-current")) and
124109
path.is_relative(image.src) then
@@ -140,20 +125,20 @@ local function update_contents(blocks, shift_by, include_path)
140125
return pandoc.walk_block(pandoc.Div(blocks), update_contents_filter).content
141126
end
142127

143-
--- Filter function for code blocks
128+
--- Filter function for code blocks.
144129
local transclude
145130
function transclude (cb)
146131
-- ignore code blocks which are not of class "include".
147132
if not cb.classes:includes 'include' then
148133
return
149134
end
150135

151-
-- Filter by includes and excludes
136+
-- Filter by includes and excludes.
152137
if not is_included(cb) then
153138
return List{} -- remove block
154139
end
155140

156-
-- Variable substitution
141+
-- Variable substitution.
157142
var_replace_codeblocks(cb)
158143

159144
local format = cb.attributes['format']
@@ -162,7 +147,7 @@ function transclude (cb)
162147
format = default_format
163148
end
164149

165-
-- Check if we include the file as raw inline
150+
-- Check if we include the file as raw inline.
166151
local raw = cb.attributes['raw']
167152
raw = raw == "true"
168153

@@ -173,12 +158,12 @@ function transclude (cb)
173158
shift_heading_level_by = tonumber(shift_input)
174159
else
175160
if include_auto then
176-
-- Auto shift headings
161+
-- Auto shift headings.
177162
shift_heading_level_by = last_heading_level
178163
end
179164
end
180165

181-
--- Keep track of level before recursion
166+
--- Keep track of level before recursion.
182167
local buffer_last_heading_level = last_heading_level
183168

184169
local blocks = List:new()
@@ -194,7 +179,7 @@ function transclude (cb)
194179
end
195180

196181
-- Make relative include path relative to pandoc's working
197-
-- dir and make it absolute
182+
-- dir and make it absolute.
198183
if (includes_relative_to_cwd and not cb.classes:includes("relative-to-current")) and
199184
path.is_relative(line) then
200185
line = path.normalize(path.join({cwd, line}))
@@ -212,19 +197,19 @@ function transclude (cb)
212197
end
213198
end
214199

215-
-- Read the file
200+
-- Read the file.
216201
local text = fh:read('*a')
217202
fh:close()
218203

219204
if raw then
220-
-- Include as raw inline element
205+
-- Include as raw inline element.
221206
blocks:extend({pandoc.RawBlock(format, text)})
222207
else
223208
-- Inlcude as parsed AST
224209
local contents = pandoc.read(text, format).blocks
225210
last_heading_level = 0
226211

227-
-- Recursive transclusion
212+
-- Recursive transclusion.
228213
contents = system.with_working_directory(
229214
path.directory(line),
230215
function ()
@@ -234,7 +219,7 @@ function transclude (cb)
234219
)
235220
end).content
236221

237-
--- Reset to level before recursion
222+
--- Reset to level before recursion.
238223
last_heading_level = buffer_last_heading_level
239224
blocks:extend(update_contents(contents, shift_heading_level_by,
240225
path.directory(line)))

0 commit comments

Comments
 (0)