diff options
Diffstat (limited to 'sphinx/transforms/post_transforms/code.py')
-rw-r--r-- | sphinx/transforms/post_transforms/code.py | 13 |
1 files changed, 6 insertions, 7 deletions
diff --git a/sphinx/transforms/post_transforms/code.py b/sphinx/transforms/post_transforms/code.py index f6ddb521f..b5b2a95de 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 @@ -94,9 +94,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) @@ -104,8 +101,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) |