Skip to content

Commit d7c791d

Browse files
lmunchtarleb
authored andcommitted
Set working directory for during recursion
This change will prepend the working directory of the included file to the Image elements src path. E.g if file included is subdoc/somefile.md then subdoc is preprended to any image element in somefile.md This change also fixes include files with relative names when nesting more than one level.
1 parent 861a526 commit d7c791d

File tree

1 file changed

+34
-15
lines changed

1 file changed

+34
-15
lines changed

include-files/include-files.lua

Lines changed: 34 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,12 @@
33
--- Copyright: © 2019–2021 Albert Krewinkel
44
--- License: MIT – see LICENSE file for details
55

6-
-- pandoc's List type
6+
-- Module pandoc.path is required and was added in version 2.12
7+
PANDOC_VERSION:must_be_at_least '2.12'
8+
79
local List = require 'pandoc.List'
10+
local path = require 'pandoc.path'
11+
local system = require 'pandoc.system'
812

913
--- Get include auto mode
1014
local include_auto = false
@@ -20,20 +24,26 @@ function update_last_level(header)
2024
last_heading_level = header.level
2125
end
2226

23-
--- Shift headings in block list by given number
24-
local function shift_headings(blocks, shift_by)
25-
if not shift_by then
26-
return blocks
27-
end
28-
29-
local shift_headings_filter = {
27+
--- Update contents of included file
28+
local function update_contents(blocks, shift_by)
29+
local update_contents_filter = {
30+
-- Shift headings in block list by given number
3031
Header = function (header)
31-
header.level = header.level + shift_by
32+
if shift_by then
33+
header.level = header.level + shift_by
34+
end
3235
return header
36+
end,
37+
-- If image paths are relative then prepend include file path
38+
Image = function (image)
39+
if path.is_relative(image.src) then
40+
image.src = path.join({system.get_working_directory(), image.src})
41+
end
42+
return image
3343
end
3444
}
3545

36-
return pandoc.walk_block(pandoc.Div(blocks), shift_headings_filter).content
46+
return pandoc.walk_block(pandoc.Div(blocks), update_contents_filter).content
3747
end
3848

3949
--- Filter function for code blocks
@@ -72,13 +82,22 @@ function transclude (cb)
7282
local contents = pandoc.read(fh:read '*a', format).blocks
7383
last_heading_level = 0
7484
-- recursive transclusion
75-
contents = pandoc.walk_block(
76-
pandoc.Div(contents),
77-
{ Header = update_last_level, CodeBlock = transclude }
78-
).content
85+
contents = system.with_working_directory(
86+
path.directory(line),
87+
function ()
88+
return pandoc.walk_block(
89+
pandoc.Div(contents),
90+
{ Header = update_last_level, CodeBlock = transclude }
91+
)
92+
end).content
7993
--- reset to level before recursion
8094
last_heading_level = buffer_last_heading_level
81-
blocks:extend(shift_headings(contents, shift_heading_level_by))
95+
blocks:extend(
96+
system.with_working_directory(
97+
path.directory(line),
98+
function ()
99+
return update_contents(contents, shift_heading_level_by)
100+
end))
82101
fh:close()
83102
end
84103
end

0 commit comments

Comments
 (0)