Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 19 additions & 2 deletions Lib/pydoc.py
Original file line number Diff line number Diff line change
Expand Up @@ -723,12 +723,15 @@ def markup(self, text, escape=None, funcs={}, classes={}, methods={}):
pattern = re.compile(r'\b((http|https|ftp)://\S+[\w/]|'
r'RFC[- ]?(\d+)|'
r'PEP[- ]?(\d+)|'
r'(self\.)?(\w+))')
r'(self\.)?(\w+))|'
r'\n*^(?P<indent>[ ]*)>>>(?:[ ].*)?$'
r'(?:\n(?P=indent)(?=.*\S).*)*\n*',
re.MULTILINE)
while match := pattern.search(text, here):
start, end = match.span()
results.append(escape(text[here:start]))

all, scheme, rfc, pep, selfdot, name = match.groups()
all, scheme, rfc, pep, selfdot, name, indent = match.groups()
if scheme:
url = escape(all).replace('"', '&quot;')
results.append('<a href="%s">%s</a>' % (url, url))
Expand All @@ -738,6 +741,20 @@ def markup(self, text, escape=None, funcs={}, classes={}, methods={}):
elif pep:
url = 'https://peps.python.org/pep-%04d/' % int(pep)
results.append('<a href="%s">%s</a>' % (url, escape(all)))
elif indent is not None:
lines = []
for line in match.group().strip('\n').split('\n'):
line = line.removeprefix(indent)
if prompt := re.match(r'(>>>|\.\.\.)(?: |$)', line):
line = ('<span class="prompt">%s</span>'
% escape(prompt.group())
+ self.markup(line[prompt.end():], escape,
funcs, classes, methods))
else:
line = self.markup(line, escape,
funcs, classes, methods)
lines.append(line)
results.append('<pre class="repl">%s</pre>' % '\n'.join(lines))
elif selfdot:
# Create a link for methods like 'self.method(...)'
# and use <strong> for attributes like 'self.attr'
Expand Down
12 changes: 11 additions & 1 deletion Lib/pydoc_data/_pydoc.css
Original file line number Diff line number Diff line change
Expand Up @@ -125,16 +125,26 @@ section section {
.docstring {
white-space: pre-wrap;
margin: 0.3em 0;
max-width: 50em;
}

pre.topic, pre.keyword {
pre.topic, pre.keyword, pre.repl {
background-color: var(--block-background-color);
border: 1px solid var(--block-border-color);
border-radius: 3px;
padding: 0.5em 0.8em;
overflow-x: auto;
}

pre.repl {
font-size: inherit;
margin: 0.5em 0;
}

.prompt {
color: var(--muted-color);
}

/* Documented names (functions, methods, data) */

dl.doc {
Expand Down
23 changes: 23 additions & 0 deletions Lib/test/test_pydoc/test_pydoc.py
Original file line number Diff line number Diff line change
Expand Up @@ -1966,6 +1966,29 @@ def a_fn_with_https_link():
html
)

@requires_docstrings
def test_html_for_repl_examples(self):
def a_fn_with_repl_example():
"""Do something.

Example:

>>> a_fn_with_repl_example()
... # comment
result
Done.
"""
pass

html = pydoc.HTMLDoc().document(a_fn_with_repl_example)
self.assertIn(
'Example:<pre class="repl">'
'<span class="prompt">&gt;&gt;&gt; </span>a_fn_with_repl_example()\n'
'<span class="prompt">... </span># comment\n'
'result</pre>Done.',
html
)

def test_module_none(self):
# Issue #128772
from test.test_pydoc import module_none
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Render interactive examples in docstrings as separate blocks in the HTML output
of :mod:`pydoc`.
Loading