@@ -9,20 +9,11 @@ PANDOC_VERSION:must_be_at_least '2.12'
9
9
local List = require ' pandoc.List'
10
10
local path = require ' pandoc.path'
11
11
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'
21
12
local utils = require ' pandoc.utils'
22
- -- local ut = require "module-lua.utils"
13
+ local cs = PANDOC_STATE
23
14
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 ()
26
17
local cwd = system .get_working_directory ()
27
18
28
19
--- Replace variables in code blocks
@@ -48,11 +39,6 @@ local function var_replace_codeblocks (cb)
48
39
return repl
49
40
end
50
41
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
-
56
42
cb .text = cb .text :gsub (" %${(%l+):([^}]+)}" , replace )
57
43
end
58
44
@@ -97,12 +83,11 @@ function get_vars (meta)
97
83
-- to to selectively choose if the include is relative to the current document.
98
84
includes_relative_to_cwd = meta [' include-paths-relative-to-cwd' ]
99
85
100
- -- Save meta table for var_replace
86
+ -- Save meta table for var_replace.
101
87
metaMap = meta
102
88
end
103
89
104
-
105
- --- Keep last heading level found
90
+ --- Keep last heading level found.
106
91
local last_heading_level = 0
107
92
function update_last_level (header )
108
93
last_heading_level = header .level
111
96
--- Update contents of included file
112
97
local function update_contents (blocks , shift_by , include_path )
113
98
local update_contents_filter = {
114
- -- Shift headings in block list by given number
99
+ -- Shift headings in block list by given number.
115
100
Header = function (header )
116
101
if shift_by then
117
102
header .level = header .level + shift_by
118
103
end
119
104
return header
120
105
end ,
121
- -- If image paths are relative then prepend include file path
106
+ -- If image paths are relative then prepend include file path.
122
107
Image = function (image )
123
108
if (not includes_relative_to_cwd or image .classes :includes (" relative-to-current" )) and
124
109
path .is_relative (image .src ) then
@@ -140,20 +125,20 @@ local function update_contents(blocks, shift_by, include_path)
140
125
return pandoc .walk_block (pandoc .Div (blocks ), update_contents_filter ).content
141
126
end
142
127
143
- --- Filter function for code blocks
128
+ --- Filter function for code blocks.
144
129
local transclude
145
130
function transclude (cb )
146
131
-- ignore code blocks which are not of class "include".
147
132
if not cb .classes :includes ' include' then
148
133
return
149
134
end
150
135
151
- -- Filter by includes and excludes
136
+ -- Filter by includes and excludes.
152
137
if not is_included (cb ) then
153
138
return List {} -- remove block
154
139
end
155
140
156
- -- Variable substitution
141
+ -- Variable substitution.
157
142
var_replace_codeblocks (cb )
158
143
159
144
local format = cb .attributes [' format' ]
@@ -162,7 +147,7 @@ function transclude (cb)
162
147
format = default_format
163
148
end
164
149
165
- -- Check if we include the file as raw inline
150
+ -- Check if we include the file as raw inline.
166
151
local raw = cb .attributes [' raw' ]
167
152
raw = raw == " true"
168
153
@@ -173,12 +158,12 @@ function transclude (cb)
173
158
shift_heading_level_by = tonumber (shift_input )
174
159
else
175
160
if include_auto then
176
- -- Auto shift headings
161
+ -- Auto shift headings.
177
162
shift_heading_level_by = last_heading_level
178
163
end
179
164
end
180
165
181
- --- Keep track of level before recursion
166
+ --- Keep track of level before recursion.
182
167
local buffer_last_heading_level = last_heading_level
183
168
184
169
local blocks = List :new ()
@@ -194,7 +179,7 @@ function transclude (cb)
194
179
end
195
180
196
181
-- Make relative include path relative to pandoc's working
197
- -- dir and make it absolute
182
+ -- dir and make it absolute.
198
183
if (includes_relative_to_cwd and not cb .classes :includes (" relative-to-current" )) and
199
184
path .is_relative (line ) then
200
185
line = path .normalize (path .join ({cwd , line }))
@@ -212,19 +197,19 @@ function transclude (cb)
212
197
end
213
198
end
214
199
215
- -- Read the file
200
+ -- Read the file.
216
201
local text = fh :read (' *a' )
217
202
fh :close ()
218
203
219
204
if raw then
220
- -- Include as raw inline element
205
+ -- Include as raw inline element.
221
206
blocks :extend ({pandoc .RawBlock (format , text )})
222
207
else
223
208
-- Inlcude as parsed AST
224
209
local contents = pandoc .read (text , format ).blocks
225
210
last_heading_level = 0
226
211
227
- -- Recursive transclusion
212
+ -- Recursive transclusion.
228
213
contents = system .with_working_directory (
229
214
path .directory (line ),
230
215
function ()
@@ -234,7 +219,7 @@ function transclude (cb)
234
219
)
235
220
end ).content
236
221
237
- --- Reset to level before recursion
222
+ --- Reset to level before recursion.
238
223
last_heading_level = buffer_last_heading_level
239
224
blocks :extend (update_contents (contents , shift_heading_level_by ,
240
225
path .directory (line )))
0 commit comments