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

Added no_relative option that disables the relative path includes, as… #270

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
71 changes: 47 additions & 24 deletions include-files/include-files.lua
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,16 @@ local List = require 'pandoc.List'
local path = require 'pandoc.path'
local system = require 'pandoc.system'

--- Get include auto mode
--- Get include auto mode and no relative mode
local include_auto = false
local no_relative = false
function get_vars (meta)
if meta['include-auto'] then
include_auto = true
end
if meta['no-relative'] then
no_relative = true
end
end

--- Keep last heading level found
Expand All @@ -26,30 +30,43 @@ end

--- Update contents of included file
local function update_contents(blocks, shift_by, include_path)
local update_contents_filter = {
-- Shift headings in block list by given number
Header = function (header)
if shift_by then
header.level = header.level + shift_by
end
return header
end,
-- If image paths are relative then prepend include file path
Image = function (image)
if path.is_relative(image.src) then
image.src = path.normalize(path.join({include_path, image.src}))
local update_contents_filter
if no_relative then
update_contents_filter = {
-- Shift headings in block list by given number
Header = function (header)
if shift_by then
header.level = header.level + shift_by
end
return header
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}))
}
else
update_contents_filter = {
-- Shift headings in block list by given number
Header = function (header)
if shift_by then
header.level = header.level + shift_by
end
return cb
end
}
return header
end,
-- If image paths are relative then prepend include file path
Image = function (image)
if path.is_relative(image.src) then
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
}
end

return pandoc.walk_block(pandoc.Div(blocks), update_contents_filter).content
end
Expand Down Expand Up @@ -95,8 +112,14 @@ function transclude (cb)
).blocks
last_heading_level = 0
-- recursive transclusion
local directory
if no_relative then
directory = path.directory('./')
else
directory = path.directory(line)
end
contents = system.with_working_directory(
path.directory(line),
directory,
function ()
return pandoc.walk_block(
pandoc.Div(contents),
Expand Down