summaryrefslogtreecommitdiff
path: root/sphinx/transforms/post_transforms/code.py
diff options
context:
space:
mode:
authorTakeshi KOMIYA <i.tkomiya@gmail.com>2020-08-01 14:31:42 +0900
committerTakeshi KOMIYA <i.tkomiya@gmail.com>2020-08-05 23:39:05 +0900
commit059dc108bab759711acb9491dfb4162e7cce8d4c (patch)
tree927ea6a586964f094451a52fc54b5d761acabafa /sphinx/transforms/post_transforms/code.py
parenta721631a541db298f7f9807c9db8433e928cb20b (diff)
downloadsphinx-git-059dc108bab759711acb9491dfb4162e7cce8d4c.tar.gz
Close #6698: doctest: Add :no-trim-doctest-flags: options
To control trimming doctest flags manually, this adds new options :trim-doctest-flags: and :no-trim-doctest-flags: to doctest directives. It helps to describes doctest module itself in python doc (see #6698).
Diffstat (limited to 'sphinx/transforms/post_transforms/code.py')
-rw-r--r--sphinx/transforms/post_transforms/code.py13
1 files changed, 6 insertions, 7 deletions
diff --git a/sphinx/transforms/post_transforms/code.py b/sphinx/transforms/post_transforms/code.py
index add35647b..2012d6e11 100644
--- a/sphinx/transforms/post_transforms/code.py
+++ b/sphinx/transforms/post_transforms/code.py
@@ -9,10 +9,10 @@
"""
import sys
-from typing import Any, Dict, List, NamedTuple, Union
+from typing import Any, Dict, List, NamedTuple
from docutils import nodes
-from docutils.nodes import Node
+from docutils.nodes import Node, TextElement
from pygments.lexers import PythonConsoleLexer, guess_lexer
from sphinx import addnodes
@@ -93,9 +93,6 @@ class TrimDoctestFlagsTransform(SphinxTransform):
default_priority = HighlightLanguageTransform.default_priority + 1
def apply(self, **kwargs: Any) -> None:
- if not self.config.trim_doctest_flags:
- return
-
for lbnode in self.document.traverse(nodes.literal_block): # type: nodes.literal_block
if self.is_pyconsole(lbnode):
self.strip_doctest_flags(lbnode)
@@ -103,8 +100,10 @@ class TrimDoctestFlagsTransform(SphinxTransform):
for dbnode in self.document.traverse(nodes.doctest_block): # type: nodes.doctest_block
self.strip_doctest_flags(dbnode)
- @staticmethod
- def strip_doctest_flags(node: Union[nodes.literal_block, nodes.doctest_block]) -> None:
+ def strip_doctest_flags(self, node: TextElement) -> None:
+ if not node.get('trim_flags', self.config.trim_doctest_flags):
+ return
+
source = node.rawsource
source = doctest.blankline_re.sub('', source)
source = doctest.doctestopt_re.sub('', source)