Relax Pygments parsing on lexing failures

This commit is contained in:
Adam Turner 2023-08-12 06:27:16 +01:00
parent 7e9a2066c2
commit 7d8df06e19
3 changed files with 18 additions and 11 deletions

View File

@ -66,6 +66,7 @@ Bugs fixed
* #11546: Translated nodes identical to their original text are now marked * #11546: Translated nodes identical to their original text are now marked
with the ``translated=True`` attribute. with the ``translated=True`` attribute.
* #10049: html: Change "Permalink" to "Link" for title text in link anchors. * #10049: html: Change "Permalink" to "Link" for title text in link anchors.
* #4225: Relax Pygments parsing on lexing failures.
Testing Testing
------- -------

View File

@ -164,17 +164,23 @@ class PygmentsBridge:
formatter = self.get_formatter(**kwargs) formatter = self.get_formatter(**kwargs)
try: try:
hlsource = highlight(source, lexer, formatter) hlsource = highlight(source, lexer, formatter)
except ErrorToken: except ErrorToken as err:
# this is most probably not the selected language, # this is most probably not the selected language,
# so let it pass unhighlighted # so let it pass un highlighted
if lang == 'default': if lang == 'default':
pass # automatic highlighting failed. lang = 'none' # automatic highlighting failed.
else: else:
logger.warning(__('Could not lex literal_block %r as "%s". ' logger.warning(
'Highlighting skipped.'), source, lang, __('Lexing literal_block %r as "%s" resulted in an error at token: %r. '
type='misc', subtype='highlighting_failure', 'Retrying in relaxed mode.'),
location=location) source, lang, str(err),
lexer = self.get_lexer(source, 'none', opts, force, location) type='misc', subtype='highlighting_failure',
location=location)
if force:
lang = 'none'
else:
force = True
lexer = self.get_lexer(source, lang, opts, force, location)
hlsource = highlight(source, lexer, formatter) hlsource = highlight(source, lexer, formatter)
if self.dest == 'html': if self.dest == 'html':

View File

@ -97,8 +97,8 @@ def test_default_highlight(logger):
# python: raises error if highlighting failed # python: raises error if highlighting failed
ret = bridge.highlight_block('reST ``like`` text', 'python') ret = bridge.highlight_block('reST ``like`` text', 'python')
logger.warning.assert_called_with('Could not lex literal_block %r as "%s". ' logger.warning.assert_called_with('Lexing literal_block %r as "%s" resulted in an error at token: %r. '
'Highlighting skipped.', 'Retrying in relaxed mode.',
'reST ``like`` text', 'python', 'reST ``like`` text', 'python', '`',
type='misc', subtype='highlighting_failure', type='misc', subtype='highlighting_failure',
location=None) location=None)