@@ -21,14 +21,9 @@ local sys = require 'pandoc.system'
21
21
local utils = require ' pandoc.utils'
22
22
-- local ut = require "module-lua.utils"
23
23
24
- -- Save env. variables
24
+ -- Save env. variables and root working dir
25
25
local env = sys .environment ()
26
-
27
- -- Save meta table and metadata
28
- local meta
29
- function save_meta (m )
30
- meta = m
31
- end
26
+ local cwd = system .get_working_directory ()
32
27
33
28
--- Replace variables in code blocks
34
29
local metaMap
@@ -97,6 +92,11 @@ function get_vars (meta)
97
92
-- If this is nil, markdown is used as a default format.
98
93
default_format = meta [' include-format' ]
99
94
95
+ -- If all relative include paths are treated relative to the current working directory.
96
+ -- An attribute "relative-to-current" can be used on include blocks, images, codeblock includes
97
+ -- to to selectively choose if the include is relative to the current document.
98
+ includes_relative_to_cwd = meta [' include-paths-relative-to-cwd' ]
99
+
100
100
-- Save meta table for var_replace
101
101
metaMap = meta
102
102
end
@@ -120,14 +120,16 @@ local function update_contents(blocks, shift_by, include_path)
120
120
end ,
121
121
-- If image paths are relative then prepend include file path
122
122
Image = function (image )
123
- if path .is_relative (image .src ) then
123
+ if (not includes_relative_to_cwd or image .classes :includes (" relative-to-current" )) and
124
+ path .is_relative (image .src ) then
124
125
image .src = path .normalize (path .join ({include_path , image .src }))
125
126
end
126
127
return image
127
128
end ,
128
129
-- Update path for include-code-files.lua filter style CodeBlocks
129
130
CodeBlock = function (cb )
130
- if cb .attributes .include and path .is_relative (cb .attributes .include ) then
131
+ if (not includes_relative_to_cwd or cb .classes :includes (" relative-to-current" )) and
132
+ cb .attributes .include and path .is_relative (cb .attributes .include ) then
131
133
cb .attributes .include =
132
134
path .normalize (path .join ({include_path , cb .attributes .include }))
133
135
end
@@ -191,10 +193,16 @@ function transclude (cb)
191
193
tostring (raw ), line ))
192
194
end
193
195
196
+ -- Make relative include path relative to pandoc's working
197
+ -- dir and make it absolute
198
+ if (includes_relative_to_cwd and not cb .classes :includes (" relative-to-current" )) and
199
+ path .is_relative (line ) then
200
+ line = path .normalize (path .join ({cwd , line }))
201
+ end
202
+
194
203
local fh = io.open (line )
195
204
if not fh then
196
- local cwd = system .get_working_directory ()
197
- local msg = " Cannot find include file: '" .. line .. " ' in working dir: '" .. cwd .. " '"
205
+ local msg = " Cannot find include file: '" .. line .. " ', curr. working dir: '" .. cwd .. " '"
198
206
if include_fail_if_read_error then
199
207
io.stderr :write (msg .. " | error\n " )
200
208
error (" Abort due to include failure" )
@@ -215,6 +223,7 @@ function transclude (cb)
215
223
-- Inlcude as parsed AST
216
224
local contents = pandoc .read (text , format ).blocks
217
225
last_heading_level = 0
226
+
218
227
-- Recursive transclusion
219
228
contents = system .with_working_directory (
220
229
path .directory (line ),
@@ -224,10 +233,11 @@ function transclude (cb)
224
233
{ Header = update_last_level , CodeBlock = transclude }
225
234
)
226
235
end ).content
236
+
227
237
--- Reset to level before recursion
228
238
last_heading_level = buffer_last_heading_level
229
- blocks :extend (update_contents (contents , shift_heading_level_by ,
230
- path .directory (line )))
239
+ blocks :extend (update_contents (contents , shift_heading_level_by ,
240
+ path .directory (line )))
231
241
end
232
242
233
243
:: skip_to_next::
0 commit comments