Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

include-files: add support for inclusion of source code block (extension specific PR) #129

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions include-files/Makefile
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
DIFF ?= diff --strip-trailing-cr -u

test: sample.md file-a.md file-b.md file-c.md include-files.lua
test: sample.md file-a.md file-b.md file-c.md shell1 shell2 include-files.lua
@pandoc --lua-filter=include-files.lua --to=native $< \
| $(DIFF) expected.native -
@pandoc --lua-filter=include-files.lua -M include-auto --to=native $< \
| $(DIFF) expected-auto.native -

expected.native: sample.md file-a.md file-b.md file-c.md include-files.lua
expected.native: sample.md file-a.md file-b.md file-c.md shell1 shell2 include-files.lua
pandoc --lua-filter=include-files.lua --output $@ $<

expected-auto.native: sample.md file-a.md file-b.md file-c.md include-files.lua
Expand Down
13 changes: 13 additions & 0 deletions include-files/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ document.

Metadata from included files is discarded.

Add class `source` to include files as normal code blocks taken from
files instead of text in same format as the input.

### Shifting Headings

The default is to include the subdocuments unchanged, but it can
Expand Down Expand Up @@ -59,6 +62,9 @@ will want to include files written in a different format. An
alternative format can be specified via the `format` attribute.
Only plain-text formats are accepted.

In order to include source code block from files use additional class
`source` instead of trying the language as `format`.

### Recursive transclusion

Included files can in turn include other files. Note that all
Expand Down Expand Up @@ -101,6 +107,13 @@ some additional information in the main file `main.md`:
appendix/questionaire.md
```

## Source code

``` {.include .source .lua}
// code of this nice filter
include-files.lua
```

An HTML can be produced with this command:

pandoc --lua-filter=include-files.lua main.md --output result.html
2 changes: 2 additions & 0 deletions include-files/expected-auto.native
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
,Para [Str "This",Space,Str "is",Space,Code ("",[],[]) "file-a.md",Str "."]
,Header 3 ("title-of-file-a",[],[]) [Str "Title",Space,Str "of",Space,Str "file-a"]
,Para [Str "This",Space,Str "is",Space,Code ("",[],[]) "file-a.md",Str "."]
,Header 1 ("source-code",[],[]) [Str "Source",Space,Str "code"]
,CodeBlock ("",["include","source","bash"],[]) "#!/bin/bash\n\necho \"Hello World !\"\n\n#!/bin/bash\nfor i in \"Hello \" \"World \" \"!\\n\" \ndo\n\tprintf \"$i\"\ndone\n\n"
,Header 1 ("appendix",[],[]) [Str "Appendix"]
,Para [Str "More",Space,Str "info",Space,Str "goes",Space,Str "here."]
,Header 2 ("questionaire",[],[]) [Str "Questionaire"]
Expand Down
2 changes: 2 additions & 0 deletions include-files/expected.native
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
,Para [Str "This",Space,Str "is",Space,Code ("",[],[]) "file-a.md",Str "."]
,Header 2 ("title-of-file-a",[],[]) [Str "Title",Space,Str "of",Space,Str "file-a"]
,Para [Str "This",Space,Str "is",Space,Code ("",[],[]) "file-a.md",Str "."]
,Header 1 ("source-code",[],[]) [Str "Source",Space,Str "code"]
,CodeBlock ("",["include","source","bash"],[]) "#!/bin/bash\n\necho \"Hello World !\"\n\n#!/bin/bash\nfor i in \"Hello \" \"World \" \"!\\n\" \ndo\n\tprintf \"$i\"\ndone\n\n"
,Header 1 ("appendix",[],[]) [Str "Appendix"]
,Para [Str "More",Space,Str "info",Space,Str "goes",Space,Str "here."]
,Header 2 ("questionaire",[],[]) [Str "Questionaire"]
Expand Down
18 changes: 17 additions & 1 deletion include-files/include-files.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
--- include-files.lua – filter to include Markdown files
--- include-files.lua – filter to include files
---
--- Copyright: © 2019–2020 Albert Krewinkel
--- License: MIT – see LICENSE file for details
Expand Down Expand Up @@ -43,6 +43,22 @@ function transclude (cb)
if not cb.classes:includes 'include' then
return
end
-- process files as source code blocks
if cb.classes:includes 'source' then
local contents = ""
for line in cb.text:gmatch('[^\n]+') do
if line:sub(1,2) ~= '//' then
local fh = io.open(line)
if not fh then
io.stderr:write("Cannot open file " .. line .. " | Skipping includes\n")
else
contents = contents .. fh:read('*a')
fh:close()
end
end
end
return pandoc.CodeBlock(contents, cb.attr)
end

-- Markdown is used if this is nil.
local format = cb.attributes['format']
Expand Down
8 changes: 8 additions & 0 deletions include-files/sample.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,14 @@ file-d.org
file-f.md
```

# Source code

``` {.include .source .bash}
// this will include simple bash files
shell1
shell2
```

# Appendix

More info goes here.
Expand Down
4 changes: 4 additions & 0 deletions include-files/shell1
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/bin/bash

echo "Hello World !"

6 changes: 6 additions & 0 deletions include-files/shell2
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/bin/bash
for i in "Hello " "World " "!\n"
do
printf "$i"
done