diff --git a/pydoctor/node2stan.py b/pydoctor/node2stan.py index 21080cc3a..5361f5fb5 100644 --- a/pydoctor/node2stan.py +++ b/pydoctor/node2stan.py @@ -11,7 +11,7 @@ from docutils.writers import html4css1 from docutils import nodes, frontend, __version_info__ as docutils_version_info -from twisted.web.template import Tag +from twisted.web.template import Tag, tags if TYPE_CHECKING: from twisted.web.template import Flattenable from pydoctor.epydoc.markup import DocstringLinker @@ -226,10 +226,27 @@ def starttag(self, node: nodes.Element, tagname: str, suffix: str = '\n', *args: def visit_doctest_block(self, node: nodes.doctest_block) -> None: pysrc = node[0].astext() - if node.get('codeblock'): - self.body.append(flatten(colorize_codeblock(pysrc))) + if is_code_block:=node.get('codeblock'): + pre_tag = colorize_codeblock(pysrc) else: - self.body.append(flatten(colorize_doctest(pysrc))) + pre_tag = colorize_doctest(pysrc) + + # If it's not a code block, then it must be a doctest block + if not is_code_block: + # Wrap doctest blocks with a container and toggle button + container = tags.div( + tags.button( + ">>>", + class_='doctest-toggle', + type='button', + title='Doctest toggle' + ), + pre_tag, + class_='doctest-output' + ) + self.body.append(flatten(container)) + else: + self.body.append(flatten(pre_tag)) raise nodes.SkipNode() diff --git a/pydoctor/test/epydoc/test_epytext2html.py b/pydoctor/test/epydoc/test_epytext2html.py index 0e8970f11..5004f39fa 100644 --- a/pydoctor/test/epydoc/test_epytext2html.py +++ b/pydoctor/test/epydoc/test_epytext2html.py @@ -124,9 +124,9 @@ def test_epytext_complex_list() -> None:
  • This list item contains two paragraphs and a doctest block.

    -
    >>> 
    +        
    >>> 
             len('This is a doctest block')
    -        23

    This is the second paragraph.

  • + 23

    This is the second paragraph.

    ''' assert epytext2html(doc) == squash(expected) diff --git a/pydoctor/test/test_node2stan.py b/pydoctor/test/test_node2stan.py index bf3d7c9a9..530730122 100644 --- a/pydoctor/test/test_node2stan.py +++ b/pydoctor/test/test_node2stan.py @@ -6,8 +6,8 @@ from pydoctor.epydoc.docutils import get_lineno from pydoctor.test import CapSys -from pydoctor.test.epydoc.test_epytext2html import epytext2node -from pydoctor.test.epydoc.test_restructuredtext import rst2node, parse_rst +from pydoctor.test.epydoc.test_epytext2html import epytext2node, epytext2html +from pydoctor.test.epydoc.test_restructuredtext import rst2node, parse_rst, rst2html from pydoctor.node2stan import gettext from docutils import nodes @@ -154,3 +154,44 @@ def test_docutils_get_lineno_title_reference(capsys:CapSys) -> None: parsed_doc.fields[0].body().to_node().walk(TitleReferenceDump(doc)) assert capsys.readouterr().out == r'''||title_reference line: None, get_lineno: 28, rawsource: `link ` ''' + + + +def test_epytext_doctest_contains_toggle() -> None: + doc = ''' + A short paragraph. + + >>> 2 + 3 + 5 + ''' + html = epytext2html(doc) + assert '
    None: + doc = ''' + Some text. + + >>> sum([1, 2, 3]) + 6 + ''' + html = rst2html(doc) + assert '
    None: + doc = ''' + .. code:: python + + >>> 1 + 1 + 2 + ''' + html = rst2html(doc) + # Code blocks should be colorized but must not include the toggle button + assert '