Skip to content
Merged
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
21 changes: 21 additions & 0 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,27 @@ jobs:
- name: Render
run: make PANDOC=/opt/quarto/bin/tools/x86_64/pandoc test-cetz

D2:
runs-on: ubuntu-latest
strategy:
fail-fast: true
container:
image: ghcr.io/quarto-dev/quarto:latest

steps:
- name: Checkout
uses: actions/checkout@v4

- name: Install dependencies
run: |
apt-get -q --no-allow-insecure-repositories update && \
apt-get install --no-install-recommends --assume-yes \
ca-certificates curl make && \
curl -fsSL https://d2lang.com/install.sh | sh -s --

- name: Render
run: make PANDOC=/opt/quarto/bin/tools/x86_64/pandoc test-d2

Quarto:
runs-on: ubuntu-latest
strategy:
Expand Down
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Diagram Generator
This Lua filter is used to create figures from code blocks: images
are generated from the code with the help of external programs.
The filter processes diagram code for Asymptote, Graphviz,
Mermaid, PlantUML, and Ti*k*Z.
Mermaid, PlantUML, Ti*k*Z, and D2.


Usage
Expand Down Expand Up @@ -103,6 +103,7 @@ that can be used to specify a specific executable.
| [PlantUML] | `plantuml` | `plantuml` | `PLANTUML_BIN` |
| [Ti*k*Z] | `tikz` | `pdflatex` | `PDFLATEX_BIN` |
| [cetz] | `cetz` | `typst` | `TYPST_BIN` |
| [D2] | `d2` | `d2` | `D2_BIN` |

### Other diagram engines

Expand All @@ -115,6 +116,7 @@ The filter can be extended with local packages; see
[PlantUML]: https://plantuml.com/
[Ti*k*Z]: https://github.com/pgf-tikz/pgf
[Cetz]: https://github.com/cetz-package/cetz
[D2]: https://d2lang.com/

Figure options
--------------
Expand Down
30 changes: 30 additions & 0 deletions _extensions/diagram/diagram.lua
Original file line number Diff line number Diff line change
Expand Up @@ -291,13 +291,43 @@ local cetz = {
end,
}

--- D2 engine for the D2 language
local d2 = {
line_comment_start = '#',
mime_types = mime_types_set{'png', 'svg'},

compile = function (self, code, user_opts)
return with_temporary_directory('diagram', function (tmpdir)
return with_working_directory(tmpdir, function ()
-- D2 format identifiers correspond to common file extensions.
local mime_type = self.mime_type or 'image/svg+xml'
local file_extension = extension_for_mimetype[mime_type]
local infile = 'diagram.d2'
local outfile = 'diagram.' .. file_extension

args = {'--bundle', '--pad=0', '--scale=1'}

table.insert(args, infile)
table.insert(args, outfile)

write_file(infile, code)

pipe(self.execpath or 'd2', args, '')

return read_file(outfile), mime_type
end)
end)
end,
}

local default_engines = {
asymptote = asymptote,
dot = graphviz,
mermaid = mermaid,
plantuml = plantuml,
tikz = tikz,
cetz = cetz,
d2 = d2,
}

--
Expand Down
6 changes: 6 additions & 0 deletions test/expected-d2.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<h3 id="d2">D2</h3>
<p>D2 is a Go-based diagramming and charting tool.</p>
<figure>
<img src="hello-world.svg" alt="Hello, World!" />
<figcaption aria-hidden="true"><p>Hello, World!</p></figcaption>
</figure>
7 changes: 7 additions & 0 deletions test/input-d2.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
### D2

D2 is a Go-based diagramming and charting tool.

```{.d2 caption="Hello, World!" filename="hello-world.svg"}
x -> y: hello world
```
10 changes: 10 additions & 0 deletions test/test-d2.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
input-files: ['test/input-d2.md']
filters:
- diagram.lua
to: html
metadata:
pagetitle: D2 diagram
diagram:
cache: false
variables:
document-css: false