-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Add support for Graphviz diagrams #2052
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
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
cda28bd
Add support for Graphviz dot diagrams
dscho a7cac47
graphviz: use <img> elements instead of <svg> ones
dscho f2d5b6f
graphviz: make the diagrams' background transparent by default
dscho d9f9c5b
graphviz: allow overriding the engine
dscho e458803
graphviz: optionally pre-generate the SVGs
dscho 66d59f7
graphviz: support the `alt` attribute
dscho 4f58ebc
deploy/ci: pre-render the Graphviz diagrams
dscho 352dbbf
Replace the diagrams in `about/distributed` with Graphviz versions
dscho File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| --- | ||
To1ne marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| outputs: | ||
| - json | ||
| --- | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| {{- $diagram_list := where .Site.Pages "RawContent" "like" "{{< graphviz" -}} | ||
| {{- $diagram_list | jsonify -}} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| <pre class="graphviz"{{ if (ne "" (.Get "engine")) }} engine="{{(.Get "engine")}}"{{ end }}{{ if (ne "" (.Get "alt")) }} alt="{{ (.Get "alt") }}"{{ end }}> | ||
| {{ .Inner | htmlEscape | safeHTML }} | ||
| </pre> | ||
| {{ .Page.Store.Set "hasGraphviz" true }} | ||
To1ne marked this conversation as resolved.
Show resolved
Hide resolved
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| #!/usr/bin/env node | ||
|
|
||
| import { readFileSync, writeFileSync } from "node:fs" | ||
| import { parse } from "node-html-parser" | ||
| import Viz from "../static/js/viz-global.js" | ||
|
|
||
| /* | ||
| * This script post-processes the site as generated via Hugo, replacing the | ||
| * `<pre class="graphviz">` elements with inline `<svg>` ones, pre-rendered | ||
| * via `viz-js`. | ||
| */ | ||
| ;(async () => { | ||
| for (const { Path: pathInPublic } of JSON.parse(readFileSync("public/diagram-list.json", "utf-8"))) { | ||
| const path = `public${pathInPublic}.html` | ||
| const contents = readFileSync(path, "utf-8") | ||
| const html = parse(contents) | ||
| const vizImport = html.querySelector('script[src$="viz-global.js"]') | ||
| if (!vizImport) { | ||
To1ne marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| console.error(`No 'viz-global.js' import found in ${path}; skipping`) | ||
| continue | ||
| } | ||
| vizImport.nextElementSibling.remove() // remove the inline script | ||
| vizImport.remove() // remove the import | ||
|
|
||
| for (const pre of html.querySelectorAll("pre.graphviz")) { | ||
| const engine = pre.getAttribute("engine") || "dot" | ||
| const svg = (await Viz.instance()).renderString(pre.textContent, { | ||
| format: "svg", | ||
| graphAttributes: { | ||
| bgcolor: "transparent", | ||
| }, | ||
| engine, | ||
| }) | ||
| const alt = pre.getAttribute("alt") | ||
| const altAttr = !alt ? '' : ` alt='${alt.replaceAll("&", "&").replaceAll("'", "'")}'` | ||
| const dataURL = `data:image/svg+xml;base64,${Buffer.from(svg).toString("base64")}` | ||
| pre.replaceWith(`<img${altAttr} src="${dataURL}" />`) | ||
| } | ||
| console.log(`Rewriting ${path}`) | ||
| writeFileSync(`${path}`, html.toString()) | ||
| } | ||
| })().catch((e) => { | ||
| console.error(e) | ||
| process.exitCode = 1 | ||
| }) | ||
Large diffs are not rendered by default.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
From the commit message:
@dscho That no longer seems to be the case now
<svg>is no longer is inlined. It's a nitpick on the commit message, I don't consider this an issue that it's not the case. Do you think it worthy to edit the commit message?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
True. I force-pushed a fix:
Range-diff
1: a2c59d6 ! 1: 352dbbf Replace the diagrams in
about/distributedwith Graphviz versions@@ Commit message Replace the diagrams in `about/distributed` with Graphviz versions This commit recreates those diagrams that have been provided as `.png` - files before. - - With this change, the text in those diagrams is copy/paste-able. But of - course that's not the real reason I made this change, the real reason is + files before. Of course, this is not strictly necessary because the + previous `.png` diagrams served their purpose well enough. But I wanted to show off that we can now use Graphviz diagrams on git-scm.com. + Note: When developing this patch, I had considered inlining the SVGs as + `<svg>` instead of `<img>`. That would have had the following + advantages: + + - The text in those diagrams would have been copy/paste-able. + + - We could have defined explicit colors for dark mode, as the `<style>` + element would have been able to refer to the HTML document's `:root` + element's dark mode definitions. + + However, having SVGs embedded in `<img>` elements have a few benefits: + + - Current browsers allow to `Open Image in New Tab` (where the text + would be copy/paste-able again). + + - Current browsers allow to `Save Image` (even if there is no way to + specify a filename, see https://github.com/whatwg/html/issues/2722). + + - By using `<img>` elements, we can reuse the previous work on images in + dark mode. + Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> ## content/about/distributed.html ##