Skip to content

Commit

Permalink
Search: correctly remove Search.init on Sphinx >= 5.0 (#100)
Browse files Browse the repository at this point in the history
On sphinx-doc/sphinx#10028
the `Sphinx.init` call was changed to be called with the `_ready`
function.

We need to update our regex to catch that,
otherwise `Sphinx.init` will be called twice.

Closes readthedocs/readthedocs.org#8862.
  • Loading branch information
stsewd committed Feb 3, 2022
1 parent f47a64a commit 4d835ca
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 8 deletions.
11 changes: 10 additions & 1 deletion .circleci/config.yml
Expand Up @@ -8,7 +8,7 @@ commands:
type: string
sphinx-version:
type: string
default: "21,22,23,24,30,31,32,33,34,latest"
default: "21,22,23,24,30,31,32,33,34,35,40,41,42,43,44,latest"
steps:
- checkout
- run: pip install --user tox
Expand Down Expand Up @@ -48,6 +48,14 @@ jobs:
- run-tox:
version: py39

py310:
docker:
- image: 'cimg/python:3.10'
steps:
- run-tox:
version: py310
sphinx-version: 42,43,44,latest

lint:
docker:
- image: 'cimg/python:3.9'
Expand All @@ -61,6 +69,7 @@ workflows:
tests:
jobs:
- lint
- py310
- py39
- py38
- py37
Expand Down
17 changes: 14 additions & 3 deletions readthedocs_ext/readthedocs.py
Expand Up @@ -237,7 +237,15 @@ def generate_json_artifacts(app, pagename, templatename, context, doctree):


def remove_search_init(app, exception):
"""Remove Sphinx's Search.init() so it can be initialized by Read the Docs."""
"""
Remove Sphinx's Search.init() so it can be initialized by Read the Docs.
RTD needs to call ``Search.init()`` after overriding some of its methods.
We remove the ``Search.init()`` call from Sphinx to avoid calling it twice.
- https://github.com/sphinx-doc/sphinx/blob/799385f5558a888d1a143bf703d06b66d6717fe4/sphinx/themes/basic/static/searchtools.js#L527-L529 # noqa
- https://github.com/sphinx-doc/sphinx/blob/3b01fbe2adf3077cad5c2cf345c6d000d429d7ac/sphinx/themes/basic/static/searchtools.js#L507 # noqa
"""
if exception:
return

Expand All @@ -249,9 +257,12 @@ def remove_search_init(app, exception):
replacement_text = '/* Search initialization removed for Read the Docs */'
replacement_regex = re.compile(
r'''
^\$\(document\).ready\(function\s*\(\)\s*{(?:\n|\r\n?)
^(\$\(document\).ready\(function\s*\(\)\s*{(?:\n|\r\n?)
\s*Search.init\(\);(?:\n|\r\n?)
\}\);
\}\);)
|
# Sphinx >=5.0 calls Search.init this way.
(_ready\(Search.init\);)
''',
(re.MULTILINE | re.VERBOSE)
)
Expand Down
2 changes: 1 addition & 1 deletion tests/pr-example/conf.py
Expand Up @@ -14,7 +14,7 @@
author = 'readthedocs'
version = '0.1'
release = '0.1'
language = None
language = 'en'
exclude_patterns = ['_build']
todo_include_todos = False
html_theme = 'alabaster'
Expand Down
2 changes: 1 addition & 1 deletion tests/pyexample-json/conf.py
Expand Up @@ -15,7 +15,7 @@
author = u'readthedocs'
version = '0.1'
release = '0.1'
language = None
language = 'en'
exclude_patterns = ['_build']
pygments_style = 'sphinx'
todo_include_todos = False
Expand Down
2 changes: 1 addition & 1 deletion tests/pyexample/conf.py
Expand Up @@ -15,7 +15,7 @@
author = u'readthedocs'
version = '0.1'
release = '0.1'
language = None
language = 'en'
exclude_patterns = ['_build']
pygments_style = 'sphinx'
todo_include_todos = False
Expand Down
10 changes: 9 additions & 1 deletion tox.ini
@@ -1,7 +1,8 @@
[tox]
envlist =
py{27,36}-sphinx{15,16,17,18}
py{36,37,38,39}-sphinx{21,22,23,24,30,31,32,33,34,latest}
py{36,37,38,39}-sphinx{21,22,23,24,30,31,32,33,34,35,40,41,42,43,44,latest}
py310-sphinx-{42,43,44,latest}
lint

[testenv]
Expand All @@ -24,7 +25,14 @@ deps =
sphinx32: Sphinx<3.3
sphinx33: Sphinx<3.4
sphinx34: Sphinx<3.5
sphinx35: Sphinx<3.6
sphinx40: Sphinx<4.1
sphinx41: Sphinx<4.2
sphinx42: Sphinx<4.3
sphinx43: Sphinx<4.4
sphinx44: Sphinx<4.5
sphinxlatest: Sphinx
sphinxmaster: git+https://github.com/sphinx-doc/sphinx.git@master
commands =
py.test {posargs}

Expand Down

0 comments on commit 4d835ca

Please sign in to comment.