8000 Add clean_howto_functional() to handle howto/functional.rst · python/cpython@7e64ef7 · GitHub
[go: up one dir, main page]

Skip to content

Commit 7e64ef7

Browse files
committed
Add clean_howto_functional() to handle howto/functional.rst
1 parent 5617eba commit 7e64ef7

File tree

2 files changed

+35
-5
lines changed

2 files changed

+35
-5
lines changed

Doc/conf.py

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -419,10 +419,40 @@
419419
codeautolink_warn_on_missing_inventory = False
420420
codeautolink_warn_on_failed_resolve = False
421421
# codeautolink_warn_on_default_parse_fail = True
422-
# codeautolink_custom_blocks = {
423-
# # https://sphinx-codeautolink.readthedocs.io/en/latest/examples.html#doctest-code-blocks
424-
# "pycon3": "sphinx_codeautolink.clean_pycon",
425-
# }
422+
# codeautolink_global_preface = """import itertools"""
423+
424+
def clean_howto_functional(source: str) -> tuple[str, str]:
425+
"""Clean up `=>` syntax used in `howto/functional.rst` to pure Python.
426+
427+
An example of such a code block is::
428+
429+
zip(['a', 'b', 'c'], (1, 2, 3)) =>
430+
('a', 1), ('b', 2), ('c', 3)
431+
"""
432+
if "=>" not in source:
433+
# no such syntax, exit early
434+
return source, source
435+
in_statement = False
436+
clean_lines = []
437+
for line in source.splitlines():
438+
if line.endswith("=>"):
439+
in_statement = True
440+
clean_lines.append(line.removesuffix("=>").rstrip())
441+
elif in_statement:
442+
clean_lines.append("# " + line) # comment out output after the "=>"
443+
else:
444+
clean_lines.append(line) # e.g. an import statement
445+
446+
transformed_source = "\n".join(clean_lines)
447+
# print(f"Cleaned up source from\n{source}\nto\n{transformed_source}")
448+
return source, transformed_source
449+
450+
451+
codeautolink_custom_blocks = {
452+
# https://sphinx-codeautolink.readthedocs.io/en/latest/examples.html#doctest-code-blocks
453+
"python3": clean_howto_functional,
454+
}
455+
426456

427457
from docutils.parsers.rst import Directive
428458

Doc/library/dataclasses.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ Module contents
227227
:meth:`~object.__init__` method, which will be defined as::
228228

229229
def __init__(self, a: int, b: int = 0):
230-
...
230+
...
231231

232232
:exc:`TypeError` will be raised if a field without a default value
233233
follows a field with a default value. This is true whether this

0 commit comments

Comments
 (0)
0