Skip to content

Commit 41b7cf1

Browse files
committed
initial commit
0 parents  commit 41b7cf1

File tree

4 files changed

+136
-0
lines changed

4 files changed

+136
-0
lines changed

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
*.html
2+
*.pdf
3+
*_files/
4+
/.luarc.json

README.md

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
# Include Code Files Extension For Quarto
2+
3+
Filter to include code from source files.
4+
5+
The filter is largely inspired by
6+
[pandoc-include-code](https://github.com/owickstrom/pandoc-include-code).
7+
8+
## Installing
9+
10+
```bash
11+
quarto add quarto-ext/include-code-files
12+
```
13+
14+
This will install the extension under the `_extensions` subdirectory.
15+
If you're using version control, you will want to check in this directory.
16+
17+
## Usage
18+
19+
The filter recognizes code blocks with the `include` attribute present. It swaps the content of the code block with contents from a file.
20+
21+
Here is how you add the filter to a page (it can also be added to a `_quarto.yml` project file with the same syntax):
22+
23+
````markdown
24+
---
25+
title: "My Document"
26+
filters:
27+
- include-code-files
28+
---
29+
30+
````
31+
32+
### Including Files
33+
34+
Once adding the filter to you page or project, the simplest way to use the filter is the `include` attribute
35+
36+
```{include="script.py"}
37+
```
38+
39+
You can still use other attributes, and classes, to control the code blocks:
40+
41+
```{.c include="script.py" code-line-numbers="true"}
42+
```
43+
44+
### Ranges
45+
46+
If you want to include a specific range of lines, use `startLine` and `endLine`:
47+
48+
```{include="script.py" startLine=35 endLine=80}
49+
```
50+
51+
`start-line` and `end-line` alternatives are also recognized.
52+
53+
### Dedent
54+
55+
Using the `dedent` attribute, you can have whitespaces removed on each line, where possible (non-whitespace character will not be removed even if they occur
56+
in the dedent area).
57+
58+
```{include="script.py" dedent=4}
59+
```
60+
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
title: Include Code Files
2+
author: Bruno Beaufils
3+
version: 1.0.0
4+
quarto-required: ">=1.2"
5+
contributes:
6+
filters:
7+
- include-code-files.lua
8+
9+
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
--- include-code-files.lua – filter to include code from source files
2+
---
3+
--- Copyright: © 2020 Bruno BEAUFILS
4+
--- License: MIT – see LICENSE file for details
5+
6+
--- Dedent a line
7+
local function dedent (line, n)
8+
return line:sub(1,n):gsub(" ","") .. line:sub(n+1)
9+
end
10+
11+
--- Filter function for code blocks
12+
local function transclude (cb)
13+
if cb.attributes.include then
14+
local content = ""
15+
local fh = io.open(cb.attributes.include)
16+
if not fh then
17+
io.stderr:write("Cannot open file " .. cb.attributes.include .. " | Skipping includes\n")
18+
else
19+
local number = 1
20+
local start = 1
21+
22+
-- change hyphenated attributes to PascalCase
23+
for i,pascal in pairs({"startLine", "endLine"})
24+
do
25+
local hyphen = pascal:gsub("%u", "-%0"):lower()
26+
if cb.attributes[hyphen] then
27+
cb.attributes[pascal] = cb.attributes[hyphen]
28+
cb.attributes[hyphen] = nil
29+
end
30+
end
31+
32+
if cb.attributes.startLine then
33+
cb.attributes.startFrom = cb.attributes.startLine
34+
start = tonumber(cb.attributes.startLine)
35+
end
36+
for line in fh:lines ("L")
37+
do
38+
if cb.attributes.dedent then
39+
line = dedent(line, cb.attributes.dedent)
40+
end
41+
if number >= start then
42+
if not cb.attributes.endLine or number <= tonumber(cb.attributes.endLine) then
43+
content = content .. line
44+
end
45+
end
46+
number = number + 1
47+
end
48+
fh:close()
49+
end
50+
-- remove key-value pair for used keys
51+
cb.attributes.include = nil
52+
cb.attributes.startLine = nil
53+
cb.attributes.endLine = nil
54+
cb.attributes.dedent = nil
55+
-- return final code block
56+
return pandoc.CodeBlock(content, cb.attr)
57+
end
58+
end
59+
60+
return {
61+
{ CodeBlock = transclude }
62+
}
63+

0 commit comments

Comments
 (0)