3
3
--- Copyright: © 2019–2021 Albert Krewinkel
4
4
--- License: MIT – see LICENSE file for details
5
5
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
+
7
9
local List = require ' pandoc.List'
10
+ local path = require ' pandoc.path'
11
+ local system = require ' pandoc.system'
8
12
9
13
--- Get include auto mode
10
14
local include_auto = false
@@ -20,20 +24,26 @@ function update_last_level(header)
20
24
last_heading_level = header .level
21
25
end
22
26
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
30
31
Header = function (header )
31
- header .level = header .level + shift_by
32
+ if shift_by then
33
+ header .level = header .level + shift_by
34
+ end
32
35
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
33
43
end
34
44
}
35
45
36
- return pandoc .walk_block (pandoc .Div (blocks ), shift_headings_filter ).content
46
+ return pandoc .walk_block (pandoc .Div (blocks ), update_contents_filter ).content
37
47
end
38
48
39
49
--- Filter function for code blocks
@@ -72,13 +82,22 @@ function transclude (cb)
72
82
local contents = pandoc .read (fh :read ' *a' , format ).blocks
73
83
last_heading_level = 0
74
84
-- 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
79
93
--- reset to level before recursion
80
94
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 ))
82
101
fh :close ()
83
102
end
84
103
end
0 commit comments