-
-
Notifications
You must be signed in to change notification settings - Fork 2
feat: add mermaid diagram rendering via sphinxcontrib.mermaid #83
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
Open
illilillillili
wants to merge
2
commits into
atsphinx:main
Choose a base branch
from
illilillillili:feat/mermaid_support
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -60,6 +60,7 @@ dev = [ | |
| "doc8", | ||
| "esbonio", | ||
| "ruff", | ||
| "sphinxcontrib-mermaid>=2.0.2", | ||
| "ty", | ||
| ] | ||
| docs = [ | ||
|
|
||
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,65 @@ | ||
| # TODO: Write docstrings after. | ||
| # ruff: noqa: D101, D102, D106, D107 | ||
| from __future__ import annotations | ||
|
|
||
| from typing import TYPE_CHECKING | ||
|
|
||
| import pytest | ||
| from docutils import nodes | ||
|
|
||
| from atsphinx.typst import builders as t | ||
|
|
||
| if TYPE_CHECKING: | ||
| from pytest_mock import MockFixture | ||
| from sphinx.testing.util import SphinxTestApp | ||
|
|
||
|
|
||
| class Test_TypstTranslator: | ||
| class Test_visit_mermaid: | ||
| @pytest.mark.sphinx("typst", testroot="with-mermaid-nodep") | ||
| def test__no_sphinxcontrib_mermaid(self, app: SphinxTestApp): | ||
| """Build succeeds when sphinxcontrib.mermaid is not installed. | ||
|
|
||
| The ``with-mermaid-nodep`` testroot does not load ``sphinxcontrib.mermaid`` | ||
| so the ``.. mermaid::`` directive is unknown and produces no node - the | ||
| translator's ``visit_mermaid`` is never reached, but the build must still | ||
| complete and emit a ``.typ`` file. | ||
| """ | ||
| app.build() | ||
| out = app.outdir / "index.typ" | ||
| assert out.exists() | ||
|
|
||
| @pytest.mark.sphinx("typst", testroot="with-mermaid", freshenv=True) | ||
| def test__render_produces_image(self, app: SphinxTestApp, mocker: MockFixture): | ||
| """When render_mm succeeds the SVG is embedded as an image directive.""" | ||
| import atsphinx.typst.writer as w | ||
|
|
||
| fake_relfn = "_images/mermaid-abc123.svg" | ||
| fake_outfn = "/tmp/mermaid-abc123.svg" | ||
| mocker.patch.object( | ||
| w.TypstTranslator, | ||
| "mermaid_render_to_svg", | ||
| return_value=(fake_relfn, fake_outfn), | ||
| ) | ||
| app.build() | ||
| out = app.outdir / "index.typ" | ||
| assert out.exists() | ||
| content = out.read_text() | ||
| # The SVG path should appear in the generated Typst source | ||
| assert "mermaid-abc123.svg" in content | ||
|
|
||
| @pytest.mark.sphinx("typst", testroot="with-mermaid", freshenv=True) | ||
| def test__render_failure_is_skipped(self, app: SphinxTestApp, mocker: MockFixture): | ||
| """A render error is caught, logged as a warning, and build still succeeds.""" | ||
| import atsphinx.typst.writer as w | ||
|
|
||
| mocker.patch.object( | ||
| w.TypstTranslator, | ||
| "mermaid_render_to_svg", | ||
| side_effect=RuntimeError("mmdc crashed"), | ||
| ) | ||
| mock_logger = mocker.patch.object(w, "logger") | ||
| app.build() | ||
| out = app.outdir / "index.typ" | ||
| assert out.exists() | ||
| mock_logger.warning.assert_called_once() |
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,15 @@ | ||
|
|
||
| # noqa: D100 | ||
|
|
||
| extensions = [ | ||
| "atsphinx.typst", | ||
| ] | ||
|
|
||
| typst_documents = [ | ||
| { | ||
| "entrypoint": "index", | ||
| "filename": "index", | ||
| "theme": "manual", | ||
| "title": "Test documentation", | ||
| } | ||
| ] |
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,9 @@ | ||
| Test doc for atsphinx-typst | ||
| =========================== | ||
|
|
||
| .. mermaid:: | ||
|
|
||
| %%{init: {'theme':'base'}}%% | ||
| block | ||
| a | ||
| b |
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,16 @@ | ||
|
|
||
| # noqa: D100 | ||
|
|
||
| extensions = [ | ||
| "sphinxcontrib.mermaid", | ||
| "atsphinx.typst", | ||
| ] | ||
|
|
||
| typst_documents = [ | ||
| { | ||
| "entrypoint": "index", | ||
| "filename": "index", | ||
| "theme": "manual", | ||
| "title": "Test documentation", | ||
| } | ||
| ] |
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,9 @@ | ||
| Test doc for atsphinx-typst | ||
| =========================== | ||
|
|
||
| .. mermaid:: | ||
|
|
||
| %%{init: {'theme':'base'}}%% | ||
| block | ||
| a | ||
| b |
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.
Uh oh!
There was an error while loading. Please reload this page.