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