diff --git a/include-files/Makefile b/include-files/Makefile index 61039b09..3bea887e 100644 --- a/include-files/Makefile +++ b/include-files/Makefile @@ -1,16 +1,16 @@ DIFF ?= diff --strip-trailing-cr -u PANDOC ?= pandoc -test: sample.md file-a.md file-b.md file-c.md include-files.lua +test: sample.md file-a.md file-b.md file-c.md subdir/file-g.md include-files.lua @$(PANDOC) --lua-filter=include-files.lua --to=native $< \ | $(DIFF) expected.native - @$(PANDOC) --lua-filter=include-files.lua -M include-auto --to=native $< \ | $(DIFF) expected-auto.native - -expected.native: sample.md file-a.md file-b.md file-c.md include-files.lua +expected.native: sample.md file-a.md file-b.md file-c.md subdir/file-g.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 +expected-auto.native: sample.md file-a.md file-b.md file-c.md subdir/file-g.md include-files.lua $(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 050a94af..2d2197d7 100644 --- a/include-files/README.md +++ b/include-files/README.md @@ -69,8 +69,8 @@ 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. +document. The same goes for image paths, link 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 5536cbf6..144d73ff 100644 --- a/include-files/expected-auto.native +++ b/include-files/expected-auto.native @@ -117,11 +117,39 @@ , Space , Str "updated." ] +, Figure + ( "" , [] , [] ) + (Caption + Nothing [ Plain [ Str "Image" , Space , Str "title" ] ]) + [ Plain + [ Image + ( "" , [] , [] ) + [ Str "Image" , Space , Str "title" ] + ( "subdir/someimage.png" , "" ) + ] + ] +, Figure + ( "" , [] , [] ) + (Caption + Nothing [ Plain [ Str "Image" , Space , Str "title" ] ]) + [ Plain + [ Image + ( "" , [] , [] ) + [ Str "Image" , Space , Str "title" ] + ( "https://example.com/someimage.png" , "" ) + ] + ] +, Para + [ Link + ( "" , [] , [] ) + [ Str "Some" , Space , Str "link" ] + ( "subdir/someimage.png" , "" ) + ] , Para - [ Image + [ Link ( "" , [] , [] ) - [ Str "Image" , Space , Str "title" ] - ( "subdir/someimage.png" , "fig:" ) + [ Str "Some" , Space , Str "link" ] + ( "https://example.com/someimage.png" , "" ) ] , Header 2 diff --git a/include-files/expected.native b/include-files/expected.native index e89a6ba8..c0889e75 100644 --- a/include-files/expected.native +++ b/include-files/expected.native @@ -117,11 +117,39 @@ , Space , Str "updated." ] +, Figure + ( "" , [] , [] ) + (Caption + Nothing [ Plain [ Str "Image" , Space , Str "title" ] ]) + [ Plain + [ Image + ( "" , [] , [] ) + [ Str "Image" , Space , Str "title" ] + ( "subdir/someimage.png" , "" ) + ] + ] +, Figure + ( "" , [] , [] ) + (Caption + Nothing [ Plain [ Str "Image" , Space , Str "title" ] ]) + [ Plain + [ Image + ( "" , [] , [] ) + [ Str "Image" , Space , Str "title" ] + ( "https://example.com/someimage.png" , "" ) + ] + ] +, Para + [ Link + ( "" , [] , [] ) + [ Str "Some" , Space , Str "link" ] + ( "subdir/someimage.png" , "" ) + ] , Para - [ Image + [ Link ( "" , [] , [] ) - [ Str "Image" , Space , Str "title" ] - ( "subdir/someimage.png" , "fig:" ) + [ Str "Some" , Space , Str "link" ] + ( "https://example.com/someimage.png" , "" ) ] , Header 1 diff --git a/include-files/include-files.lua b/include-files/include-files.lua index 2499968c..c9a585a6 100644 --- a/include-files/include-files.lua +++ b/include-files/include-files.lua @@ -39,8 +39,24 @@ local function update_contents(blocks, shift_by, include_path) end return header end, + -- If link paths are relative then prepend include file path + Link = function (link) + if string.match(link.target, "^%a+://") then + return link + end + + if path.is_relative(link.target) then + link.target = path.normalize(path.join({include_path, link.target})) + end + + return link + end, -- If image paths are relative then prepend include file path Image = function (image) + if string.match(image.src, "^%a+://") then + return image + end + if path.is_relative(image.src) then image.src = path.normalize(path.join({include_path, image.src})) end diff --git a/include-files/subdir/file-g.md b/include-files/subdir/file-g.md index 1ce89a90..cd97dcd9 100644 --- a/include-files/subdir/file-g.md +++ b/include-files/subdir/file-g.md @@ -4,6 +4,12 @@ Image relative path will be updated. ![Image title](someimage.png) +![Image title](https://example.com/someimage.png) + +[Some link](someimage.png) + +[Some link](https://example.com/someimage.png) + # Source include File inclusion codeblocks for use with include-code-files will be