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

Commit 8563b2a

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 8563b2a

File tree

2 files changed

+46
-11
lines changed

2 files changed

+46
-11
lines changed

hoverxref/extension.py

+44-8
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
import os
22
import inspect
3+
import types
34
from docutils import nodes
45
from sphinx.roles import XRefRole
56
from sphinx.util.fileutil import copy_asset
67

78
from . import version
89
from .domains import HoverXRefPythonDomain, HoverXRefStandardDomain
9-
from .translators import HoverXRefHTMLTranslator
10+
from .translators import HoverXRefHTMLTranslatorMixin
1011

1112
ASSETS_FILES = [
1213
'js/hoverxref.js_t', # ``_t`` tells Sphinx this is a template
@@ -83,6 +84,47 @@ def setup_sphinx_tabs(app, config):
8384
app.disconnect(listener_id)
8485

8586

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

@@ -109,13 +151,7 @@ def setup(app):
109151
app.add_config_value('hoverxref_tooltip_content', 'Loading...', 'env')
110152
app.add_config_value('hoverxref_tooltip_class', 'rst-content', 'env')
111153

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-
154+
app.connect('builder-inited', setup_translators)
119155
app.connect('config-inited', setup_domains)
120156
app.connect('config-inited', setup_sphinx_tabs)
121157
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)