Skip to content

Commit

Permalink
Include source in highlighting warnings (#11150)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
jbms committed Mar 17, 2023
1 parent 2f03886 commit 7a4ce71
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 6 deletions.
4 changes: 2 additions & 2 deletions sphinx/highlighting.py
Expand Up @@ -170,8 +170,8 @@ def highlight_block(self, source: str, lang: str, opts: dict | None = None,
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)
Expand Down
2 changes: 1 addition & 1 deletion tests/test_build_html.py
Expand Up @@ -34,7 +34,7 @@
%(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.
"""


Expand Down
2 changes: 1 addition & 1 deletion tests/test_build_latex.py
Expand Up @@ -35,7 +35,7 @@
%(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.
"""


Expand Down
5 changes: 3 additions & 2 deletions tests/test_highlighting.py
Expand Up @@ -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)

0 comments on commit 7a4ce71

Please sign in to comment.