diff --git a/CHANGES b/CHANGES index 38f08351d..822466d54 100644 --- a/CHANGES +++ b/CHANGES @@ -26,6 +26,9 @@ Incompatible changes by ``termsep`` node. In new implementation, each terms are converted into individual ``term`` nodes and ``termsep`` node is removed. By this change, output layout of every builders are changed a bit. +* The default highlight language is now Python 3. This means that source code + is highlighted as Python 3 (which is mostly a superset of Python 2), and no + parsing is attempted to distinguish valid code. Features added -------------- diff --git a/doc/config.rst b/doc/config.rst index 38e4603ab..18f372ff3 100644 --- a/doc/config.rst +++ b/doc/config.rst @@ -320,11 +320,16 @@ Project information .. confval:: highlight_language The default language to highlight source code in. The default is - ``'python'``. The value should be a valid Pygments lexer name, see + ``'python3'``. The value should be a valid Pygments lexer name, see :ref:`code-examples` for more details. .. versionadded:: 0.5 + .. versionchanged:: 1.4 + The default is now ``'python3'``, since it is mostly a superset of + ``'python'``. If you prefer Python 2 only highlighting, you can set + it back to ``'python'``. + .. confval:: highlight_options A dictionary of options that modify how the lexer specified by diff --git a/sphinx/config.py b/sphinx/config.py index 402eb1400..55c17ab43 100644 --- a/sphinx/config.py +++ b/sphinx/config.py @@ -65,7 +65,7 @@ class Config(object): trim_footnote_reference_space = (False, 'env'), show_authors = (False, 'env'), pygments_style = (None, 'html', [str]), - highlight_language = ('python', 'env'), + highlight_language = ('python3', 'env'), highlight_options = ({}, 'env'), templates_path = ([], 'html'), template_bridge = (None, 'html', [str]), diff --git a/sphinx/highlighting.py b/sphinx/highlighting.py index eb008dc4e..37efdc887 100644 --- a/sphinx/highlighting.py +++ b/sphinx/highlighting.py @@ -25,8 +25,8 @@ from sphinx.util.texescape import tex_hl_escape_map_new from sphinx.ext import doctest from pygments import highlight -from pygments.lexers import PythonLexer, PythonConsoleLexer, CLexer, \ - TextLexer, RstLexer +from pygments.lexers import PythonLexer, Python3Lexer, PythonConsoleLexer, \ + CLexer, TextLexer, RstLexer from pygments.lexers import get_lexer_by_name, guess_lexer from pygments.formatters import HtmlFormatter, LatexFormatter from pygments.filters import ErrorToken @@ -37,6 +37,7 @@ from sphinx.pygments_styles import SphinxStyle, NoneStyle lexers = dict( none = TextLexer(stripnl=False), python = PythonLexer(stripnl=False), + python3 = Python3Lexer(stripnl=False), pycon = PythonConsoleLexer(stripnl=False), pycon3 = PythonConsoleLexer(python3=True, stripnl=False), rest = RstLexer(stripnl=False), @@ -151,9 +152,11 @@ class PygmentsBridge(object): lexer = lexers['none'] else: lexer = lexers['python'] - elif lang in ('python3', 'py3') and source.startswith('>>>'): - # for py3, recognize interactive sessions, but do not try parsing... - lexer = lexers['pycon3'] + elif lang in ('py3', 'python3'): + if source.startswith('>>>'): + lexer = lexers['pycon3'] + else: + lexer = lexers['python3'] elif lang == 'guess': try: lexer = guess_lexer(source) @@ -187,7 +190,8 @@ class PygmentsBridge(object): # this is most probably not the selected language, # so let it pass unhighlighted if warn: - warn('Could not parse literal_block as "%s". highlighting skipped.' % lang) + warn('Could not lex literal_block as "%s". ' + 'Highlighting skipped.' % lang) else: raise exc hlsource = highlight(source, lexers['none'], formatter) diff --git a/tests/root/includes.txt b/tests/root/includes.txt index 907b81e93..41b72b0ab 100644 --- a/tests/root/includes.txt +++ b/tests/root/includes.txt @@ -17,10 +17,12 @@ Test file and literal inclusion .. should give a warning .. literalinclude:: wrongenc.inc + :language: none .. should succeed .. literalinclude:: wrongenc.inc :encoding: latin-1 + :language: none .. include:: wrongenc.inc :encoding: latin-1 diff --git a/tests/root/markup.txt b/tests/root/markup.txt index 1700b3844..5cd7d2cbf 100644 --- a/tests/root/markup.txt +++ b/tests/root/markup.txt @@ -23,6 +23,8 @@ Generic reST A |subst| (the definition is in rst_epilog). +.. highlight:: none + .. _label: :: diff --git a/tests/test_build_html.py b/tests/test_build_html.py index 3b4629c64..4ccaf5f38 100644 --- a/tests/test_build_html.py +++ b/tests/test_build_html.py @@ -31,16 +31,16 @@ http://www.python.org/logo.png reading included file u'.*?wrongenc.inc' seems to be wrong, try giving an \ :encoding: option\\n? %(root)s/includes.txt:4: WARNING: download file not readable: .*?nonexisting.png -(%(root)s/markup.txt:357: WARNING: invalid single index entry u'')? +(%(root)s/markup.txt:359: WARNING: invalid single index entry u'')? (%(root)s/undecodable.txt:3: WARNING: undecodable source characters, replacing \ with "\\?": b?'here: >>>(\\\\|/)xbb<<<' )?""" HTML_WARNINGS = ENV_WARNINGS + """\ %(root)s/images.txt:20: WARNING: no matching candidate for image URI u'foo.\\*' -%(root)s/markup.txt:269: WARNING: Could not parse literal_block as "c". highlighting skipped. +%(root)s/markup.txt:271: WARNING: Could not lex literal_block as "c". Highlighting skipped. %(root)s/footnote.txt:60: WARNING: citation not found: missing -%(root)s/markup.txt:158: WARNING: unknown option: &option +%(root)s/markup.txt:160: WARNING: unknown option: &option """ if PY3: diff --git a/tests/test_build_latex.py b/tests/test_build_latex.py index e73356688..73fa17275 100644 --- a/tests/test_build_latex.py +++ b/tests/test_build_latex.py @@ -24,10 +24,10 @@ from test_build_html import ENV_WARNINGS LATEX_WARNINGS = ENV_WARNINGS + """\ -%(root)s/markup.txt:158: WARNING: unknown option: &option +%(root)s/markup.txt:160: WARNING: unknown option: &option %(root)s/footnote.txt:60: WARNING: citation not found: missing %(root)s/images.txt:20: WARNING: no matching candidate for image URI u'foo.\\*' -%(root)s/markup.txt:269: WARNING: Could not parse literal_block as "c". highlighting skipped. +%(root)s/markup.txt:271: WARNING: Could not lex literal_block as "c". Highlighting skipped. """ if PY3: diff --git a/tests/test_build_texinfo.py b/tests/test_build_texinfo.py index 4f6813653..1cf2232c4 100644 --- a/tests/test_build_texinfo.py +++ b/tests/test_build_texinfo.py @@ -23,7 +23,7 @@ from test_build_html import ENV_WARNINGS TEXINFO_WARNINGS = ENV_WARNINGS + """\ -%(root)s/markup.txt:158: WARNING: unknown option: &option +%(root)s/markup.txt:160: WARNING: unknown option: &option %(root)s/footnote.txt:60: WARNING: citation not found: missing %(root)s/images.txt:20: WARNING: no matching candidate for image URI u'foo.\\*' %(root)s/images.txt:29: WARNING: no matching candidate for image URI u'svgimg.\\*'