Skip to content
This repository was archived by the owner on Apr 9, 2025. It is now read-only.

Commit 07e659f

Browse files
committed
Setup our own translator without override the existing one
Instead of fully override the translator set by the user (or another Sphinx extension), we create a new class dynamically by inheriting the translator that's defined and adding our own Mixin class with our custom behavior. This way, we are compatible with other extensions nicely. As a example, I tested this with pydata-sphinx-theme (https://github.com/pandas-dev/pydata-sphinx-theme) that defines a custom translator. Besides, this new approach is compatible with people using HTML5 translator by default since we are not forcing HTML4 when inheriting our own custom class anymore.
1 parent 1eea296 commit 07e659f

File tree

2 files changed

+33
-11
lines changed

2 files changed

+33
-11
lines changed

hoverxref/extension.py

+31-8
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
from . import version
88
from .domains import HoverXRefPythonDomain, HoverXRefStandardDomain
9-
from .translators import HoverXRefHTMLTranslator
9+
from .translators import HoverXRefHTMLTranslatorMixin
1010

1111
ASSETS_FILES = [
1212
'js/hoverxref.js_t', # ``_t`` tells Sphinx this is a template
@@ -83,6 +83,35 @@ def setup_sphinx_tabs(app, config):
8383
app.disconnect(listener_id)
8484

8585

86+
def setup_translators(app):
87+
"""
88+
Override translators respecting the one defined.
89+
90+
We create a new class by inheriting the Sphinx Translator already defined
91+
and our own ``HoverXRefHTMLTranslatorMixin`` that includes the logic to
92+
``_hoverxref`` attributes.
93+
94+
User's defined translator has to be added with ``override=True``. Otherwise,
95+
Sphinx fails with an error.
96+
"""
97+
for name, klass in app.registry.translators.items():
98+
# Read the Docs use ``readthedocs`` as the name of the build, so we need
99+
# to replace this as well
100+
if name not in ['html', 'readthedocs', 'readthedocsdirhtml']:
101+
# Skip translator that are not HTML
102+
continue
103+
104+
translator = type(
105+
'HoverXRefHTMLTranslator',
106+
(
107+
HoverXRefHTMLTranslatorMixin,
108+
klass,
109+
),
110+
{},
111+
)
112+
app.set_translator(name, translator, override=True)
113+
114+
86115
def setup(app):
87116
"""Setup ``hoverxref`` Sphinx extension."""
88117

@@ -109,13 +138,7 @@ def setup(app):
109138
app.add_config_value('hoverxref_tooltip_content', 'Loading...', 'env')
110139
app.add_config_value('hoverxref_tooltip_class', 'rst-content', 'env')
111140

112-
app.set_translator('html', HoverXRefHTMLTranslator, override=True)
113-
114-
# Read the Docs use ``readthedocs`` as the name of the build, so we need to
115-
# replace this as well
116-
app.set_translator('readthedocs', HoverXRefHTMLTranslator, override=True)
117-
app.set_translator('readthedocsdirhtml', HoverXRefHTMLTranslator, override=True)
118-
141+
app.connect('builder-inited', setup_translators)
119142
app.connect('config-inited', setup_domains)
120143
app.connect('config-inited', setup_sphinx_tabs)
121144
app.connect('build-finished', copy_asset_files)

hoverxref/translators.py

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
1-
from sphinx.writers.html import HTMLTranslator
21
from sphinx.util import logging
32

43
logger = logging.getLogger(__name__)
54

65

7-
class HoverXRefHTMLTranslator(HTMLTranslator):
6+
class HoverXRefHTMLTranslatorMixin:
87

98
"""
10-
Override ``HTMLTranslator`` to render extra data saved in reference nodes.
9+
Mixin ``HTMLTranslator`` to render extra data saved in reference nodes.
1110
1211
It adds all the values saved under ``_hoverxref`` as attributes of the HTML
1312
reference tag.

0 commit comments

Comments
 (0)