Skip to content

Commit

Permalink
Fix generation of images for documents in subdirectories.
Browse files Browse the repository at this point in the history
  • Loading branch information
whitequark committed Apr 7, 2024
1 parent 732049a commit 078551f
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 3 deletions.
15 changes: 12 additions & 3 deletions sphinxcontrib/yowasp_wavedrom.py
@@ -1,9 +1,10 @@
import re
import json
from pathlib import Path
from pathlib import Path, PosixPath
from docutils.parsers.rst import Directive
from docutils import nodes
import sphinx.application
import sphinx.writers.html5
import yowasp_wavedrom


Expand Down Expand Up @@ -40,7 +41,7 @@ class wavedrom_diagram(nodes.General, nodes.Inline, nodes.Element):
pass


def html_visit_wavedrom_diagram(self, node):
def html_visit_wavedrom_diagram(self: sphinx.writers.html5.HTML5Translator, node: wavedrom_diagram):
basename: str = node["name"]
wavedrom_loc: str = node["loc"]
wavedrom_src: dict = node["src"]
Expand All @@ -65,7 +66,15 @@ def html_visit_wavedrom_diagram(self, node):
# significantly different behavior: duplicate IDs result in broken rendering, text can be
# selected, media queries can't be overridden with a `color-scheme` CSS attribute for themes
# that have a dark/light toggle via JS, etc.
pathname = Path(self.builder.outdir) / self.builder.imagedir / f'{basename}.svg'
pathname = Path(self.builder.outdir).joinpath(
# Note that for documents in subdirectories, the image directory is placed within that
# subdirectory. The other option would be to use enough `../` to locate the top-level
# image directory; using leading `/` in the `<img>` tag isn't feasible since that would
# break on `file:///` URLs.
PosixPath(self.builder.current_docname).parent,
self.builder.imagedir,
f'{basename}.svg'
)
pathname.parent.mkdir(parents=True, exist_ok=True)
pathname.write_text(wavedrom_svg)

Expand Down
7 changes: 7 additions & 0 deletions test/index.rst
Expand Up @@ -84,3 +84,10 @@ Circuit diagrams
]
]
}
Subdocument
-----------

.. toctree::

subdir/index
25 changes: 25 additions & 0 deletions test/subdir/index.rst
@@ -0,0 +1,25 @@
Subdirectory example
--------------------

..
Name intentionally conflicts with the one from a level above.
.. wavedrom:: skin_default

{
"signal": [
{"name": "clk",
"wave": "0P............"},
{"name": "rd_port.addr",
"wave": "==============",
"data": [0,1,2,3,4,5,6,7,8,9,10,11,0,1],
"node": ".a"},
{"name": "rd_port.data",
"wave": "==============",
"data": ["H","e","l","l","o"," ","w","o","r","l","d","\\n","H","e"],
"node": ".d"}
],
"edge": [
"a-|d"
]
}

0 comments on commit 078551f

Please sign in to comment.