diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index ac493a7..474916c 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -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: diff --git a/README.md b/README.md index 3b18348..e7316de 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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 @@ -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 -------------- diff --git a/_extensions/diagram/diagram.lua b/_extensions/diagram/diagram.lua index 37f4a62..0223bcb 100644 --- a/_extensions/diagram/diagram.lua +++ b/_extensions/diagram/diagram.lua @@ -291,6 +291,35 @@ 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, @@ -298,6 +327,7 @@ local default_engines = { plantuml = plantuml, tikz = tikz, cetz = cetz, + d2 = d2, } -- diff --git a/test/expected-d2.html b/test/expected-d2.html new file mode 100644 index 0000000..0a202d8 --- /dev/null +++ b/test/expected-d2.html @@ -0,0 +1,6 @@ +
D2 is a Go-based diagramming and charting tool.
+