diff --git a/CHANGES b/CHANGES index c14656394..c31ac1255 100644 --- a/CHANGES +++ b/CHANGES @@ -46,6 +46,9 @@ Bugs fixed * #901: Emit a warning when using docutils' new "math" markup without a Sphinx math extension active. +* #845: In code blocks, when the selected lexer fails, display line numbers + nevertheless if configured. + Documentation ------------- diff --git a/doc/markup/code.rst b/doc/markup/code.rst index c0e7e8ebe..98fdad7db 100644 --- a/doc/markup/code.rst +++ b/doc/markup/code.rst @@ -62,8 +62,8 @@ installed) and handled in a smart way: * ``c`` * ... and any other lexer name that Pygments supports. -* If highlighting with the selected language fails, the block is not highlighted - in any way. +* If highlighting with the selected language fails (i.e. Pygments emits an + "Error" token), the block is not highlighted in any way. Line numbers ^^^^^^^^^^^^ diff --git a/sphinx/highlighting.py b/sphinx/highlighting.py index df422321b..2c95dec02 100644 --- a/sphinx/highlighting.py +++ b/sphinx/highlighting.py @@ -175,7 +175,7 @@ class PygmentsBridge(object): if self.try_parse(source): lexer = lexers['python'] else: - return self.unhighlighted(source) + lexer = lexers['none'] else: lexer = lexers['python'] elif lang in ('python3', 'py3') and source.startswith('>>>'): @@ -185,7 +185,7 @@ class PygmentsBridge(object): try: lexer = guess_lexer(source) except Exception: - return self.unhighlighted(source) + lexer = lexers['none'] else: if lang in lexers: lexer = lexers[lang] @@ -195,7 +195,7 @@ class PygmentsBridge(object): except ClassNotFound: if warn: warn('Pygments lexer name %r is not known' % lang) - return self.unhighlighted(source) + lexer = lexers['none'] else: raise else: @@ -207,19 +207,19 @@ class PygmentsBridge(object): source = doctest.doctestopt_re.sub('', source) # highlight via Pygments + formatter = self.get_formatter(**kwargs) try: - formatter = self.get_formatter(**kwargs) hlsource = highlight(source, lexer, formatter) - if self.dest == 'html': - return hlsource - else: - if not isinstance(hlsource, unicode): # Py2 / Pygments < 1.6 - hlsource = hlsource.decode() - return hlsource.translate(tex_hl_escape_map_new) except ErrorToken: # this is most probably not the selected language, # so let it pass unhighlighted - return self.unhighlighted(source) + hlsource = highlight(source, lexers['none'], formatter) + if self.dest == 'html': + return hlsource + else: + if not isinstance(hlsource, unicode): # Py2 / Pygments < 1.6 + hlsource = hlsource.decode() + return hlsource.translate(tex_hl_escape_map_new) def get_stylesheet(self): if not pygments: