Skip to content

Commit b9b7ac0

Browse files
authored
Add support for including text from other files into code blocks. (#277)
Also strip out carriage-returns, as it messes with code block rendering.
1 parent d7eeefa commit b9b7ac0

File tree

5 files changed

+51
-0
lines changed

5 files changed

+51
-0
lines changed

build.sh

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -789,6 +789,7 @@ do_latex() {
789789
--lua-filter=convert-diagrams.lua
790790
--lua-filter=convert-images.lua
791791
--lua-filter=center-images.lua
792+
--lua-filter=include-into-code-blocks.lua
792793
--lua-filter=informative-sections.lua
793794
--lua-filter=parse-html.lua
794795
--lua-filter=apply-classes-to-tables.lua
@@ -984,6 +985,7 @@ do_docx() {
984985
--standalone
985986
--lua-filter=convert-diagrams.lua
986987
--lua-filter=convert-images.lua
988+
--lua-filter=include-into-code-blocks.lua
987989
--lua-filter=parse-html.lua
988990
--lua-filter=apply-classes-to-tables.lua
989991
--lua-filter=landscape-pages.lua
@@ -1025,6 +1027,7 @@ do_html() {
10251027
--standalone
10261028
${HTML_ARGS}
10271029
--lua-filter=convert-diagrams.lua
1030+
--lua-filter=include-into-code-blocks.lua
10281031
--lua-filter=parse-html.lua
10291032
--lua-filter=apply-classes-to-tables.lua
10301033
--lua-filter=landscape-pages.lua

filter/divide-code-blocks.lua

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ function CodeBlock(block)
2828
-- all code blocks are rendered consistently.
2929
table.insert(block.classes, "_placeholder")
3030

31+
block.text = block.text:gsub("\r", "") -- Remove carriage-returns.
32+
3133
font = class_spec["font"]
3234
return {
3335
pandoc.RawInline('latex', string.format([[
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
-- Allow code blocks to include code from other files.
2+
3+
-- Patch the path to include the current script's directory.
4+
package.path = package.path .. ";" .. debug.getinfo(1).source:match("@?(.*/)") .. "?.lua"
5+
utils = require "utils"
6+
7+
function resolveIncludes(text)
8+
-- A hack to make the regex below work, since gsub can't match `^ | \n`
9+
if text:match("^!include ") then
10+
text = "\n" .. text
11+
end
12+
13+
return text:gsub("\n(!include )(%C+)", function(_, include_path)
14+
return utils.readFile(include_path)
15+
end)
16+
end
17+
18+
function CodeBlock(el)
19+
for i, class in ipairs(el.classes) do
20+
if class == 'include' then
21+
return pandoc.CodeBlock(resolveIncludes(el.text), el.attr)
22+
end
23+
end
24+
25+
return el
26+
end

guide.tcg

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -574,6 +574,23 @@ auctor lorem, ultricies vehicula orci aliquam sed. Praesent interdum leo metus,
574574
ut rhoncus sem consequat a.
575575
```
576576

577+
Code blocks can also include content from other files.
578+
579+
````md
580+
```include {.small}
581+
!include sample-text.inc
582+
```
583+
````
584+
585+
The result looks like this:
586+
587+
```include {.small}
588+
!include sample-text.inc
589+
```
590+
591+
An `include` block can contain multiple `!include` directives. Paths are resolved
592+
relative to the directory in which the build occurs.
593+
577594
## Footnotes {#sec:footnotes}
578595

579596
```md

sample-text.inc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
This file now can
2+
be included verbatim
3+
into the source spec.

0 commit comments

Comments
 (0)