Skip to content

Commit

Permalink
Add custom Sphinx extension for translating image-sg directives to …
Browse files Browse the repository at this point in the history
…text
  • Loading branch information
Mandrenkov committed Oct 2, 2023
1 parent 2157354 commit 87c0a55
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
4 changes: 2 additions & 2 deletions conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@
"sphinx.ext.intersphinx",
"sphinx.ext.mathjax",
"sphinx.ext.ifconfig",
"sphinx_gallery.gen_gallery"
"sphinx_gallery.gen_gallery",
"extension",
]


Expand Down Expand Up @@ -212,4 +213,3 @@

# Example configuration for intersphinx: refer to the Python standard library.
intersphinx_mapping = {"https://docs.pennylane.ai/en/stable/": None}

26 changes: 26 additions & 0 deletions extension.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
"""
Custom Sphinx extension that adds text translation support for the ``image-sg``
directive from Sphinx Gallery.
"""

from sphinx.errors import ExtensionError
from sphinx.util.docutils import is_node_registered
from sphinx_gallery.directives import imgsgnode

def visit_imgsg_text(self, node):
"""Visit function for ``imgsgnode`` nodes."""
self.visit_image(node)

def depart_imgsg_text(self, node):
"""Departure function for ``imgsgnode`` nodes."""
self.depart_image(node)

def setup(app):
"""Entrypoint for the extension."""
if not is_node_registered(imgsgnode):
raise ExtensionError("Sphinx Gallery extension must be loaded first.")

app.registry.add_translation_handlers(
imgsgnode,
text=(visit_imgsg_text, depart_imgsg_text),
)

0 comments on commit 87c0a55

Please sign in to comment.