Skip to content

Some quality-of-life enhancements to toc.py CLI script#329

Open
edoardob90 wants to merge 13 commits intomainfrom
feature/document-toc-py
Open

Some quality-of-life enhancements to toc.py CLI script#329
edoardob90 wants to merge 13 commits intomainfrom
feature/document-toc-py

Conversation

@edoardob90
Copy link
Copy Markdown
Member

Hopefully closes #240.

Add proper error handling, logging, command-line options, and improved documentation. Clean anchor generation, add version, and enhance CLI help text.

Add proper error handling, logging, command-line options, and improved
documentation. Clean anchor generation, add version, and enhance
CLI help text with better descriptions and examples.
@edoardob90 edoardob90 added documentation Improvements or additions to documentation enhancement New feature or request labels May 13, 2025
Replace f-strings with %-style formatting in logger calls for better
performance. Use logger.exception() for better error reporting with
tracebacks.
@despadam
Copy link
Copy Markdown
Contributor

There is also: #131. Should that one be closed?

@edoardob90 edoardob90 mentioned this pull request May 13, 2025
2 tasks
@edoardob90
Copy link
Copy Markdown
Member Author

There is also: #131. Should that one be closed?

Done, closed. We can track all the relevant changes here.

Upgrade TocEntry from namedtuple to NamedTuple with type hints.
Improve logic for detecting and ignoring markdown headers inside
code blocks to prevent incorrect TOC entries. Also refactor the
file output handling for better organization when using --force.
@edoardob90 edoardob90 marked this pull request as ready for review May 13, 2025 19:52
@edoardob90 edoardob90 requested a review from yakutovicha May 13, 2025 19:54
- Add PEP 723 inline script metadata so the script runs standalone via
  `uv run toc.py` without any manual dependency installation
- Replace argparse with Typer: grouped help panels (Output / TOC Options
/
  Misc), path validation delegated to Typer/Click, eager --version
callback
- Replace logging with Rich: console for stdout, stderr console for
warnings,
  print_exception() for errors
- Add APP_HELP embedded documentation covering how the tool works, the
  placeholder cell requirement, all output modes, and usage examples
- build_toc() now returns a 3-tuple (notebook, toc_replaced,
has_headings)
  so all I/O stays in main(); remove try/except from extract_toc()
- Add toc_fixture.ipynb test fixture covering: fenced code blocks with #
  comments, multiple consecutive code blocks, language-spec fences,
anchor
  generation edge cases, non-heading # patterns, and deep nesting
- Bump version to 0.2.0
Jupyter only generates working anchor IDs for headings when each heading
is in its own markdown cell. This adds --split-cells / -s which rewrites
multi-heading cells into one cell per heading before TOC generation, so
every link in the table of contents works correctly.

New functions:
- split_cell(): splits a single cell source at each heading boundary,
  respecting fenced code blocks and skipping the TOC header line
- split_multi_heading_cells(): applies split_cell() across all markdown
  cells in a notebook, returning the modified notebook and a count of
  cells that were split

build_toc() gains a split_cells parameter and returns a 4-tuple that
includes the split count. main() passes the new --split-cells flag
through and prints a summary line when cells were split.

Also adds a dedicated multi-heading cell to toc_fixture.ipynb to
exercise the splitting path, and bumps the version to 0.3.0.
Anchor format fix:
JupyterLab sets data-jupyter-id on headings by replacing spaces with
hyphens and preserving everything else verbatim. The previous
implementation lowercased text and stripped all non-word characters,
producing anchors that never matched. Fix: strip backticks only (they
are markdown syntax, absent from rendered text) and replace spaces with
hyphens, preserving case and all other characters.

CLI improvements:
- --output and --force are now mutually exclusive; passing both exits
  with a clear error message
- --output paths missing the .ipynb extension have it appended
  automatically
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

documentation Improvements or additions to documentation enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Document the behaviour of the toc.py tool

2 participants