-
Notifications
You must be signed in to change notification settings - Fork 188
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add custom Sphinx extension for translating
image-sg
directives to …
…text
- Loading branch information
1 parent
2157354
commit 87c0a55
Showing
2 changed files
with
28 additions
and
2 deletions.
There are no files selected for viewing
This file contains 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 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,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), | ||
) |