Skip to content

Commit de3073b

Browse files
committed
Merge branch 'master' of github.com:humitos/sphinx-hoverxref into humitos/send-docpath
2 parents 064233c + fc34b38 commit de3073b

File tree

12 files changed

+102
-35
lines changed

12 files changed

+102
-35
lines changed

.travis.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ dist: xenial
33
python:
44
- 3.6
55
- 3.7
6+
- 3.8
67
install:
78
pip install tox-travis
89
script:

MANIFEST.in

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,7 @@ include hoverxref/_static/js/tooltipster.bundle.min.js
55
include hoverxref/_static/css/tooltipster.bundle.min.css
66
include hoverxref/_static/css/tooltipster-sideTip-shadow.min.css
77
include hoverxref/_static/css/tooltipster.custom.css
8+
include hoverxref/_static/css/tooltipster-sideTip-light.min.css
9+
include hoverxref/_static/css/tooltipster-sideTip-borderless.min.css
10+
include hoverxref/_static/css/tooltipster-sideTip-noir.min.css
11+
include hoverxref/_static/css/tooltipster-sideTip-punk.min.css

docs/conf.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@
128128
# Add any paths that contain custom static files (such as style sheets) here,
129129
# relative to this directory. They are copied after the builtin static files,
130130
# so a file named "default.css" will overwrite the builtin "default.css".
131-
html_static_path = ['_static']
131+
# html_static_path = ['_static']
132132

133133
# Custom sidebar templates, must be a dictionary that maps document names
134134
# to template names.

docs/requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
sphinx==1.8.5 # pyup: ignore
2-
sphinx-autoapi==1.2.1
2+
sphinx-autoapi==1.3.0
33
sphinx-rtd-theme==0.4.3
44
sphinx-tabs==1.1.13
55
sphinx-prompt==1.1.0

docs/usage.rst

Lines changed: 2 additions & 2 deletions
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

Lines changed: 3 additions & 5 deletions
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

@@ -32,7 +30,7 @@ def _get_docpath(self, builder, docname):
3230
return docpath
3331

3432

35-
class HoverXRefPythonDomain(HoverXRefBaseDomain, PythonDomain):
33+
class HoverXRefPythonDomainMixin(HoverXRefBaseDomain):
3634

3735
def resolve_xref(self, env, fromdocname, builder, type, target, node, contnode):
3836
refnode = super().resolve_xref(env, fromdocname, builder, type, target, node, contnode)
@@ -59,9 +57,9 @@ def resolve_xref(self, env, fromdocname, builder, type, target, node, contnode):
5957
return refnode
6058

6159

62-
class HoverXRefStandardDomain(HoverXRefBaseDomain, StandardDomain):
60+
class HoverXRefStandardDomainMixin(HoverXRefBaseDomain):
6361
"""
64-
Override ``StandardDomain`` to save the values after the xref resolution.
62+
Mixin for ``StandardDomain`` to save the values after the xref resolution.
6563
6664
``:ref:`` are treating as a different node in Sphinx
6765
(``sphinx.addnodes.pending_xref``). These nodes are translated to regular

hoverxref/extension.py

Lines changed: 75 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
import os
22
import inspect
3+
import types
34
from docutils import nodes
5+
import sphinx
46
from sphinx.roles import XRefRole
57
from sphinx.util.fileutil import copy_asset
68

79
from . import version
8-
from .domains import HoverXRefPythonDomain, HoverXRefStandardDomain
9-
from .translators import HoverXRefHTMLTranslator
10+
from .domains import HoverXRefPythonDomainMixin, HoverXRefStandardDomainMixin
11+
from .translators import HoverXRefHTMLTranslatorMixin
1012

1113
ASSETS_FILES = [
1214
'js/hoverxref.js_t', # ``_t`` tells Sphinx this is a template
@@ -53,6 +55,13 @@ def copy_asset_files(app, exception):
5355

5456

5557
def setup_domains(app, config):
58+
"""
59+
Override domains respecting the one defined (if any).
60+
61+
We create a new class by inheriting the Sphinx Domain already defined
62+
and our own ``HoverXRef...DomainMixin`` that includes the logic for
63+
``_hoverxref`` attributes.
64+
"""
5665
# Add ``hoverxref`` role replicating the behavior of ``ref``
5766
app.add_role_to_domain(
5867
'std',
@@ -63,10 +72,27 @@ def setup_domains(app, config):
6372
warn_dangling=True,
6473
),
6574
)
66-
app.add_domain(HoverXRefStandardDomain, override=True)
6775

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

7197

7298
def setup_sphinx_tabs(app, config):
@@ -76,13 +102,55 @@ def setup_sphinx_tabs(app, config):
76102
Sphinx Tabs removes the CSS/JS from pages that does not use the directive.
77103
Although, we need them to use inside the tooltip.
78104
"""
79-
listeners = list(app.events.listeners.get('html-page-context').items())
105+
if sphinx.version_info < (3, 0, 0):
106+
listeners = list(app.events.listeners.get('html-page-context').items())
107+
else:
108+
listeners = [
109+
(listener.id, listener.handler)
110+
for listener in app.events.listeners.get('html-page-context')
111+
]
80112
for listener_id, function in listeners:
81113
module_name = inspect.getmodule(function).__name__
82114
if module_name == 'sphinx_tabs.tabs':
83115
app.disconnect(listener_id)
84116

85117

118+
def setup_translators(app):
119+
"""
120+
Override translators respecting the one defined (if any).
121+
122+
We create a new class by inheriting the Sphinx Translator already defined
123+
and our own ``HoverXRefHTMLTranslatorMixin`` that includes the logic to
124+
``_hoverxref`` attributes.
125+
"""
126+
if not app.registry.translators.items():
127+
translator = types.new_class(
128+
'HoverXRefHTMLTranslator',
129+
(
130+
HoverXRefHTMLTranslatorMixin,
131+
app.builder.default_translator_class,
132+
),
133+
{},
134+
)
135+
app.set_translator(app.builder.name, translator, override=True)
136+
else:
137+
for name, klass in app.registry.translators.items():
138+
if app.builder.format != 'html':
139+
# Skip translators that are not HTML
140+
continue
141+
142+
translator = types.new_class(
143+
'HoverXRefHTMLTranslator',
144+
(
145+
HoverXRefHTMLTranslatorMixin,
146+
klass,
147+
),
148+
{},
149+
)
150+
app.set_translator(name, translator, override=True)
151+
152+
153+
86154
def setup(app):
87155
"""Setup ``hoverxref`` Sphinx extension."""
88156

@@ -109,13 +177,7 @@ def setup(app):
109177
app.add_config_value('hoverxref_tooltip_content', 'Loading...', 'env')
110178
app.add_config_value('hoverxref_tooltip_class', 'rst-content', 'env')
111179

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-
180+
app.connect('builder-inited', setup_translators)
119181
app.connect('config-inited', setup_domains)
120182
app.connect('config-inited', setup_sphinx_tabs)
121183
app.connect('build-finished', copy_asset_files)

hoverxref/translators.py

Lines changed: 2 additions & 3 deletions
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.

tests/examples/python-domain/api.rst

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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

0 commit comments

Comments
 (0)