Skip to content

fix(docs): trim rustdoc hidden lines via Sphinx doctree literal_block nodes - #28008

Open
rctruta wants to merge 3 commits into
apache:mainfrom
rctruta:fix/rustdoc-trim-regex-backtracking
Open

rctruta wants to merge 3 commits into
apache:mainfrom
rctruta:fix/rustdoc-trim-regex-backtracking

Conversation

@rctruta

@rctruta rctruta commented Dec 3, 2026 •

Copy link
Copy Markdown
Contributor

Which issue does this PR close?

Rationale for this change

docs/rustdoc_trim.py previously matched Rust code blocks on raw Markdown (source-read) with re.compile(r"```rust(?:,ignore)?\s*(.*?)```", re.DOTALL), which:

  1. Mangled the 4-backtick rust ```` example in docs/source/contributor-guide/testing_infrastructure.md by matching its last 3 backticks and closing early on inner /// ```.
  2. Sliced info strings like ```rust,no_run / ```rustfmt and closed early on "```" inside Rust string literals.
  3. Left orphan blank lines at the top of rendered code blocks after # setup imports were removed.
  4. Triggered $O(N^2)$ backtracking on unclosed ```rust fences.

What changes are included in this PR?

  • Move docs/rustdoc_trim.py from source-read (raw Markdown regex) to Sphinx's doctree-read event on parsed literal_block nodes (rust / rust,<attr>), letting Sphinx and myst-parser handle all Markdown/MyST fences and directives directly.
  • Support rustdoc's ## escape and trim leftover leading/trailing blank lines after removing hidden # lines.
  • Add docs/test_rustdoc_trim.py and run it in docs/build.sh.

What is the testing strategy for this PR?

  • Unit and Sphinx + MyST integration tests in docs/test_rustdoc_trim.py (run automatically in docs/build.sh during the Docs CI job).
  • Verified full sphinx-build -W across all 76 Sphinx source files (70 .md and 6 .rst) in docs/source/.

Are there any user-facing changes?

Fixes broken code-block rendering in contributor-guide/testing_infrastructure.md and removes leading blank lines after stripped # setup blocks in the user guide.

@github-actions github-actions Bot added the documentation Improvements or additions to documentation label Dec 3, 2026

@kumarUjjawal kumarUjjawal left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you @rctruta for working on this

I have left few comments please take a look.

Comment thread docs/test_rustdoc_trim.py Resolved
Comment thread docs/rustdoc_trim.py Resolved
Comment thread docs/rustdoc_trim.py Resolved
Comment thread docs/rustdoc_trim.py Resolved
Comment thread docs/rustdoc_trim.py Resolved
Comment thread docs/rustdoc_trim.py Resolved
Comment thread docs/rustdoc_trim.py Resolved
Comment thread docs/rustdoc_trim.py Resolved
Comment thread docs/test_rustdoc_trim.py Resolved
Comment thread docs/test_rustdoc_trim.py Resolved
@rctruta rctruta changed the title fix(docs): parse CommonMark code fences in rustdoc_trim.py fix(docs): trim rustdoc hidden lines via Sphinx doctree literal_block nodes Dec 4, 2026
@rctruta

rctruta commented Dec 4, 2026

Copy link
Copy Markdown
Contributor Author

Thank you @rctruta for working on this

I have left few comments please take a look.

Thank you, @kumarUjjawal!

Letting Sphinx parse the Markdown first, instead of parsing code fences by hand, is much cleaner.
I've addressed your comments and updated the PR title and description to match the new approach.

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

docs/rustdoc_trim.py corrupts 4-backtick Rust fences (testing_infrastructure.md) and non-ignore fence headers

2 participants