Skip to content

Commit 3558a67

Browse files
authored
Merge pull request readthedocs#71 from readthedocs/humitos/support-numref
2 parents 52a6472 + 3fb36cd commit 3558a67

File tree

7 files changed

+87
-2
lines changed

7 files changed

+87
-2
lines changed

docs/configuration.rst

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ These settings are global and have effect on both, tooltips and modal dialogues.
2020

2121
Default: ``{}``
2222

23+
Type: dictionary
24+
2325
Example:
2426

2527
.. code-block:: python
@@ -32,7 +34,6 @@ These settings are global and have effect on both, tooltips and modal dialogues.
3234
'class': 'tooltip', # for Python Sphinx Domain
3335
}
3436
35-
Type: dictionary
3637
3738
.. confval:: hoverxref_default_type
3839

@@ -80,6 +81,17 @@ These settings are global and have effect on both, tooltips and modal dialogues.
8081

8182
Type: list
8283

84+
Example:
85+
86+
.. code-block:: python
87+
88+
hoverxref_roles = [
89+
'numref',
90+
'confval',
91+
'setting',
92+
]
93+
94+
8395
.. confval:: hoverxref_sphinxtabs
8496

8597
Description: trigger an extra step to render tooltips where its content has a `Sphinx Tabs`_

hoverxref/domains.py

Lines changed: 25 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_numref_data
33

44
logger = logging.getLogger(__name__)
55

@@ -167,3 +167,27 @@ def _resolve_obj_xref(self, env, fromdocname, builder, typ, target, node, contno
167167
refnode._hoverxref,
168168
)
169169
return refnode
170+
171+
# TODO: combine this method with ``_resolve_obj_xref``
172+
def _resolve_numref_xref(self, env, fromdocname, builder, typ, target, node, contnode):
173+
refnode = super()._resolve_numref_xref(env, fromdocname, builder, typ, target, node, contnode)
174+
if refnode is None:
175+
return refnode
176+
177+
if any([
178+
not env.config.hoverxref_is_configured,
179+
self._is_ignored_ref(env, target),
180+
typ not in env.config.hoverxref_roles,
181+
]):
182+
return refnode
183+
184+
docname, labelid = get_ref_numref_data(self, node, typ, target)
185+
docpath = self._get_docpath(builder, docname)
186+
self._inject_hoverxref_data(env, refnode, typ, docname, docpath, labelid)
187+
logger.debug(
188+
':%s: _hoverxref injected: fromdocname=%s %s',
189+
typ,
190+
fromdocname,
191+
refnode._hoverxref,
192+
)
193+
return refnode

hoverxref/utils.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,3 +52,25 @@ def get_ref_obj_data(domain, node, typ, target):
5252
docname, labelid = domain.objects[objtype, target]
5353
break
5454
return docname, labelid
55+
56+
57+
def get_ref_numref_data(domain, node, typ, target):
58+
"""
59+
Use Sphinx's internals to resolve :numref: and returns this data.
60+
61+
:returns: tuple (``docname``, ``labelid``)
62+
"""
63+
# Borrowed from https://github.com/sphinx-doc/sphinx/blob/47cd262b3e50ed650a82f272ba128a1f872cda4d/sphinx/domains/std.py#L699-L702
64+
if sphinx.version_info < (2, 1):
65+
if node['refexplicit']:
66+
docname, labelid = domain.data['anonlabels'].get(target, ('', ''))
67+
else:
68+
# reference to named label; the final node will
69+
# contain the section name after the label
70+
docname, labelid, sectname = domain.data['labels'].get(target, ('', '', ''))
71+
else:
72+
if target in domain.labels:
73+
docname, labelid, figname = domain.labels.get(target, ('', '', ''))
74+
else:
75+
docname, labelid = domain.anonlabels.get(target, ('', ''))
76+
return docname, labelid

tests/examples/custom-object/code.rst

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
:orphan:
2+
3+
Code
4+
====
5+
6+
7+
This is a page with some code blocks directives
8+
9+
10+
.. code-block:: python
11+
:caption: PyExample
12+
:name: python-code-block
13+
14+
import datetime
15+
datetime.datetime.now()

tests/examples/custom-object/conf.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,10 @@
1010
hoverxref_version = 'myversion'
1111
hoverxref_roles = [
1212
'confval',
13+
'numref',
1314
]
1415

16+
numfig = True
1517

1618
def setup(app):
1719
app.add_object_type(

tests/examples/custom-object/index.rst

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,12 @@ This documentation contains user-defined objects using ``Sphinx.add_object_type`
88
:confval:`This is a :confval: to conf-title <conf-title>`.
99

1010
:hoverxref:`This is a :hoverxref: to Configuration document <configuration>`.
11+
12+
:numref:`This is a :numref: to a Python code block ({name}) <python-code-block>`.
13+
14+
15+
.. code page needs to be in the toctree for numfig=True to work and number it correctly
16+
17+
.. toctree::
18+
19+
code

tests/test_htmltag.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ def test_custom_object(app, status, warning):
108108
chunks = [
109109
'<a class="hoverxref tooltip reference internal" data-doc="configuration" data-docpath="/configuration.html" data-project="myproject" data-section="confval-conf-title" data-version="myversion" href="configuration.html#confval-conf-title"><code class="xref std std-confval docutils literal notranslate"><span class="pre">This</span> <span class="pre">is</span> <span class="pre">a</span> <span class="pre">:confval:</span> <span class="pre">to</span> <span class="pre">conf-title</span></code></a>',
110110
'<a class="hoverxref tooltip reference internal" data-doc="configuration" data-docpath="/configuration.html" data-project="myproject" data-section="configuration" data-version="myversion" href="configuration.html#configuration"><span class="std std-ref">This is a :hoverxref: to Configuration document</span></a>',
111+
'<a class="hoverxref tooltip reference internal" data-doc="code" data-docpath="/code.html" data-project="myproject" data-section="python-code-block" data-version="myversion" href="code.html#python-code-block"><span class="std std-numref">This is a :numref: to a Python code block (PyExample)</span></a>'
111112
]
112113

113114
for chunk in chunks:

0 commit comments

Comments
 (0)