Skip to content
Snippets Groups Projects
Unverified Commit e6b3142a authored by Bruno BEAUFILS's avatar Bruno BEAUFILS Committed by GitHub
Browse files

Merge pull request #1 from b3/samedwardes/quarto

Quarto support review and updates
parents 633e84b5 e9db0fa6
No related branches found
No related tags found
No related merge requests found
/_site/
/.luarc.json
......@@ -11,6 +11,57 @@ This repository exists thanks to the work of [Albert
KREWINKEL](https://github.com/tarleb/) among other things in
[pandoc/lua-filters](https://github.com/pandoc/lua-filters/issues/207).
## Quick Start
The `include-code-files` filter supports pandoc and quarto.
### Pandoc
Install the extension by cloning this repository:
```bash
git clone https://github.com/b3/include-code-files.git
```
You can then use the extension in any pandoc file:
``````markdown
```{include="hello.c"}
```
``````
When rendering include the path to `include-code-files.lua`
```bash
pandoc -s --lua-filter=include-code-files.lua test/input.md --output test/output.html
```
For complete details see the [Lua filter for pandoc](#lua-filter-for-pandoc) section below.
### Quarto
Install the extension:
```bash
quarto add b3/include-code-files@quarto
```
Use the extension in any `.qmd` file by including the filter in the YAML front matter.
``````markdown
---
filters:
- include-code-files
---
```{.python include="_snippets/hello_world.py"}
```
``````
For complete details see the [Extension for Quarto](#extension-for-quarto) section below.
## Lua filter for pandoc
### Installing
......@@ -78,13 +129,13 @@ An HTML version of [input.md](test/input.md) can be produced as
## Extension for Quarto
![Screenshot of an example output using include-code-files](docs/example-screenshot.png)
![Screenshot of an example output using include-code-files](examples/quarto/screenshot.png)
### Installing
```bash
quarto add SamEdwardes/include-code-files
quarto add b3/include-code-files
```
This will install the extension under the `_extensions`
......@@ -131,6 +182,6 @@ You can combine this with other quarto attributes like `filename` or `code-line-
### Example
Here is the source code for a minimal example:
[example.qmd](docs/example.qmd). See a rendered sample here:
[examples/quarto/index.qmd](examples/quarto/index.qmd). See a rendered sample here:
<https://samedwardes.quarto.pub/example-for-include-code-files-filter/>.
name: include-code-files
author: Sam Edwardes
author: Bruno Beaufils, Sam Edwardes
version: 1.0.0
quarto-required: ">=1.2.0"
contributes:
......
index.html
index_files/*
name: include-code-files
author: Sam Edwardes
version: 1.0.0
quarto-required: ">=1.2.0"
contributes:
filters:
- include-code-files.lua
--- include-code-files.lua – filter to include code from source files
---
--- Copyright: © 2020 Bruno BEAUFILS
--- License: MIT – see LICENSE file for details
--- Dedent a line
local function dedent (line, n)
return line:sub(1,n):gsub(" ","") .. line:sub(n+1)
end
--- Filter function for code blocks
local function transclude (cb)
if cb.attributes.include then
local content = ""
local fh = io.open(cb.attributes.include)
if not fh then
io.stderr:write("Cannot open file " .. cb.attributes.include .. " | Skipping includes\n")
else
local number = 1
local start = 1
-- change hyphenated attributes to PascalCase
for i,pascal in pairs({"startLine", "endLine"})
do
local hyphen = pascal:gsub("%u", "-%0"):lower()
if cb.attributes[hyphen] then
cb.attributes[pascal] = cb.attributes[hyphen]
cb.attributes[hyphen] = nil
end
end
if cb.attributes.startLine then
cb.attributes.startFrom = cb.attributes.startLine
start = tonumber(cb.attributes.startLine)
end
for line in fh:lines ("L")
do
if cb.attributes.dedent then
line = dedent(line, cb.attributes.dedent)
end
if number >= start then
if not cb.attributes.endLine or number <= tonumber(cb.attributes.endLine) then
content = content .. line
end
end
number = number + 1
end
fh:close()
end
-- remove key-value pair for used keys
cb.attributes.include = nil
cb.attributes.startLine = nil
cb.attributes.endLine = nil
cb.attributes.dedent = nil
-- return final code block
return pandoc.CodeBlock(content, cb.attr)
end
end
return {
{ CodeBlock = transclude }
}
- source: example.qmd
- source: index.qmd
quarto-pub:
- id: 509ab2f5-de97-45f6-b5e8-bb1304c1797d
url: 'https://quartopub.com/sites/samedwardes/example-for-include-code-files-filter'
File moved
File moved
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment