Apply :confval:trim_doctest_flags to all builders (cf. text, manpages)

This commit is contained in:
Takeshi KOMIYA 2018-07-21 11:25:54 +09:00
parent 4f296c5e67
commit f3019ee197
8 changed files with 64 additions and 11 deletions

View File

@ -113,6 +113,8 @@ Deprecated
* ``sphinx.ext.mathbase.is_in_section_title()`` is deprecated * ``sphinx.ext.mathbase.is_in_section_title()`` is deprecated
* ``sphinx.ext.mathbase.MathDomain`` is deprecated * ``sphinx.ext.mathbase.MathDomain`` is deprecated
* ``sphinx.highlighting.PygmentsBridge.unhighlight()`` is deprecated * ``sphinx.highlighting.PygmentsBridge.unhighlight()`` is deprecated
* The ``trim_doctest_flags`` argument of ``sphinx.highlighting.PygmentsBridge``
is deprecated
For more details, see `deprecation APIs list For more details, see `deprecation APIs list
<http://www.sphinx-doc.org/en/master/extdev/index.html#deprecated-apis>`_ <http://www.sphinx-doc.org/en/master/extdev/index.html#deprecated-apis>`_
@ -173,6 +175,7 @@ Features added
if :confval:`latex_engine` is ``'xelatex'`` or ``'lualatex'``. if :confval:`latex_engine` is ``'xelatex'`` or ``'lualatex'``.
* #4976: ``SphinxLoggerAdapter.info()`` now supports ``location`` parameter * #4976: ``SphinxLoggerAdapter.info()`` now supports ``location`` parameter
* #5122: setuptools: support nitpicky option * #5122: setuptools: support nitpicky option
* Apply :confval:`trim_doctest_flags` to all builders (cf. text, manpages)
Bugs fixed Bugs fixed
---------- ----------

View File

@ -136,6 +136,12 @@ The following is a list of deprecated interface.
- 3.0 - 3.0
- N/A - N/A
* - ``trim_doctest_flags`` arguments of
``sphinx.highlighting.PygmentsBridge``
- 1.8
- 3.0
- N/A
* - ``sphinx.ext.mathbase.MathDomain`` * - ``sphinx.ext.mathbase.MathDomain``
- 1.8 - 1.8
- 3.0 - 3.0

View File

@ -386,8 +386,7 @@ class StandaloneHTMLBuilder(Builder):
style = self.theme.get_config('theme', 'pygments_style', 'none') style = self.theme.get_config('theme', 'pygments_style', 'none')
else: else:
style = 'sphinx' style = 'sphinx'
self.highlighter = PygmentsBridge('html', style, self.highlighter = PygmentsBridge('html', style)
self.config.trim_doctest_flags)
def init_css_files(self): def init_css_files(self):
# type: () -> None # type: () -> None

View File

@ -169,8 +169,7 @@ class LaTeXBuilder(Builder):
def write_stylesheet(self): def write_stylesheet(self):
# type: () -> None # type: () -> None
highlighter = highlighting.PygmentsBridge( highlighter = highlighting.PygmentsBridge('latex', self.config.pygments_style)
'latex', self.config.pygments_style, self.config.trim_doctest_flags)
stylesheet = path.join(self.outdir, 'sphinxhighlight.sty') stylesheet = path.join(self.outdir, 'sphinxhighlight.sty')
with open(stylesheet, 'w') as f: with open(stylesheet, 'w') as f:
f.write('\\NeedsTeXFormat{LaTeX2e}[1995/12/01]\n') f.write('\\NeedsTeXFormat{LaTeX2e}[1995/12/01]\n')

View File

