The default highlight language is now Python 3.

References: #2279.

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.
This commit is contained in:
Georg Brandl 2016-02-14 09:21:35 +01:00
parent 7a4f914f91
commit c1b7b6f369
9 changed files with 30 additions and 14 deletions

View File

@ -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
--------------

View File

@ -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

View File

@ -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]),

View File

@ -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...
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)

View File

@ -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

View File

@ -23,6 +23,8 @@ Generic reST
A |subst| (the definition is in rst_epilog).
.. highlight:: none
.. _label:
::

View File

@ -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:

View File

@ -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:

View File

@ -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.\\*'