From 585f4e60676973ea8709cf6451b95d2166470406 Mon Sep 17 00:00:00 2001 From: Lars Munch Date: Sat, 24 Apr 2021 08:56:54 +0200 Subject: [PATCH 1/3] Use relative path for images Use the recursive nature of the filter to build up the correct relative path to images --- include-files/include-files.lua | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/include-files/include-files.lua b/include-files/include-files.lua index b8c26f24..f6c07f6a 100644 --- a/include-files/include-files.lua +++ b/include-files/include-files.lua @@ -25,7 +25,7 @@ function update_last_level(header) end --- Update contents of included file -local function update_contents(blocks, shift_by) +local function update_contents(blocks, shift_by, include_path) local update_contents_filter = { -- Shift headings in block list by given number Header = function (header) @@ -37,7 +37,7 @@ local function update_contents(blocks, shift_by) -- If image paths are relative then prepend include file path Image = function (image) if path.is_relative(image.src) then - image.src = path.join({system.get_working_directory(), image.src}) + image.src = path.normalize(path.join({include_path, image.src})) end return image end @@ -92,12 +92,8 @@ function transclude (cb) end).content --- reset to level before recursion last_heading_level = buffer_last_heading_level - blocks:extend( - system.with_working_directory( - path.directory(line), - function () - return update_contents(contents, shift_heading_level_by) - end)) + blocks:extend(update_contents(contents, shift_heading_level_by, + path.directory(line))) fh:close() end end From c467fdbccace4e15705d83c6f8a3e182376e35fc Mon Sep 17 00:00:00 2001 From: Lars Munch Date: Sat, 24 Apr 2021 10:28:12 +0200 Subject: [PATCH 2/3] Update path for include-code-files.lua filter style CodeBlocks This improves the usage of include-files.lua filter in combination with the include-code-files.lua --- include-files/include-files.lua | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/include-files/include-files.lua b/include-files/include-files.lua index f6c07f6a..b9338369 100644 --- a/include-files/include-files.lua +++ b/include-files/include-files.lua @@ -40,6 +40,14 @@ local function update_contents(blocks, shift_by, include_path) image.src = path.normalize(path.join({include_path, image.src})) end return image + end, + -- Update path for include-code-files.lua filter style CodeBlocks + CodeBlock = function (cb) + if cb.attributes.include and path.is_relative(cb.attributes.include) then + cb.attributes.include = + path.normalize(path.join({include_path, cb.attributes.include})) + end + return cb end } From 10e50f4685a9940adebe238087f010237e7ac1f3 Mon Sep 17 00:00:00 2001 From: Lars Munch Date: Thu, 13 May 2021 08:46:16 +0200 Subject: [PATCH 3/3] Updated test and docs for include-files filter --- include-files/Makefile | 2 +- include-files/README.md | 11 +++++++---- include-files/expected-auto.native | 7 +++++++ include-files/expected.native | 7 +++++++ include-files/sample.md | 9 +++++++++ include-files/subdir/file-g.md | 14 ++++++++++++++ 6 files changed, 45 insertions(+), 5 deletions(-) create mode 100644 include-files/subdir/file-g.md diff --git a/include-files/Makefile b/include-files/Makefile index c5f5279e..0e060f86 100644 --- a/include-files/Makefile +++ b/include-files/Makefile @@ -10,6 +10,6 @@ expected.native: sample.md file-a.md file-b.md file-c.md include-files.lua pandoc --lua-filter=include-files.lua --output $@ $< expected-auto.native: sample.md file-a.md file-b.md file-c.md include-files.lua - pandoc --lua-filter=include-files.lua --output $@ $< + pandoc --lua-filter=include-files.lua -M include-auto --output $@ $< .PHONY: test diff --git a/include-files/README.md b/include-files/README.md index a99402a2..38664b8a 100644 --- a/include-files/README.md +++ b/include-files/README.md @@ -62,10 +62,13 @@ Only plain-text formats are accepted. ### Recursive transclusion Included files can in turn include other files. Note that all -filenames must be relative to the directory from which pandoc is -run. I.e., if a file `a/b.md` is included in the main document, -and another file `a/b/c.md` should be included, the full relative -path must be used. Writing `b/c.md` in `a/b.md` would _not_ work. +filenames must be relative to the directory from which they are +included. I.e., if a file `a/b.md` is included in the main +document, and another file `a/b/c.md` should be included from +`a/b.md`, then the relative path from `a/b.md` must be used, in +this case `b/c.md`. The full relative path will be automatically +generated in the final document. The same goes for image paths and +codeblock file paths using the `include-code-files` filter. ## Example diff --git a/include-files/expected-auto.native b/include-files/expected-auto.native index 22be9279..4decb2f7 100644 --- a/include-files/expected-auto.native +++ b/include-files/expected-auto.native @@ -16,6 +16,13 @@ ,Para [Str "This",Space,Str "is",Space,Code ("",[],[]) "file-a.md",Str "."] ,Header 3 ("title-of-file-a",[],[]) [Str "Title",Space,Str "of",Space,Str "file-a"] ,Para [Str "This",Space,Str "is",Space,Code ("",[],[]) "file-a.md",Str "."] +,Header 1 ("subdirectories",[],[]) [Str "Subdirectories"] +,Header 2 ("image-include",[],[]) [Str "Image",Space,Str "include"] +,Para [Str "Image",Space,Str "relative",Space,Str "path",Space,Str "will",Space,Str "be",Space,Str "updated."] +,Para [Image ("",[],[]) [Str "Image",Space,Str "title"] ("subdir/someimage.png","fig:")] +,Header 2 ("source-include",[],[]) [Str "Source",Space,Str "include"] +,Para [Str "File",Space,Str "inclusion",Space,Str "codeblocks",Space,Str "for",Space,Str "use",Space,Str "with",Space,Str "include-code-files",Space,Str "will",Space,Str "be",SoftBreak,Str "updated",Space,Str "too."] +,CodeBlock ("",["c"],[("include","subdir/somecode.c")]) "" ,Header 1 ("appendix",[],[]) [Str "Appendix"] ,Para [Str "More",Space,Str "info",Space,Str "goes",Space,Str "here."] ,Header 2 ("questionaire",[],[]) [Str "Questionaire"] diff --git a/include-files/expected.native b/include-files/expected.native index d50caa48..46a02883 100644 --- a/include-files/expected.native +++ b/include-files/expected.native @@ -16,6 +16,13 @@ ,Para [Str "This",Space,Str "is",Space,Code ("",[],[]) "file-a.md",Str "."] ,Header 2 ("title-of-file-a",[],[]) [Str "Title",Space,Str "of",Space,Str "file-a"] ,Para [Str "This",Space,Str "is",Space,Code ("",[],[]) "file-a.md",Str "."] +,Header 1 ("subdirectories",[],[]) [Str "Subdirectories"] +,Header 1 ("image-include",[],[]) [Str "Image",Space,Str "include"] +,Para [Str "Image",Space,Str "relative",Space,Str "path",Space,Str "will",Space,Str "be",Space,Str "updated."] +,Para [Image ("",[],[]) [Str "Image",Space,Str "title"] ("subdir/someimage.png","fig:")] +,Header 1 ("source-include",[],[]) [Str "Source",Space,Str "include"] +,Para [Str "File",Space,Str "inclusion",Space,Str "codeblocks",Space,Str "for",Space,Str "use",Space,Str "with",Space,Str "include-code-files",Space,Str "will",Space,Str "be",SoftBreak,Str "updated",Space,Str "too."] +,CodeBlock ("",["c"],[("include","subdir/somecode.c")]) "" ,Header 1 ("appendix",[],[]) [Str "Appendix"] ,Para [Str "More",Space,Str "info",Space,Str "goes",Space,Str "here."] ,Header 2 ("questionaire",[],[]) [Str "Questionaire"] diff --git a/include-files/sample.md b/include-files/sample.md index b0352fb8..c2831709 100644 --- a/include-files/sample.md +++ b/include-files/sample.md @@ -28,6 +28,15 @@ file-d.org file-f.md ``` +# Subdirectories + +``` {.include} +// file-g.md includes an image and source code. The relative +// path used in file-g.md will be prefixed with subdir in the +// final document. +subdir/file-g.md +``` + # Appendix More info goes here. diff --git a/include-files/subdir/file-g.md b/include-files/subdir/file-g.md new file mode 100644 index 00000000..1ce89a90 --- /dev/null +++ b/include-files/subdir/file-g.md @@ -0,0 +1,14 @@ +# Image include + +Image relative path will be updated. + +![Image title](someimage.png) + +# Source include + +File inclusion codeblocks for use with include-code-files will be +updated too. + +```{.c include=somecode.c} +``` +