Skip to content

Commit 594d236

Browse files
committed
Improve relative file includes
- Still allow includes from current working directory.
1 parent 9927681 commit 594d236

File tree

2 files changed

+32
-14
lines changed

2 files changed

+32
-14
lines changed

include-files/README.md

+9-1
Original file line numberDiff line numberDiff line change
@@ -105,13 +105,21 @@ subdir/file-h-latex.md
105105

106106
Included files can in turn include other files. Note that all
107107
filenames must be relative to the directory from which they are
108-
included. I.e., if a file `a/b.md` is included in the main
108+
included.
109+
I.e., if a file `a/b.md` is included in the main
109110
document, and another file `a/b/c.md` should be included from
110111
`a/b.md`, then the relative path from `a/b.md` must be used, in
111112
this case `b/c.md`. The full relative path will be automatically
112113
generated in the final document. The same goes for image paths and
113114
codeblock file paths using the `include-code-files` filter.
114115

116+
If you set the meta variable `include-paths-relative-to-cwd` to `true`,
117+
all include paths are treated relative to pandoc's working directory.
118+
You can selectively set the attribute `relative-to-current` on all pandoc
119+
elements modiefied by this filter (e.g. image, codeblock file paths,
120+
codeblock transclusion) which will treat the
121+
paths relative to the directory from which they are included.
122+
115123
### Missing Includes
116124

117125
You can set the meta data variable `include-fail-if-read-error` to `true`

include-files/include-files.lua

+23-13
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,9 @@ local sys = require 'pandoc.system'
2121
local utils = require 'pandoc.utils'
2222
-- local ut = require "module-lua.utils"
2323

24-
-- Save env. variables
24+
-- Save env. variables and root working dir
2525
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()
3227

3328
--- Replace variables in code blocks
3429
local 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
102102
end
@@ -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

Comments
 (0)