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

Commit 1d9ecc2

Browse files
committed
Setup our own domains without override the existing one
Follow the same pattern for translators (#42) for domains. Closes #14
1 parent 1eea296 commit 1d9ecc2

File tree

6 files changed

+37
-14
lines changed

6 files changed

+37
-14
lines changed

docs/usage.rst

+2-2
Original file line numberDiff line numberDiff line change
@@ -68,11 +68,11 @@ An example using Python Domain would be like:
6868

6969
.. code-block:: rst
7070
71-
:py:class:`hoverxref.domains.HoverXRefStandardDomain`
71+
:py:class:`hoverxref.domains.HoverXRefStandardDomainMixin`
7272
7373
That will render to:
7474

75-
:py:class:`hoverxref.domains.HoverXRefStandardDomain`
75+
:py:class:`hoverxref.domains.HoverXRefStandardDomainMixin`
7676

7777

7878
To enable ``hoverxref`` on a domain, you need to use the config :confval:`hoverxref_domains`

hoverxref/domains.py

+3-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
from sphinx.domains.python import PythonDomain
2-
from sphinx.domains.std import StandardDomain
31
from sphinx.util import logging
42
from .utils import get_ref_xref_data, get_ref_obj_data
53

@@ -26,7 +24,7 @@ def _inject_hoverxref_data(self, env, refnode, docname, labelid):
2624
}
2725

2826

29-
class HoverXRefPythonDomain(HoverXRefBaseDomain, PythonDomain):
27+
class HoverXRefPythonDomainMixin(HoverXRefBaseDomain):
3028

3129
def resolve_xref(self, env, fromdocname, builder, type, target, node, contnode):
3230
refnode = super().resolve_xref(env, fromdocname, builder, type, target, node, contnode)
@@ -51,9 +49,9 @@ def resolve_xref(self, env, fromdocname, builder, type, target, node, contnode):
5149
return refnode
5250

5351

54-
class HoverXRefStandardDomain(HoverXRefBaseDomain, StandardDomain):
52+
class HoverXRefStandardDomainMixin(HoverXRefBaseDomain):
5553
"""
56-
Override ``StandardDomain`` to save the values after the xref resolution.
54+
Mixin for ``StandardDomain`` to save the values after the xref resolution.
5755
5856
``:ref:`` are treating as a different node in Sphinx
5957
(``sphinx.addnodes.pending_xref``). These nodes are translated to regular

hoverxref/extension.py

+29-4
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
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
8-
from .domains import HoverXRefPythonDomain, HoverXRefStandardDomain
9+
from .domains import HoverXRefPythonDomainMixin, HoverXRefStandardDomainMixin
910
from .translators import HoverXRefHTMLTranslator
1011

1112
ASSETS_FILES = [
@@ -53,6 +54,13 @@ def copy_asset_files(app, exception):
5354

5455

5556
def setup_domains(app, config):
57+
"""
58+
Override domains respecting the one defined (if any).
59+
60+
We create a new class by inheriting the Sphinx Domain already defined
61+
and our own ``HoverXRef...DomainMixin`` that includes the logic for
62+
``_hoverxref`` attributes.
63+
"""
5664
# Add ``hoverxref`` role replicating the behavior of ``ref``
5765
app.add_role_to_domain(
5866
'std',
@@ -63,10 +71,27 @@ def setup_domains(app, config):
6371
warn_dangling=True,
6472
),
6573
)
66-
app.add_domain(HoverXRefStandardDomain, override=True)
6774

68-
if 'py' in config.hoverxref_domains:
69-
app.add_domain(HoverXRefPythonDomain, override=True)
75+
domain = types.new_class(
76+
'HoverXRefStandardDomain',
77+
(
78+
HoverXRefStandardDomainMixin,
79+
app.registry.domains.get('std'),
80+
),
81+
{}
82+
)
83+
app.add_domain(domain, override=True)
84+
85+
if 'py' in app.config.hoverxref_domains:
86+
domain = types.new_class(
87+
'HoverXRefPythonDomain',
88+
(
89+
HoverXRefPythonDomainMixin,
90+
app.registry.domains.get('py'),
91+
),
92+
{}
93+
)
94+
app.add_domain(domain, override=True)
7095

7196

7297
def setup_sphinx_tabs(app, config):

tests/examples/python-domain/api.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
API
44
===
55

6-
.. autoclass:: hoverxref.extension.HoverXRefStandardDomain
6+
.. autoclass:: hoverxref.extension.HoverXRefStandardDomainMixin
77

88

99
.. automodule:: hoverxref.extension

tests/examples/python-domain/index.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ Python Domain
33

44
This is an example page with a Python Domain role usage.
55

6-
:py:class:`This is a :py:class: role to a Python object <hoverxref.extension.HoverXRefStandardDomain>`.
6+
:py:class:`This is a :py:class: role to a Python object <hoverxref.extension.HoverXRefStandardDomainMixin>`.
77

88
:py:mod:`hoverxref.extension`
99

tests/test_htmltag.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ def test_python_domain(app, status, warning):
162162
content = open(path).read()
163163

164164
chunks = [
165-
'<a class="hoverxref reference internal" data-doc="api" data-project="myproject" data-section="hoverxref.extension.HoverXRefStandardDomain" data-version="myversion" href="api.html#hoverxref.extension.HoverXRefStandardDomain" title="hoverxref.extension.HoverXRefStandardDomain"><code class="xref py py-class docutils literal notranslate"><span class="pre">This</span> <span class="pre">is</span> <span class="pre">a</span> <span class="pre">:py:class:</span> <span class="pre">role</span> <span class="pre">to</span> <span class="pre">a</span> <span class="pre">Python</span> <span class="pre">object</span></code></a>',
165+
'<a class="hoverxref reference internal" data-doc="api" data-project="myproject" data-section="hoverxref.extension.HoverXRefStandardDomainMixin" data-version="myversion" href="api.html#hoverxref.extension.HoverXRefStandardDomainMixin" title="hoverxref.extension.HoverXRefStandardDomainMixin"><code class="xref py py-class docutils literal notranslate"><span class="pre">This</span> <span class="pre">is</span> <span class="pre">a</span> <span class="pre">:py:class:</span> <span class="pre">role</span> <span class="pre">to</span> <span class="pre">a</span> <span class="pre">Python</span> <span class="pre">object</span></code></a>',
166166
'<a class="hoverxref reference internal" data-doc="api" data-project="myproject" data-section="hoverxref.extension" data-version="myversion" href="api.html#module-hoverxref.extension" title="hoverxref.extension"><code class="xref py py-mod docutils literal notranslate"><span class="pre">hoverxref.extension</span></code></a>',
167167
'<a class="hoverxref reference internal" data-doc="api" data-project="myproject" data-section="hoverxref.utils.get_ref_xref_data" data-version="myversion" href="api.html#hoverxref.utils.get_ref_xref_data" title="hoverxref.utils.get_ref_xref_data"><code class="xref py py-func docutils literal notranslate"><span class="pre">hoverxref.utils.get_ref_xref_data()</span></code></a>',
168168
]

0 commit comments

Comments
 (0)