Skip to content

Commit f840962

Browse files
committed
appendix header customizability
1 parent f8aacc6 commit f840962

File tree

2 files changed

+29
-11
lines changed

2 files changed

+29
-11
lines changed

_extensions/code-appendix/code-appendix.lua

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,26 +6,40 @@ appendix = {}
66
function CodeBlock(el)
77

88
if el.classes[2] == "cell-code" then
9-
109
table.insert(appendix, el)
1110
return {}
12-
1311
else
14-
1512
return el
13+
end
14+
15+
end
1616

17+
-- reads yaml to obtain desired header for code appendix
18+
function Meta(m)
19+
20+
if m["code-appendix-title"] ~= nil then
21+
codeAppendixTitle = pandoc.utils.stringify(m["code-appendix-title"])
22+
else
23+
codeAppendixTitle = pandoc.Str("Code")
24+
end
25+
26+
if m["code-appendix-header-level"] ~= nil then
27+
codeAppendixHeaderLevel = tonumber(pandoc.utils.stringify(m["code-appendix-header-level"]))
28+
else
29+
codeAppendixHeaderLevel = 1
1730
end
1831

32+
return m
33+
1934
end
2035

2136
-- append pandoc document with source code
22-
function Pandoc(el)
37+
function Pandoc(doc)
2338

24-
appendixHeader = pandoc.Header(1, pandoc.Str "test")
25-
el.blocks:extend(pandoc.List({appendixHeader}))
26-
el.blocks:extend(pandoc.List(appendix))
39+
appendixHeader = pandoc.Header(codeAppendixHeaderLevel, codeAppendixTitle)
40+
doc.blocks:extend(pandoc.List({appendixHeader}))
41+
doc.blocks:extend(pandoc.List(appendix))
2742

28-
return el
43+
return doc
2944

3045
end
31-

example.qmd

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
---
22
title: "Code-appendix Example"
3-
format: pdf
3+
format: typst
44
filters:
55
- code-appendix
6+
code-appendix-title: "Source Code:"
7+
code-appendix-header-level: 2
68
---
79

810
This filter moves the source code generating the following text
@@ -21,6 +23,8 @@ plot(mtcars[,c("mpg", "cyl")])
2123

2224

2325
to an appendix at the end of the document, (but keeps the output in its usual place).
24-
Force a page break in the `.qmd` to display appendix on separate page.
26+
Force a page break in the `.qmd` to display appendix on separate page. Note that
27+
you can change the appendix header using the `code-appendix-title` and
28+
`code-appendix-header-level` options in the document YAML.
2529

2630
{{< pagebreak >}}

0 commit comments

Comments
 (0)