Skip to content

Commit 851968f

Browse files
authored
Merge branch 'master' into master
2 parents 180765d + 9a30ca7 commit 851968f

File tree

18 files changed

+116
-34
lines changed

18 files changed

+116
-34
lines changed

CHANGES.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ Features added
3333
Bugs fixed
3434
----------
3535

36+
* #11944: Use anchor in search preview.
37+
Patch by Will Lachance.
3638
* #11668: Raise a useful error when ``theme.conf`` is missing.
3739
Patch by Vinay Sajip.
3840
* #11622: Ensure that the order of keys in ``searchindex.js`` is deterministic.

doc/man/sphinx-build.rst

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,11 @@ Options
3939
Extensions can add their own builders.
4040

4141
.. important::
42-
Sphinx only recognizes the ``-M`` option if it is used first.
42+
Sphinx only recognizes the ``-M`` option if it is used first, along with
43+
the source and output directories, before any other options are passed.
44+
For example::
45+
46+
sphinx-build -M html ./source ./build -W --keep-going
4347

4448
The *make-mode* provides the same build functionality as
4549
a default :ref:`Makefile or Make.bat <makefile_options>`,

pyproject.toml

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ lint = [
8787
"sphinx-lint",
8888
"docutils-stubs",
8989
"types-requests",
90+
"pytest>=6.0",
9091
]
9192
test = [
9293
"pytest>=6.0",
@@ -133,9 +134,6 @@ exclude = [
133134
files = ["sphinx"]
134135
check_untyped_defs = true
135136
disallow_incomplete_defs = true
136-
follow_imports = "skip"
137-
ignore_missing_imports = true
138-
no_implicit_optional = true
139137
python_version = "3.9"
140138
show_column_numbers = true
141139
show_error_context = true
@@ -152,6 +150,9 @@ enable_error_code = [
152150
"ignore-without-code",
153151
"unused-awaitable",
154152
]
153+
disable_error_code = [
154+
"import-untyped",
155+
]
155156

156157
[[tool.mypy.overrides]]
157158
module = [

sphinx/ext/autosummary/generate.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,8 @@ def __init__(self, app: Sphinx) -> None:
133133

134134
if app.translator:
135135
self.env.add_extension("jinja2.ext.i18n")
136-
self.env.install_gettext_translations(app.translator)
136+
# ``install_gettext_translations`` is injected by the ``jinja2.ext.i18n`` extension
137+
self.env.install_gettext_translations(app.translator) # type: ignore[attr-defined]
137138

138139
def render(self, template_name: str, context: dict) -> str:
139140
"""Render a template file."""

sphinx/jinja2glue.py

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,12 @@
88

99
from jinja2 import BaseLoader, FileSystemLoader, TemplateNotFound
1010
from jinja2.sandbox import SandboxedEnvironment
11-
from jinja2.utils import open_if_exists
11+
from jinja2.utils import open_if_exists, pass_context
1212

1313
from sphinx.application import TemplateBridge
1414
from sphinx.util import logging
1515
from sphinx.util.osutil import mtimes_of_files
1616

17-
try:
18-
from jinja2.utils import pass_context
19-
except ImportError:
20-
from jinja2 import contextfunction as pass_context
21-
2217
if TYPE_CHECKING:
2318
from collections.abc import Iterator
2419

@@ -194,7 +189,9 @@ def init(
194189
self.environment.globals['accesskey'] = pass_context(accesskey)
195190
self.environment.globals['idgen'] = idgen
196191
if use_i18n:
197-
self.environment.install_gettext_translations(builder.app.translator)
192+
# ``install_gettext_translations`` is injected by the ``jinja2.ext.i18n`` extension
193+
self.environment.install_gettext_translations( # type: ignore[attr-defined]
194+
builder.app.translator)
198195

199196
def render(self, template: str, context: dict) -> str: # type: ignore[override]
200197
return self.environment.get_template(template).render(context)
@@ -218,5 +215,6 @@ def get_source(self, environment: Environment, template: str) -> tuple[str, str,
218215
return loader.get_source(environment, template)
219216
except TemplateNotFound:
220217
pass
221-
msg = f"{template!r} not found in {self.environment.loader.pathchain}"
218+
msg = (f"{template!r} not found in "
219+
f"{self.environment.loader.pathchain}") # type: ignore[union-attr]
222220
raise TemplateNotFound(msg)

sphinx/registry.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
if sys.version_info >= (3, 10):
1212
from importlib.metadata import entry_points
1313
else:
14-
from importlib_metadata import entry_points
14+
from importlib_metadata import entry_points # type: ignore[import-not-found]
1515

1616
from sphinx.domains import Domain, Index, ObjType
1717
from sphinx.domains.std import GenericObject, Target

sphinx/search/ja.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,16 @@
1313
import os
1414
import re
1515
import sys
16-
from typing import TYPE_CHECKING, Any, Dict, List
16+
from typing import Any
1717

1818
try:
19-
import MeCab
19+
import MeCab # type: ignore[import-not-found]
2020
native_module = True
2121
except ImportError:
2222
native_module = False
2323

2424
try:
25-
import janome.tokenizer
25+
import janome.tokenizer # type: ignore[import-not-found]
2626
janome_module = True
2727
except ImportError:
2828
janome_module = False

sphinx/search/zh.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,13 @@
44

55
import os
66
import re
7-
from typing import TYPE_CHECKING, Dict, List
87

98
import snowballstemmer
109

1110
from sphinx.search import SearchLanguage
1211

1312
try:
14-
import jieba
13+
import jieba # type: ignore[import-not-found]
1514
JIEBA = True
1615
except ImportError:
1716
JIEBA = False

sphinx/testing/fixtures.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ def app_params(request: Any, test_params: dict, shared_result: SharedResult,
8282
if test_params['shared_result']:
8383
if 'srcdir' in kwargs:
8484
msg = 'You can not specify shared_result and srcdir in same time.'
85-
raise pytest.Exception(msg)
85+
pytest.fail(msg)
8686
kwargs['srcdir'] = test_params['shared_result']
8787
restore = shared_result.restore(test_params['shared_result'])
8888
kwargs.update(restore)

sphinx/themes/basic/static/searchtools.js

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ const _displayItem = (item, searchTerms, highlightTerms) => {
9999
.then((data) => {
100100
if (data)
101101
listItem.appendChild(
102-
Search.makeSearchSummary(data, searchTerms)
102+
Search.makeSearchSummary(data, searchTerms, anchor)
103103
);
104104
// highlight search terms in the summary
105105
if (SPHINX_HIGHLIGHT_ENABLED) // set in sphinx_highlight.js
@@ -160,11 +160,22 @@ const Search = {
160160
_queued_query: null,
161161
_pulse_status: -1,
162162

163-
htmlToText: (htmlString) => {
163+
htmlToText: (htmlString, anchor) => {
164164
const htmlElement = new DOMParser().parseFromString(htmlString, 'text/html');
165165
htmlElement.querySelectorAll(".headerlink").forEach((el) => { el.remove() });
166+
if (anchor) {
167+
const anchorContent = htmlElement.querySelector(anchor);
168+
if (anchorContent) return anchorContent.textContent;
169+
170+
console.warn(
171+
`Anchor block not found. Sphinx search tries to obtain it via '${anchor}'. Check your theme or template.`
172+
);
173+
}
174+
175+
// if anchor not specified or not found, fall back to main content
166176
const docContent = htmlElement.querySelector('[role="main"]');
167177
if (docContent) return docContent.textContent;
178+
168179
console.warn(
169180
"Content block not found. Sphinx search tries to obtain it via '[role=main]'. Could you check your theme or template."
170181
);
@@ -549,8 +560,8 @@ const Search = {
549560
* search summary for a given text. keywords is a list
550561
* of stemmed words.
551562
*/
552-
makeSearchSummary: (htmlText, keywords) => {
553-
const text = Search.htmlToText(htmlText);
563+
makeSearchSummary: (htmlText, keywords, anchor) => {
564+
const text = Search.htmlToText(htmlText, anchor);
554565
if (text === "") return null;
555566

556567
const textLower = text.toLowerCase();

0 commit comments

Comments
 (0)