Skip to content

Commit 7ab8160

Browse files
committed
feature to write source code to txt file
1 parent f840962 commit 7ab8160

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

_extensions/code-appendix/code-appendix.lua

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ function CodeBlock(el)
1515
end
1616

1717
-- reads yaml to obtain desired header for code appendix
18+
-- writes appendix to txt file if desired
1819
function Meta(m)
1920

2021
if m["code-appendix-title"] ~= nil then
@@ -29,6 +30,21 @@ function Meta(m)
2930
codeAppendixHeaderLevel = 1
3031
end
3132

33+
-- write appendix to text file
34+
if m["code-appendix-write"] ~= nil then
35+
36+
codeAppendixWrite = pandoc.utils.stringify(m["code-appendix-write"])
37+
38+
if codeAppendixWrite == "true" then
39+
local f = io.open(pandoc.system.get_working_directory() .. '/code-appendix.txt', 'w')
40+
for index, value in ipairs(pandoc.List(appendix)) do
41+
f:write(pandoc.List(appendix)[index].text .. "\n\n")
42+
end
43+
f:close()
44+
end
45+
46+
end
47+
3248
return m
3349

3450
end

example.qmd

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ filters:
55
- code-appendix
66
code-appendix-title: "Source Code:"
77
code-appendix-header-level: 2
8+
code-appendix-write: false
89
---
910

1011
This filter moves the source code generating the following text
@@ -25,6 +26,7 @@ plot(mtcars[,c("mpg", "cyl")])
2526
to an appendix at the end of the document, (but keeps the output in its usual place).
2627
Force a page break in the `.qmd` to display appendix on separate page. Note that
2728
you can change the appendix header using the `code-appendix-title` and
28-
`code-appendix-header-level` options in the document YAML.
29+
`code-appendix-header-level` options in the document YAML. Set
30+
`code-appendix-write: true` to print source code to a `txt` file.
2931

3032
{{< pagebreak >}}

0 commit comments

Comments
 (0)