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+
79local List = require ' pandoc.List'
10+ local path = require ' pandoc.path'
11+ local system = require ' pandoc.system'
812
913--- Get include auto mode
1014local include_auto = false
@@ -20,20 +24,26 @@ function update_last_level(header)
2024 last_heading_level = header .level
2125end
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
3747end
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