Skip to content

Commit c162972

Browse files
committed
include-files: support recursive includes
Closes: #101
1 parent f0ad062 commit c162972

File tree

4 files changed

+30
-2
lines changed

4 files changed

+30
-2
lines changed

include-files/README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,14 @@ will want to include files written in a different format. An
3131
alternative format can be specified via the `format` attribute.
3232
Only plain-text formats are accepted.
3333

34+
### Recursive transclusion
35+
36+
Included files can in turn include other files. Note that all
37+
filenames must be relative to the directory from which pandoc is
38+
run. I.e., if a file `a/b.md` is included in the main document,
39+
and another file `a/b/c.md` should be included, the full relative
40+
path must be used. Writing `b/c.md` in `a/b.md` would *not* work.
41+
3442
## Example
3543

3644
Let's assume we are writing a longer document, like a thesis.

include-files/expected.native

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77
,Header 1 ("different-format",[],[]) [Str "Different",Space,Str "format"]
88
,Header 2 ("org-header",[],[]) [Str "Org",Space,Str "header"]
99
,Para [Str "This",Space,Str "is",Space,Emph [Str "emphasized"],Str "."]
10+
,Header 1 ("recursive-transclusion",[],[]) [Str "Recursive",Space,Str "transclusion"]
11+
,Para [Str "This",Space,Str "is",Space,Code ("",[],[]) "file-f.md",Str "."]
12+
,Para [Str "This",Space,Str "is",Space,Code ("",[],[]) "file-a.md",Str "."]
1013
,Header 1 ("appendix",[],[]) [Str "Appendix"]
1114
,Para [Str "More",Space,Str "info",Space,Str "goes",Space,Str "here."]
1215
,Header 2 ("questionaire",[],[]) [Str "Questionaire"]

include-files/include-files.lua

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
local List = require 'pandoc.List'
88

99
--- Shift headings in block list by given number
10-
function shift_headings(blocks, shift_by)
10+
local function shift_headings(blocks, shift_by)
1111
if not shift_by then
1212
return blocks
1313
end
@@ -23,7 +23,8 @@ function shift_headings(blocks, shift_by)
2323
end
2424

2525
--- Filter function for code blocks
26-
function CodeBlock(cb)
26+
local transclude
27+
function transclude (cb)
2728
-- ignore code blocks which are not of class "include".
2829
if not cb.classes:includes 'include' then
2930
return
@@ -40,9 +41,18 @@ function CodeBlock(cb)
4041
if line:sub(1,2) ~= '//' then
4142
local fh = io.open(line)
4243
local contents = pandoc.read(fh:read '*a', format).blocks
44+
-- recursive transclusion
45+
contents = pandoc.walk_block(
46+
pandoc.Div(contents),
47+
{CodeBlock = transclude}
48+
).content
4349
blocks:extend(shift_headings(contents, shift_heading_level_by))
4450
fh:close()
4551
end
4652
end
4753
return blocks
4854
end
55+
56+
return {
57+
{CodeBlock = transclude}
58+
}

include-files/sample.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,13 @@ file-b.md
2121
file-d.org
2222
```
2323

24+
# Recursive transclusion
25+
26+
``` {.include}
27+
// this will also include file-a.md
28+
file-f.md
29+
```
30+
2431
# Appendix
2532

2633
More info goes here.

0 commit comments

Comments
 (0)