Skip to content

Commit 7ceefee

Browse files
authored
Merge pull request #146 from readthedocs/humitos/embed-api-v3
2 parents eed8700 + cf1c8b2 commit 7ceefee

20 files changed

+118
-433
lines changed

.readthedocs.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
version: 2
22

33
python:
4-
version: 3
4+
version: "3.8"
55
install:
66
- method: pip
77
path: .

docs/conf.py

+11
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,16 @@
5555
intersphinx_mapping = {
5656
'readthedocs': ('https://docs.readthedocs.io/en/stable/', None),
5757
'sphinx': ('https://www.sphinx-doc.org/en/master/', None),
58+
'sympy': ('https://docs.sympy.org/latest/', None),
59+
'numpy': ('https://numpy.org/doc/stable/', None),
60+
'python': ('https://docs.python.org/3/', None),
5861
}
5962
hoverxref_intersphinx = [
6063
'readthedocs',
6164
'sphinx',
65+
'sympy',
66+
'numpy',
67+
'python',
6268
]
6369
hoverxref_intersphinx_types = {
6470
'readthedocs': 'modal',
@@ -81,6 +87,10 @@
8187
# Building on a local Read the Docs instance
8288
hoverxref_api_host = 'http://community.dev.readthedocs.io'
8389

90+
if os.environ.get('NGROK_READTHEDOCS') == 'True':
91+
# Building on a local Read the Docs instance using NGROK for HTTPS
92+
hoverxref_api_host = 'https://readthedocs.ngrok.io'
93+
8494
hoverxref_tooltip_maxwidth = 650
8595
hoverxref_auto_ref = True
8696
hoverxref_roles = [
@@ -93,6 +103,7 @@
93103
'confval': 'tooltip',
94104
'mod': 'modal',
95105
'class': 'modal',
106+
'obj': 'tooltip',
96107
}
97108
hoverxref_domains = [
98109
'py',

docs/configuration.rst

+7-23
Original file line numberDiff line numberDiff line change
@@ -101,8 +101,9 @@ These settings are global and have effect on both, tooltips and modal dialogues.
101101

102102
.. warning::
103103

104-
The Sphinx's target project **must be hosted on Read the Docs** to work.
105-
This is a current limitation that we hope to remove in the future.
104+
The Sphinx's target project **must be hosted on Read the Docs** to work or,
105+
be one of the allowed external projects:
106+
currently CPython, SymPy, NumPy are supported.
106107

107108
.. confval:: hoverxref_intersphinx_types
108109

@@ -152,33 +153,16 @@ These settings are global and have effect on both, tooltips and modal dialogues.
152153
.. _Mathjax: http://www.sphinx-doc.org/es/master/usage/extensions/math.html#module-sphinx.ext.mathjax
153154

154155

155-
.. warning::
156-
157-
You shouldn't modify the following three settings (api_host, project, version) unless you know what you are doing.
158-
Their defaults should be fine to build the documentation and make it work in Read the Docs.
159-
160-
161156
.. confval:: hoverxref_api_host
162157

163158
Description: Host URL for the API to retrieve the content of the floating window
164159

165-
Default: ``https://readthedocs.org``
166-
167-
Type: string
168-
169-
.. confval:: hoverxref_project
170-
171-
Description: Read the Docs project slug
172-
173-
Default: It defaults to ``READTHEDOCS_PROJECT`` environment variable
174-
175-
Type: string
176-
177-
.. confval:: hoverxref_version
160+
.. warning::
178161

179-
Description: Read the Docs version slug
162+
You shouldn't modify this setting unless you know what you are doing.
163+
Its default should be fine to build the documentation and make it work in Read the Docs.
180164

181-
Default: It defaults to ``READTHEDOCS_VERSION`` environment variable
165+
Default: ``https://readthedocs.org``
182166

183167
Type: string
184168

docs/development.rst

+2-4
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,11 @@ To setup this approach, you need to put these settings in the ``conf.py`` of you
2121

2222
.. code-block:: python
2323
24-
hoverxref_project = 'sphinx-hoverxref'
25-
hoverxref_version = 'latest'
2624
hoverxref_api_host = 'https://readthedocs.org'
2725
2826
After building the documentation all the requests will be done to URLs like::
2927

30-
https://readthedocs.org/api/v2/embed/?project=sphinx-hoverxref&version=latest&doc=...&section=...
28+
https://readthedocs.org/api/v3/embed/?doctool=sphinx&doctoolversion=...&url=...
3129

3230
.. note::
3331

@@ -84,7 +82,7 @@ To make the extension to work, you will need to define this setting in your ``co
8482

8583
.. code-block:: python
8684
87-
hoverxref_api_host = 'http://dev.readthedocs.io:8000'
85+
hoverxref_api_host = 'http://community.dev.readthedocs.io'
8886
8987
.. tip::
9088

docs/index.rst

+2-2
Original file line numberDiff line numberDiff line change
@@ -99,8 +99,8 @@ Badges:
9999
.. _Read the Docs: https://readthedocs.org
100100

101101

102-
.. |Build| image:: https://travis-ci.org/readthedocs/sphinx-hoverxref.svg?branch=master
103-
:target: https://travis-ci.org/readthedocs/sphinx-hoverxref
102+
.. |Build| image:: https://circleci.com/gh/readthedocs/sphinx-hoverxref.svg?style=svg
103+
:target: https://circleci.com/gh/readthedocs/sphinx-hoverxref
104104
:alt: Build status
105105
.. |PyPI version| image:: https://img.shields.io/pypi/v/sphinx-hoverxref.svg
106106
:target: https://pypi.org/project/sphinx-hoverxref

docs/installation.rst

+2-2
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ you can use ``:hoverxref:`` role to show a tooltip [#]_ when hovering with the m
3737
.. warning::
3838

3939
This extension **requires a backend server** to retrieve the tooltip content.
40-
Currently only `Read the Docs`_ is supported,
41-
so it will only work if your documentation is hosted on Read the Docs.
40+
Currently, only `Read the Docs`_ is supported as backend server.
41+
Take into account that your documentation has to be hosted on Read the Docs for this extension to work.
4242

4343
If you prefer to apply this behavior to *all* your ``:ref:`` in your documentation,
4444
you can use the config :confval:`hoverxref_auto_ref`.

docs/requirements.txt

+2-8
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,6 @@
1-
# Sphinx 3.5.x includes a feature to only include the JS and CSS on the pages
2-
# that they are used. This conflicts when we render content that uses MathJax,
3-
# since the page that shows the tooltip does not has MathJax loaded, but the
4-
# content rendered inside the tooltip requires it to work.
5-
# https://github.com/sphinx-doc/sphinx/pull/8631
6-
sphinx==3.4.3 # pyup: <3.5
7-
1+
sphinx==4.2.0
82
sphinx-autoapi==1.8.4
9-
sphinx-rtd-theme==0.5.2
3+
sphinx-rtd-theme==1.0.0
104
sphinx-tabs==3.2.0
115
sphinx-prompt==1.4.0
126
sphinx-version-warning==1.1.2

docs/usage.rst

+15-10
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,7 @@ and will embed the content of the document/section the link is pointing to, into
77

88
``:hoverxref:`` role uses Sphinx's internals reference resolution to find out where the link points to.
99
So, the way of referencing the section works in the same way as the ``:ref:`` standard role.
10-
See `Sphinx's ref role documentation`_ for more information.
11-
12-
.. _Sphinx's ref role documentation: https://www.sphinx-doc.org/en/stable/usage/restructuredtext/roles.html#cross-referencing-arbitrary-locations
10+
See Sphinx's :rst:role:`ref` for more information.
1311

1412
Simplest usage example,
1513

@@ -42,14 +40,23 @@ Show a tooltip for :doc:`Read the Docs automation rules <readthedocs:automation-
4240

4341
.. note::
4442

45-
Keep in mind that the linked project should be hosted at Read the Docs.
46-
This is a limitation that will be removed in the future.
43+
Keep in mind that the linked project should be hosted at Read the Docs or,
44+
be one of the allowed external projects:
45+
currently CPython, SymPy, NumPy are supported.
46+
47+
48+
Example with projects not hosted on Read the Docs
49+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
50+
51+
* Show a tooltip for CPython documentation: :py:mod:`webbrowser <webbrowser>`.
52+
* Show a tooltip for SymPy documentation: :py:class:`sympy.functions.combinatorial.numbers.tribonacci <sympy.functions.combinatorial.numbers.tribonacci>`.
53+
* Show a tooltip for NumPy documentation: :py:class:`numpy.single <numpy.single>`.
4754

4855

4956
Tooltip on custom object
5057
------------------------
5158

52-
Sphinx has the ability to define custom objects (via `Sphinx.add_object_type`_).
59+
Sphinx has the ability to define custom objects (via :py:meth:`Sphinx.add_object_type <sphinx.application.Sphinx.add_object_type>`).
5360
``hoverxref`` can also show a tooltip on these objects if desired.
5461
You need to tell ``hoverxref`` which are the roles where the tooltip has to appear on.
5562
To do this, use :confval:`hoverxref_roles <hoverxref_roles>` config.
@@ -119,9 +126,9 @@ These actions are usually calling a Javascript function.
119126

120127
.. warning::
121128

122-
Note that Sphinx>3.5 adds `a feature to only include JS/CSS in pages where they are used`_ instead of in all the pages.
129+
Note that Sphinx>=3.5 adds `a feature to only include JS/CSS in pages where they are used`_ instead of in all the pages.
123130
This `may affect the rendering of tooltips`_ that includes content requiring extra rendering steps.
124-
**Make sure you are using Sphinx 3.4.x or >=4.1.x** if you require rendering this type of content in your tooltips.
131+
**Make sure you are using Sphinx <=3.4.x or >=4.1.x** if you require rendering this type of content in your tooltips.
125132

126133
.. _a feature to only include JS/CSS in pages where they are used: https://github.com/sphinx-doc/sphinx/pull/8631
127134
.. _may affect the rendering of tooltips: https://github.com/sphinx-doc/sphinx/issues/9115
@@ -151,8 +158,6 @@ To render a tooltip where its contents has a ``mathjax`` you need to enable :con
151158
Show a :hoverxref:`tooltip with Mathjax <mathjax:Mathjax>` formulas.
152159

153160

154-
.. _Sphinx.add_object_type: https://www.sphinx-doc.org/en/master/extdev/appapi.html#sphinx.application.Sphinx.add_object_type
155-
156161
.. _sphinx-tabs: https://github.com/djungelorm/sphinx-tabs
157162
.. _mathjax: http://www.sphinx-doc.org/es/master/usage/extensions/math.html#module-sphinx.ext.mathjax
158163

hoverxref/_static/js/hoverxref.js_t

+17-32
Original file line numberDiff line numberDiff line change
@@ -79,22 +79,14 @@ function reLoadSphinxTabs() {
7979
};
8080
};
8181

82-
function getEmbedURL(project, version, doc, docpath, section, url) {
83-
if (url) {
84-
var params = {
85-
'url': url,
86-
}
87-
} else {
88-
var params = {
89-
'project': project,
90-
'version': version,
91-
'doc': doc,
92-
'path': docpath,
93-
'section': section,
94-
}
82+
function getEmbedURL(url) {
83+
var params = {
84+
'doctool': 'sphinx',
85+
'doctoolversion': '{{ hoverxref_sphinx_version }}',
86+
'url': url,
9587
}
9688
console.debug('Data: ' + JSON.stringify(params));
97-
var url = '{{ hoverxref_api_host }}' + '/api/v2/embed/?' + $.param(params);
89+
var url = '{{ hoverxref_api_host }}' + '/api/v3/embed/?' + $.param(params);
9890
console.debug('URL: ' + url);
9991
return url
10092
}
@@ -109,20 +101,15 @@ $(document).ready(function() {
109101
animationDuration: {{ hoverxref_tooltip_animation_duration }},
110102
side: '{{ hoverxref_tooltip_side }}',
111103
content: '{{ hoverxref_tooltip_content }}',
104+
contentAsHTML: true,
112105

113106
functionBefore: function(instance, helper) {
114107
var $origin = $(helper.origin);
115-
var project = $origin.data('project');
116-
var version = $origin.data('version');
117-
var doc = $origin.data('doc');
118-
var docpath = $origin.data('docpath');
119-
var section = $origin.data('section');
120-
var url = $origin.data('url');
121-
108+
var href = $origin.prop('href');
122109

123110
// we set a variable so the data is only loaded once via Ajax, not every time the tooltip opens
124111
if ($origin.data('loaded') !== true) {
125-
var url = getEmbedURL(project, version, doc, docpath, section, url);
112+
var url = getEmbedURL(href);
126113
$.get(url, function(data) {
127114
// call the 'content' method to update the content of our tooltip with the returned data.
128115
// note: this content update will trigger an update animation (see the updateAnimation option)
@@ -186,23 +173,21 @@ $(document).ready(function() {
186173
{% endif %}
187174

188175
function showModal(element) {
189-
var project = element.data('project');
190-
var version = element.data('version');
191-
var doc = element.data('doc');
192-
var docpath = element.data('docpath');
193-
var section = element.data('section');
194-
var url = element.data('url');
195-
196-
var url = getEmbedURL(project, version, doc, docpath, section, url);
176+
var href = element.prop('href');
177+
var url = getEmbedURL(href);
197178
$.get(url, function(data) {
198179
var content = $('<div></div>');
199-
content.html(data['content'][0]);
180+
content.html(data['content']);
200181

201182
var h1 = $('h1:first', content);
202183
var title = h1.text()
203184
if (title) {
204185
var link = $('a', h1).attr('href') || '#';
205-
var a = $('<a></a>').attr('href', link).text('{{ hoverxref_modal_prefix_title }}' + title.replace('¶', ''));
186+
187+
// Remove permalink icon from the title
188+
var title = title.replace('¶', '').replace('', '');
189+
190+
var a = $('<a></a>').attr('href', link).text('{{ hoverxref_modal_prefix_title }}' + title);
206191
}
207192
else {
208193
var a = '{{ hoverxref_modal_prefix_title }}{{ hoverxref_modal_default_title }}';

0 commit comments

Comments
 (0)