@ -68,7 +68,7 @@ class PygmentsBridge(object):
html_formatter = HtmlFormatter html_formatter = HtmlFormatter
latex_formatter = LatexFormatter latex_formatter = LatexFormatter
def __init__(self, dest='html', stylename='sphinx', trim_doctest_flags=False): def __init__(self, dest='html', stylename='sphinx', trim_doctest_flags=None):
# type: (unicode, unicode, bool) -> None # type: (unicode, unicode, bool) -> None
self.dest = dest self.dest = dest
if stylename is None or stylename == 'sphinx': if stylename is None or stylename == 'sphinx':
@ -81,7 +81,6 @@ class PygmentsBridge(object):
stylename) stylename)
else: else:
style = get_style_by_name(stylename) style = get_style_by_name(stylename)
self.trim_doctest_flags = trim_doctest_flags
self.formatter_args = {'style': style} # type: Dict[unicode, Any] self.formatter_args = {'style': style} # type: Dict[unicode, Any]
if dest == 'html': if dest == 'html':
self.formatter = self.html_formatter self.formatter = self.html_formatter
@ -89,6 +88,11 @@ class PygmentsBridge(object):
self.formatter = self.latex_formatter self.formatter = self.latex_formatter
self.formatter_args['commandprefix'] = 'PYG' self.formatter_args['commandprefix'] = 'PYG'
self.trim_doctest_flags = trim_doctest_flags
if trim_doctest_flags is not None:
warnings.warn('trim_doctest_flags option for PygmentsBridge is now deprecated.',
RemovedInSphinx30Warning)
def get_formatter(self, **kwargs): def get_formatter(self, **kwargs):
# type: (Any) -> Formatter # type: (Any) -> Formatter
kwargs.update(self.formatter_args) # type: ignore kwargs.update(self.formatter_args) # type: ignore

View File

@ -13,9 +13,11 @@ import sys
from typing import NamedTuple from typing import NamedTuple
from docutils import nodes from docutils import nodes
from pygments.lexers import PythonConsoleLexer, guess_lexer
from six import text_type from six import text_type
from sphinx import addnodes from sphinx import addnodes
from sphinx.ext import doctest
from sphinx.transforms import SphinxTransform from sphinx.transforms import SphinxTransform
if False: if False:
@ -95,9 +97,51 @@ class HighlightLanguageVisitor(nodes.NodeVisitor):
node['linenos'] = (lines >= setting.lineno_threshold - 1) node['linenos'] = (lines >= setting.lineno_threshold - 1)
class TrimDoctestFlagsTransform(SphinxTransform):
"""
Trim doctest flags like ``# doctest: +FLAG`` from python code-blocks.
see :confval:`trim_doctest_flags` for more information.
"""
default_priority = HighlightLanguageTransform.default_priority + 1
def apply(self):
if not self.config.trim_doctest_flags:
return
for node in self.document.traverse(nodes.literal_block):
if self.is_pyconsole(node):
source = node.rawsource
source = doctest.blankline_re.sub('', source)
source = doctest.doctestopt_re.sub('', source)
node.rawsource = source
node[:] = [nodes.Text(source)]
@staticmethod
def is_pyconsole(node):
# type: (nodes.literal_block) -> bool
if node.rawsource != node.astext():
return False # skip parsed-literal node
language = node.get('language')
if language in ('pycon', 'pycon3'):
return True
elif language in ('py', 'py3', 'python', 'python3', 'default'):
return node.rawsource.startswith('>>>')
elif language == 'guess':
try:
lexer = guess_lexer(node.rawsource)
return isinstance(lexer, PythonConsoleLexer)
except Exception:
pass
return False
def setup(app): def setup(app):
# type: (Sphinx) -> Dict[unicode, Any] # type: (Sphinx) -> Dict[unicode, Any]
app.add_post_transform(HighlightLanguageTransform) app.add_post_transform(HighlightLanguageTransform)
app.add_post_transform(TrimDoctestFlagsTransform)
return { return {
'version': 'builtin', 'version': 'builtin',

View File

@ -697,9 +697,7 @@ class LaTeXTranslator(nodes.NodeVisitor):
self.babel_defmacro('\\pageautorefname', self.encode(_('page'))) self.babel_defmacro('\\pageautorefname', self.encode(_('page')))
self.elements['numfig_format'] = self.generate_numfig_format(builder) self.elements['numfig_format'] = self.generate_numfig_format(builder)
self.highlighter = highlighting.PygmentsBridge( self.highlighter = highlighting.PygmentsBridge('latex', builder.config.pygments_style)
'latex',
builder.config.pygments_style, builder.config.trim_doctest_flags)
self.context = [] # type: List[Any] self.context = [] # type: List[Any]
self.descstack = [] # type: List[unicode] self.descstack = [] # type: List[unicode]
self.table = None # type: Table self.table = None # type: Table

View File

@ -1,7 +1,7 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
""" """
test_config_trim_doctest_flags test_transforms_post_transforms_code
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
:copyright: Copyright 2007-2018 by the Sphinx team, see AUTHORS. :copyright: Copyright 2007-2018 by the Sphinx team, see AUTHORS.
:license: BSD, see LICENSE for details. :license: BSD, see LICENSE for details.