summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJeremy Maitin-Shepard <jbms@google.com>2023-03-17 08:32:27 -0700
committerGitHub <noreply@github.com>2023-03-17 15:32:27 +0000
commit7a4ce71f6b464b0f1d8f3c5af535f026b0051f0b (patch)
tree0b00c025504d3a0f9cf6d750a983e30956936439
parent2f03886d55ea4b5fa2d6c9a0644683712a508c0d (diff)
downloadsphinx-git-7a4ce71f6b464b0f1d8f3c5af535f026b0051f0b.tar.gz
Include source in highlighting warnings (#11150)
If pygments fails to lex a source string as the specified highlight language, Sphinx prints a warning. Previously, that warning did not include the actual source text, although it does include location information. However, in some cases the location information may be missing, there may be multiple highlighted literals on the same line, or the rST is automatically generated somehow. In such cases, it can be difficult to determine the source text that led to the error. With this change, the source text is included in the warning.
-rw-r--r--sphinx/highlighting.py4
-rw-r--r--tests/test_build_html.py2
-rw-r--r--tests/test_build_latex.py2
-rw-r--r--tests/test_highlighting.py5
4 files changed, 7 insertions, 6 deletions
diff --git a/sphinx/highlighting.py b/sphinx/highlighting.py
index 7794f3d6e..a042b1cf3 100644
--- a/sphinx/highlighting.py
+++ b/sphinx/highlighting.py
@@ -170,8 +170,8 @@ class PygmentsBridge:
if lang == 'default':
pass # automatic highlighting failed.
else:
- logger.warning(__('Could not lex literal_block as "%s". '
- 'Highlighting skipped.'), lang,
+ logger.warning(__('Could not lex literal_block %r as "%s". '
+ 'Highlighting skipped.'), source, lang,
type='misc', subtype='highlighting_failure',
location=location)
lexer = self.get_lexer(source, 'none', opts, force, location)
diff --git a/tests/test_build_html.py b/tests/test_build_html.py
index a5f1d7987..013c09bdb 100644
--- a/tests/test_build_html.py
+++ b/tests/test_build_html.py
@@ -34,7 +34,7 @@ HTML_WARNINGS = ENV_WARNINGS + """\
%(root)s/index.rst:\\d+: WARNING: unknown option: '&option'
%(root)s/index.rst:\\d+: WARNING: citation not found: missing
%(root)s/index.rst:\\d+: WARNING: a suitable image for html builder not found: foo.\\*
-%(root)s/index.rst:\\d+: WARNING: Could not lex literal_block as "c". Highlighting skipped.
+%(root)s/index.rst:\\d+: WARNING: Could not lex literal_block .* as "c". Highlighting skipped.
"""
diff --git a/tests/test_build_latex.py b/tests/test_build_latex.py
index e97f8da38..784ba7e4b 100644
--- a/tests/test_build_latex.py
+++ b/tests/test_build_latex.py
@@ -35,7 +35,7 @@ LATEX_WARNINGS = ENV_WARNINGS + """\
%(root)s/index.rst:\\d+: WARNING: unknown option: '&option'
%(root)s/index.rst:\\d+: WARNING: citation not found: missing
%(root)s/index.rst:\\d+: WARNING: a suitable image for latex builder not found: foo.\\*
-%(root)s/index.rst:\\d+: WARNING: Could not lex literal_block as "c". Highlighting skipped.
+%(root)s/index.rst:\\d+: WARNING: Could not lex literal_block .* as "c". Highlighting skipped.
"""
diff --git a/tests/test_highlighting.py b/tests/test_highlighting.py
index d7e3625c4..1e949ae8f 100644
--- a/tests/test_highlighting.py
+++ b/tests/test_highlighting.py
@@ -97,7 +97,8 @@ def test_default_highlight(logger):
# python: raises error if highlighting failed
ret = bridge.highlight_block('reST ``like`` text', 'python')
- logger.warning.assert_called_with('Could not lex literal_block as "%s". '
- 'Highlighting skipped.', 'python',
+ logger.warning.assert_called_with('Could not lex literal_block %r as "%s". '
+ 'Highlighting skipped.',
+ 'reST ``like`` text', 'python',
type='misc', subtype='highlighting_failure',
location=None)