|
419 | 419 | codeautolink_warn_on_missing_inventory = False
|
420 | 420 | codeautolink_warn_on_failed_resolve = False
|
421 | 421 | # 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 | + |
426 | 456 |
|
427 | 457 | from docutils.parsers.rst import Directive
|
428 | 458 |
|
|
0 commit comments