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

Commit 58f34ae

Browse files
committed
Support :doc: role
Inject hoverxref data in :doc: roles. It adds a new config (`hoverxref_auto_doc`) that works in a similar way as `hoverxref_auto_ref` by enabling hoverxref in all `:doc:` roles.
1 parent 1385bde commit 58f34ae

File tree

6 files changed

+71
-2
lines changed

6 files changed

+71
-2
lines changed

hoverxref/domains.py

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from sphinx.util import logging
2-
from .utils import get_ref_xref_data, get_ref_obj_data
2+
from .utils import get_ref_xref_data, get_ref_obj_data, get_ref_doc_data
33

44
logger = logging.getLogger(__name__)
55

@@ -145,6 +145,29 @@ def _resolve_ref_xref(self, env, fromdocname, builder, typ, target, node, contno
145145
)
146146
return refnode
147147

148+
def _resolve_doc_xref(self, env, fromdocname, builder, typ, target, node, contnode):
149+
refnode = super()._resolve_doc_xref(env, fromdocname, builder, typ, target, node, contnode)
150+
if refnode is None:
151+
return refnode
152+
153+
if any([
154+
not env.config.hoverxref_is_configured,
155+
self._is_ignored_ref(env, target),
156+
not env.config.hoverxref_auto_doc,
157+
]):
158+
return refnode
159+
160+
docname = get_ref_doc_data(env, node, fromdocname)
161+
docpath = self._get_docpath(builder, docname)
162+
self._inject_hoverxref_data(env, refnode, typ, docname, docpath, '')
163+
logger.debug(
164+
':%s: _hoverxref injected: fromdocname=%s %s',
165+
typ,
166+
fromdocname,
167+
refnode._hoverxref,
168+
)
169+
return refnode
170+
148171
def _resolve_obj_xref(self, env, fromdocname, builder, typ, target, node, contnode):
149172
refnode = super()._resolve_obj_xref(env, fromdocname, builder, typ, target, node, contnode)
150173
if refnode is None:

hoverxref/extension.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,7 @@ def setup(app):
248248
app.add_config_value('hoverxref_project', default_project, 'html')
249249
app.add_config_value('hoverxref_version', default_version, 'html')
250250
app.add_config_value('hoverxref_auto_ref', False, 'env')
251+
app.add_config_value('hoverxref_auto_doc', False, 'env')
251252
app.add_config_value('hoverxref_mathjax', False, 'env')
252253
app.add_config_value('hoverxref_sphinxtabs', False, 'env')
253254
app.add_config_value('hoverxref_roles', [], 'env')

hoverxref/translators.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ class HoverXRefHTMLTranslatorMixin:
1515
def starttag(self, node, tagname, suffix='\n', empty=False, **attributes):
1616
if tagname == 'a' and hasattr(node, '_hoverxref'):
1717
attributes.update(node._hoverxref)
18-
logger.info('_hoverxref attributes: %s', attributes)
18+
logger.debug('node.astext: %s', node.astext())
19+
logger.debug('_hoverxref attributes: %s', attributes)
1920

2021
return super().starttag(node, tagname, suffix, empty, **attributes)

hoverxref/utils.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import sphinx
2+
from sphinx.util import docname_join
23

34

45
def get_ref_xref_data(domain, node, target):
@@ -52,3 +53,18 @@ def get_ref_obj_data(domain, node, typ, target):
5253
docname, labelid = domain.objects[objtype, target]
5354
break
5455
return docname, labelid
56+
57+
58+
def get_ref_doc_data(env, node, fromdocname):
59+
"""
60+
Use Sphinx's internals to get the docname from a reftarget.
61+
62+
:returns: docname
63+
:rtype: str
64+
"""
65+
# Borrowed from https://github.com/sphinx-doc/sphinx/blob/6ef08a42df4534dbb2664d49dc10a16f6df2acb2/sphinx/domains/std.py#L791-L749
66+
refdoc = node.get('refdoc', fromdocname)
67+
docname = docname_join(refdoc, node['reftarget'])
68+
if docname not in env.all_docs:
69+
return None
70+
return docname

tests/examples/default/index.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,8 @@ Example Reference
1616
-----------------
1717

1818
This is a reference to :ref:`example-reference`.
19+
20+
doc role example
21+
----------------
22+
23+
:doc:`This is a :doc: to another document <chapter-i>`.

tests/test_htmltag.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,29 @@ def test_project_version_settings(app, status, warning):
3838
chunks = [
3939
'<a class="reference internal" href="chapter-i.html#chapter-i"><span class="std std-ref">This a :ref: to Chapter I</span></a>',
4040
'<a class="hoverxref tooltip reference internal" data-doc="chapter-i" data-docpath="/chapter-i.html" data-project="myproject" data-section="section-i" data-version="myversion" href="chapter-i.html#section-i"><span class="std std-ref">This a :hoverxref: to Chapter I, Section I</span></a>',
41+
'<a class="reference internal" href="chapter-i.html"><span class="doc">This is a :doc: to another document</span></a>',
42+
]
43+
44+
for chunk in chunks:
45+
assert chunk in content
46+
47+
48+
@pytest.mark.sphinx(
49+
srcdir=srcdir,
50+
confoverrides={
51+
'hoverxref_project': 'myproject',
52+
'hoverxref_version': 'myversion',
53+
'hoverxref_auto_doc': True,
54+
},
55+
)
56+
def test_doc_role(app, status, warning):
57+
app.build()
58+
path = app.outdir / 'index.html'
59+
assert path.exists() is True
60+
content = open(path).read()
61+
62+
chunks = [
63+
'<a class="hoverxref tooltip reference internal" data-doc="chapter-i" data-docpath="/chapter-i.html" data-project="myproject" data-section="" data-version="myversion" href="chapter-i.html"><span class="doc">This is a :doc: to another document</span></a>',
4164
]
4265

4366
for chunk in chunks:

0 commit comments

Comments
 (0)