@@ -21,14 +21,9 @@ local sys = require 'pandoc.system'
2121local utils = require ' pandoc.utils'
2222-- local ut = require "module-lua.utils"
2323
24- -- Save env. variables
24+ -- Save env. variables and root working dir
2525local 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 ()
3227
3328--- Replace variables in code blocks
3429local metaMap
@@ -97,6 +92,11 @@ function get_vars (meta)
9792 -- If this is nil, markdown is used as a default format.
9893 default_format = meta [' include-format' ]
9994
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+
100100 -- Save meta table for var_replace
101101 metaMap = meta
102102end
@@ -120,14 +120,16 @@ local function update_contents(blocks, shift_by, include_path)
120120 end ,
121121 -- If image paths are relative then prepend include file path
122122 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
124125 image .src = path .normalize (path .join ({include_path , image .src }))
125126 end
126127 return image
127128 end ,
128129 -- Update path for include-code-files.lua filter style CodeBlocks
129130 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
131133 cb .attributes .include =
132134 path .normalize (path .join ({include_path , cb .attributes .include }))
133135 end
@@ -191,10 +193,16 @@ function transclude (cb)
191193 tostring (raw ), line ))
192194 end
193195
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+
194203 local fh = io.open (line )
195204 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 .. " '"
198206 if include_fail_if_read_error then
199207 io.stderr :write (msg .. " | error\n " )
200208 error (" Abort due to include failure" )
@@ -215,6 +223,7 @@ function transclude (cb)
215223 -- Inlcude as parsed AST
216224 local contents = pandoc .read (text , format ).blocks
217225 last_heading_level = 0
226+
218227 -- Recursive transclusion
219228 contents = system .with_working_directory (
220229 path .directory (line ),
@@ -224,10 +233,11 @@ function transclude (cb)
224233 { Header = update_last_level , CodeBlock = transclude }
225234 )
226235 end ).content
236+
227237 --- Reset to level before recursion
228238 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 )))
231241 end
232242
233243 :: skip_to_next::
0 commit comments