Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix tests and update relative link paths for include-files #285

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions include-files/Makefile
Original file line number Diff line number Diff line change
@@ -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
4 changes: 2 additions & 2 deletions include-files/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
34 changes: 31 additions & 3 deletions include-files/expected-auto.native
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
34 changes: 31 additions & 3 deletions include-files/expected.native
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
16 changes: 16 additions & 0 deletions include-files/include-files.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 6 additions & 0 deletions include-files/subdir/file-g.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading