@@ -9,20 +9,11 @@ PANDOC_VERSION:must_be_at_least '2.12'
99local List = require ' pandoc.List'
1010local path = require ' pandoc.path'
1111local 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'
2112local 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 ()
2617local 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 )
5743end
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
10288end
10389
104-
105- --- Keep last heading level found
90+ --- Keep last heading level found.
10691local last_heading_level = 0
10792function update_last_level (header )
10893 last_heading_level = header .level
11196--- Update contents of included file
11297local 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
141126end
142127
143- --- Filter function for code blocks
128+ --- Filter function for code blocks.
144129local transclude
145130function 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