mirror of
https://github.com/sphinx-doc/sphinx.git
synced 2025-02-25 18:55:22 -06:00
Merge pull request #6547 from ngoldbaum/issue-6545
Fix #6545. Strip doctests for doctest_block nodes.
This commit is contained in:
commit
632223b860
@ -9,7 +9,7 @@
|
||||
"""
|
||||
|
||||
import sys
|
||||
from typing import NamedTuple
|
||||
from typing import NamedTuple, Union
|
||||
|
||||
from docutils import nodes
|
||||
from pygments.lexers import PythonConsoleLexer, guess_lexer
|
||||
@ -110,13 +110,21 @@ class TrimDoctestFlagsTransform(SphinxTransform):
|
||||
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)]
|
||||
for lbnode in self.document.traverse(nodes.literal_block): # type: nodes.literal_block
|
||||
if self.is_pyconsole(lbnode):
|
||||
self.strip_doctest_flags(lbnode)
|
||||
|
||||
for dbnode in self.document.traverse(nodes.doctest_block): # type: nodes.doctest_block
|
||||
self.strip_doctest_flags(dbnode)
|
||||
|
||||
@staticmethod
|
||||
def strip_doctest_flags(node):
|
||||
# type: (Union[nodes.literal_block, nodes.doctest_block]) -> None
|
||||
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):
|
||||
|
@ -21,3 +21,8 @@ test-trim_doctest_flags
|
||||
|
||||
>>> datetime.date.now() # doctest: +QUX
|
||||
datetime.date(2008, 1, 1)
|
||||
|
||||
.. doctest_block::
|
||||
|
||||
>>> datetime.date.now() # doctest: +QUUX
|
||||
datetime.date(2008, 1, 1)
|
||||
|
@ -18,6 +18,7 @@ def test_trim_doctest_flags_html(app, status, warning):
|
||||
assert 'BAR' in result
|
||||
assert 'BAZ' not in result
|
||||
assert 'QUX' not in result
|
||||
assert 'QUUX' not in result
|
||||
|
||||
|
||||
@pytest.mark.sphinx('latex', testroot='trim_doctest_flags')
|
||||
@ -29,3 +30,4 @@ def test_trim_doctest_flags_latex(app, status, warning):
|
||||
assert 'BAR' in result
|
||||
assert 'BAZ' not in result
|
||||
assert 'QUX' not in result
|
||||
assert 'QUUX' not in result
|
||||
|
Loading…
Reference in New Issue
Block a user