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

Commit 15671c8

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 15671c8

File tree

2 files changed

+43
-11
lines changed

2 files changed

+43
-11
lines changed

hoverxref/extension.py

+41-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,44 @@ 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 (if any).
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+
if not app.registry.translators.items():
96+
translator = types.new_class(
97+
'HoverXRefHTMLTranslator',
98+
(
99+
HoverXRefHTMLTranslatorMixin,
100+
app.builder.default_translator_class,
101+
),
102+
{},
103+
)
104+
app.set_translator(app.builder.name, translator, override=True)
105+
else:
106+
for name, klass in app.registry.translators.items():
107+
# Read the Docs use ``readthedocs`` as the name of the build, so we
108+
# need to replace this as well
109+
if name not in ['html', 'readthedocs', 'readthedocsdirhtml']:
110+
# Skip translator that are not HTML
111+
continue
112+
113+
translator = types.new_class(
114+
'HoverXRefHTMLTranslator',
115+
(
116+
HoverXRefHTMLTranslatorMixin,
117+
klass,
118+
),
119+
{},
120+
)
121+
app.set_translator(name, translator, override=True)
122+
123+
124+
86125
def setup(app):
87126
"""Setup ``hoverxref`` Sphinx extension."""
88127

@@ -109,13 +148,7 @@ def setup(app):
109148
app.add_config_value('hoverxref_tooltip_content', 'Loading...', 'env')
110149
app.add_config_value('hoverxref_tooltip_class', 'rst-content', 'env')
111150

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-
151+
app.connect('builder-inited', setup_translators)
119152
app.connect('config-inited', setup_domains)
120153
app.connect('config-inited', setup_sphinx_tabs)
121154
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)