Skip to content

Commit 95db41a

Browse files
committed
Support glossary / term and sphinxcontrib-bibtex
1 parent cf1c8b2 commit 95db41a

File tree

7 files changed

+95
-4
lines changed

7 files changed

+95
-4
lines changed

docs/conf.py

+5
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,11 @@
5050
'hoverxref.extension',
5151
'versionwarning.extension',
5252
'notfound.extension',
53+
'sphinxcontrib.bibtex',
5354
]
5455

56+
bibtex_bibfiles = ['refs.bib']
57+
5558
intersphinx_mapping = {
5659
'readthedocs': ('https://docs.readthedocs.io/en/stable/', None),
5760
'sphinx': ('https://www.sphinx-doc.org/en/master/', None),
@@ -95,6 +98,7 @@
9598
hoverxref_auto_ref = True
9699
hoverxref_roles = [
97100
'confval',
101+
'term',
98102
]
99103

100104
hoverxref_role_types = {
@@ -107,6 +111,7 @@
107111
}
108112
hoverxref_domains = [
109113
'py',
114+
'cite',
110115
]
111116
hoverxref_sphinxtabs = True
112117
hoverxref_mathjax = True

docs/configuration.rst

-4
Original file line numberDiff line numberDiff line change
@@ -65,10 +65,6 @@ These settings are global and have effect on both, tooltips and modal dialogues.
6565

6666
Description: List containing the Sphinx Domain's names where ``hoverxref`` has to be applied.
6767

68-
.. warning::
69-
70-
Only Python Domain (``py``) is currently supported.
71-
7268
Default: ``[]``
7369

7470
Type: list

docs/refs.bib

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
@Book{1987:nelson,
2+
author = {Edward Nelson},
3+
title = {Radically Elementary Probability Theory},
4+
publisher = {Princeton University Press},
5+
year = {1987}
6+
}

docs/requirements.txt

+1
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@ sphinx-prompt==1.4.0
66
sphinx-version-warning==1.1.2
77
sphinx-notfound-page==0.7.1
88
sphinx-autobuild==2021.3.14
9+
sphinxcontrib-bibtex==2.4.1

docs/usage.rst

+34
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,40 @@ To enable ``hoverxref`` on a domain, you need to use the config :confval:`hoverx
113113
indicating which are the domains you desire.
114114

115115

116+
Tooltip on glossary terms
117+
-------------------------
118+
119+
You can add tooltips to glossary terms:
120+
121+
.. code-block:: rst
122+
123+
See the :term:`sphinx:environment` definition in the glossary.
124+
125+
That will render to:
126+
127+
See the :term:`sphinx:environment` definition in the glossary.
128+
129+
To enable ``hoverxref`` on glossary terms, you need to add ``'term'`` to :confval:`hoverxref_roles`.
130+
131+
132+
Tooltip on sphinxcontrib-bibtex cites
133+
-------------------------------------
134+
135+
If you want to show a tooltip on `sphinxcontrib-bibtex <https://sphinxcontrib-bibtex.readthedocs.io/en/latest/>`_ cites,
136+
you just need to enable it in :confval:`hoverxref_domains` by adding ``'cite'`` to that list.
137+
Example:
138+
139+
.. code-block:: rst
140+
141+
See :cite:t:`1987:nelson` for an introduction to non-standard analysis.
142+
Non-standard analysis is fun :cite:p:`1987:nelson`.
143+
144+
See :cite:t:`1987:nelson` for an introduction to non-standard analysis.
145+
Non-standard analysis is fun :cite:p:`1987:nelson`.
146+
147+
.. bibliography::
148+
149+
116150
Tooltip with content that needs extra rendering steps
117151
-----------------------------------------------------
118152

hoverxref/domains.py

+37
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import docutils
2+
13
from sphinx.util import logging
24

35
logger = logging.getLogger(__name__)
@@ -138,3 +140,38 @@ def _resolve_numref_xref(self, env, fromdocname, builder, typ, target, node, con
138140

139141
self._inject_hoverxref_data(env, refnode, typ)
140142
return refnode
143+
144+
145+
class HoverXRefBibtexDomainMixin(HoverXRefBaseDomain):
146+
"""
147+
Mixin for ``BibtexDomain`` to save the values after the xref resolution.
148+
149+
This class add the required ``hoverxref`` and ``modal``/``tooltip`` to tell
150+
the frontend to show a modal/tooltip on this element.
151+
152+
https://github.com/mcmtroffaes/sphinxcontrib-bibtex/blob/2.4.1/src/sphinxcontrib/bibtex/domain.py#L281
153+
"""
154+
155+
def resolve_xref(self, env, fromdocname, builder, typ, target, node, contnode):
156+
textnode = super().resolve_xref(env, fromdocname, builder, typ, target, node, contnode)
157+
if textnode is None:
158+
return textnode
159+
160+
if any([
161+
self._is_ignored_ref(env, target),
162+
not (env.config.hoverxref_auto_ref or typ in self.hoverxref_types)
163+
]):
164+
return textnode
165+
166+
# The structure of the node generated by bibtex is between two
167+
# ``#text`` nodes and we need to add the classes into the ``reference``
168+
# node to get the ``href=`` attribute from it
169+
#
170+
# (Pdb++) textnode.children
171+
# [<#text: 'Nelson ['>, <reference: <#text: 'Nel87'>>, <#text: ']'>]
172+
refnode_index = textnode.first_child_matching_class(docutils.nodes.reference)
173+
if refnode_index:
174+
refnode = textnode.children[refnode_index]
175+
self._inject_hoverxref_data(env, refnode, typ)
176+
177+
return textnode

hoverxref/extension.py

+12
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
from . import version
1313
from .domains import (
1414
HoverXRefBaseDomain,
15+
HoverXRefBibtexDomainMixin,
1516
HoverXRefPythonDomainMixin,
1617
HoverXRefStandardDomainMixin,
1718
)
@@ -119,6 +120,17 @@ def setup_domains(app, config):
119120
)
120121
app.add_domain(domain, override=True)
121122

123+
if 'cite' in app.config.hoverxref_domains:
124+
domain = types.new_class(
125+
'HoverXRefBibtexDomain',
126+
(
127+
HoverXRefBibtexDomainMixin,
128+
app.registry.domains.get('cite'),
129+
),
130+
{}
131+
)
132+
app.add_domain(domain, override=True)
133+
122134

123135
def setup_sphinx_tabs(app, config):
124136
"""

0 commit comments

Comments
 (0)