Skip to content

Commit

Permalink
Always substitute variables
Browse files Browse the repository at this point in the history
  • Loading branch information
gabyx committed May 24, 2021
1 parent 594d236 commit f671e39
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 36 deletions.
11 changes: 8 additions & 3 deletions include-files/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,12 +80,12 @@ subdir/file-i.md

### Variable Substitution in Paths

If attribute `var-replace` is used, the patterns `${meta:<meta-var>}` or `${env:<env-var>}`
will be replaced by the corresponding meta data variable `<meta-var>` in the document or the
The patterns `${meta:<meta-var>}` or `${env:<env-var>}` inside include paths
will be replaced by the corresponding meta data variable `<meta-var>` or the
environment variable `<env-var>`, e.g.

````md
```{.include .var-replace}
```{.include}
${meta:subdir-name}/file-h.md
${env:SUBDIR_NAME}/file-h.md
```
Expand Down Expand Up @@ -136,6 +136,11 @@ some additional information in the main file `main.md`:
---
author: me
title: Thesis

include-format: markdown+markdown_in_html_blocks+link_attributes+tex_math_dollars+fenced_divs+bracketed_spans
include-auto: true
include-fail-if-read-error: true
include-paths-relative-to-cwd: false
---

# Frontmatter
Expand Down
51 changes: 18 additions & 33 deletions include-files/include-files.lua
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,11 @@ PANDOC_VERSION:must_be_at_least '2.12'
local List = require 'pandoc.List'
local path = require 'pandoc.path'
local system = require 'pandoc.system'
local cs = PANDOC_STATE

-- This is the codeblock-var-replace
-- filter directly copied, since we
-- cannot run Lua filters inside this filter
-- https://github.com/jgm/pandoc/issues/6830
-- We replace variables in include blocks.

local sys = require 'pandoc.system'
local utils = require 'pandoc.utils'
-- local ut = require "module-lua.utils"
local cs = PANDOC_STATE

-- Save env. variables and root working dir
local env = sys.environment()
-- Save env. variables and root working dir.
local env = system.environment()
local cwd = system.get_working_directory()

--- Replace variables in code blocks
Expand All @@ -48,11 +39,6 @@ local function var_replace_codeblocks (cb)
return repl
end

-- ignore code blocks which are not of class "var-replace".
if not cb.classes:includes 'var-replace' then
return
end

cb.text = cb.text:gsub("%${(%l+):([^}]+)}", replace)
end

Expand Down Expand Up @@ -97,12 +83,11 @@ function get_vars (meta)
-- to to selectively choose if the include is relative to the current document.
includes_relative_to_cwd = meta['include-paths-relative-to-cwd']

-- Save meta table for var_replace
-- Save meta table for var_replace.
metaMap = meta
end


--- Keep last heading level found
--- Keep last heading level found.
local last_heading_level = 0
function update_last_level(header)
last_heading_level = header.level
Expand All @@ -111,14 +96,14 @@ 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
-- 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
-- If image paths are relative then prepend include file path.
Image = function (image)
if (not includes_relative_to_cwd or image.classes:includes("relative-to-current")) and
path.is_relative(image.src) then
Expand All @@ -140,20 +125,20 @@ local function update_contents(blocks, shift_by, include_path)
return pandoc.walk_block(pandoc.Div(blocks), update_contents_filter).content
end

--- Filter function for code blocks
--- Filter function for code blocks.
local transclude
function transclude (cb)
-- ignore code blocks which are not of class "include".
if not cb.classes:includes 'include' then
return
end

-- Filter by includes and excludes
-- Filter by includes and excludes.
if not is_included(cb) then
return List{} -- remove block
end

-- Variable substitution
-- Variable substitution.
var_replace_codeblocks(cb)

local format = cb.attributes['format']
Expand All @@ -162,7 +147,7 @@ function transclude (cb)
format = default_format
end

-- Check if we include the file as raw inline
-- Check if we include the file as raw inline.
local raw = cb.attributes['raw']
raw = raw == "true"

Expand All @@ -173,12 +158,12 @@ function transclude (cb)
shift_heading_level_by = tonumber(shift_input)
else
if include_auto then
-- Auto shift headings
-- Auto shift headings.
shift_heading_level_by = last_heading_level
end
end

--- Keep track of level before recursion
--- Keep track of level before recursion.
local buffer_last_heading_level = last_heading_level

local blocks = List:new()
Expand All @@ -194,7 +179,7 @@ function transclude (cb)
end

-- Make relative include path relative to pandoc's working
-- dir and make it absolute
-- dir and make it absolute.
if (includes_relative_to_cwd and not cb.classes:includes("relative-to-current")) and
path.is_relative(line) then
line = path.normalize(path.join({cwd, line}))
Expand All @@ -212,19 +197,19 @@ function transclude (cb)
end
end

-- Read the file
-- Read the file.
local text = fh:read('*a')
fh:close()

if raw then
-- Include as raw inline element
-- Include as raw inline element.
blocks:extend({pandoc.RawBlock(format, text)})
else
-- Inlcude as parsed AST
local contents = pandoc.read(text, format).blocks
last_heading_level = 0

-- Recursive transclusion
-- Recursive transclusion.
contents = system.with_working_directory(
path.directory(line),
function ()
Expand All @@ -234,7 +219,7 @@ function transclude (cb)
)
end).content

--- Reset to level before recursion
--- Reset to level before recursion.
last_heading_level = buffer_last_heading_level
blocks:extend(update_contents(contents, shift_heading_level_by,
path.directory(line)))
Expand Down

0 comments on commit f671e39

Please sign in to comment.