summaryrefslogtreecommitdiff
path: root/sphinx/transforms/post_transforms/code.py
diff options
context:
space:
mode:
authorNathan Goldbaum <nathan12343@gmail.com>2019-07-02 10:19:28 -0400
committerNathan Goldbaum <nathan12343@gmail.com>2019-07-07 19:28:45 -0400
commita570ea6c03cf54f4250beeeadb95315a1858c8a4 (patch)
tree98ea0ec9e209a73da40649e64e2bcdcbb11f1799 /sphinx/transforms/post_transforms/code.py
parentef2f1057cdfa4ef3b777761e384b6cda8e40b988 (diff)
downloadsphinx-git-a570ea6c03cf54f4250beeeadb95315a1858c8a4.tar.gz
Fix #6545. Strip doctests for doctest_node blocks.
Diffstat (limited to 'sphinx/transforms/post_transforms/code.py')
-rw-r--r--sphinx/transforms/post_transforms/code.py24
1 files changed, 16 insertions, 8 deletions
diff --git a/sphinx/transforms/post_transforms/code.py b/sphinx/transforms/post_transforms/code.py
index a42de4bf1..bae1216bd 100644
--- a/sphinx/transforms/post_transforms/code.py
+++ b/sphinx/transforms/post_transforms/code.py
@@ -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):