Skip to content

♻️ refactor(c): house style, decompose run_drain - #482

Merged
gaborbernat merged 5 commits into
tox-dev:mainfrom
gaborbernat:refactor/1.0-house-style
Jul 4, 2026
Merged

gaborbernat merged 5 commits into
tox-dev:mainfrom
gaborbernat:refactor/1.0-house-style

Conversation

@gaborbernat

Copy link
Copy Markdown
Member

This is the consistency-and-house-style workstream of the pre-1.0 review (#478). The C tree had grown a well-built but unevenly-styled surface: file-section banner comments in a minority of files and none in the rest, markdown.c locals that had drifted to single letters against the strict dom/ convention, and a run_drain tree-construction driver that had swollen to roughly 1640 lines of one giant switch. None of this changes what the parser does; all of it makes the code harder to read and to keep consistent as 1.0 approaches.

The headline is run_drain. Its body was a single switch over the twenty WHATWG insertion modes, so following any one mode meant scrolling past the other nineteen. 🧵 Each case now lifts into a drain_<mode> handler that takes the tree, the current token, and a th_insert context carrying the tokenizer handle and the insertion-mode state the algorithm threads across tokens. A handler returns TH_DRAIN_NEXT to advance or TH_DRAIN_REPROCESS to run the same token again under the mode it just set; run_drain keeps the token pump, the shared foreign/template/select pre-dispatch, and the reprocess loop, and now reads top to bottom in call order. The handlers are single-call-site statics the compiler still inlines, so the hot path is unchanged.

Alongside that, a new repository HOUSE_STYLE.md records the conventions the audit found in the good majority, for both the C tree and the Python shim. The banner sweep applies the majority convention uniformly by dropping the section dividers, folding the few that carried a spec or issue reference (the EXSLT families, issue #182/#267, the CSS Level 4 input pseudo-classes) into a plain comment on the declaration. The markdown.c rename restores descriptive local names.

Every change is behavior-preserving: parser output is byte-identical. The shared core/vec.h and core/ascii.h deduplication the workstream also scopes is deferred to a focused follow-up, since it is an all-or-nothing consistency change across heterogeneous allocators and cap types that a partial pass would leave less consistent, not more. Part of #478.

Codify the C-tree and Python-shim conventions the consistency audit found
in the good majority: file-header why-comments, no section banners, no
single-letter names, the th_/turbohtml_ prefix split, one child-iteration
idiom, shared vec.h/ascii.h helpers, and the docstring/__all__/Final rules
for the shim.

part of tox-dev#478
The consistency audit found divider banners in ~28 C files and none in the
rest, including the largest hand-written units. House style bans section and
grouping-header comments, so drop the dividers tree-wide; where a banner
carried a spec or issue reference (EXSLT families, issue tox-dev#182/tox-dev#267, the CSS
Level 4 input pseudo-classes) fold that text into a plain comment on the
declaration it described.

part of tox-dev#478
markdown.c had drifted from the strict dom/ naming convention with
single-letter locals. Rename them to descriptive names: loop indices to
index/scan, the code point to ch, the run/pad/row/column counters, and the
text and count parameters.

part of tox-dev#478
run_drain was a ~1640-line function whose body was one giant switch over
the twenty insertion modes. Lift each case into a drain_<mode> handler that
takes the tree, the current token, and a th_insert context (the tokenizer
handle plus the insertion-mode state the WHATWG algorithm threads across
tokens). A handler returns TH_DRAIN_NEXT to advance or TH_DRAIN_REPROCESS to
run the same token again under the mode it just set; run_drain keeps the
token pump, the shared foreign/template/select pre-dispatch, and the reprocess
loop, and now reads top to bottom in call order.

Behavior is byte-identical: each case body moved verbatim, with break and
goto reprocess rewritten to the two return codes and the threaded state
reached through the context. The handlers are single-call-site statics the
compiler still inlines, so the hot tree-construction path is unchanged.

part of tox-dev#478
@gaborbernat gaborbernat added the enhancement New feature or request label Jul 4, 2026
@gaborbernat
gaborbernat enabled auto-merge (squash) July 4, 2026 18:16
@codspeed

codspeed Bot commented Jul 4, 2026

Copy link
Copy Markdown

Merging this PR will not alter performance

✅ 58 untouched benchmarks
⏩ 18 skipped benchmarks1


Comparing gaborbernat:refactor/1.0-house-style (1dfb871) with main (450635d)

Open in CodSpeed

Footnotes

  1. 18 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports. ↩

@gaborbernat
gaborbernat merged commit 1843a58 into tox-dev:main Jul 4, 2026
44 checks passed
Comment thread HOUSE_STYLE.md

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

@gaborbernat is this file your invention? Is it wired somewhere into the agent configs? Is it documented anywhere? I'd like to learn more and maybe borrow the approach into other projects.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

A bit of both 🤔 but based on my experience using it the last week I think I'll just integrate it into https://turbohtml.readthedocs.io/en/stable/development/index.html

@gaborbernat
gaborbernat deleted the refactor/1.0-house-style branch July 10, 2026 17:35
webknjaz added a commit to webknjaz/ansible--pylibssh that referenced this pull request Aug 5, 2026
This patch establishes the entry-point chain `CLAUDE.md` ->
`AGENTS.md` -> `.agents-ctx/HOUSE_RULES.md`, along with
`.agents-ctx/HOUSE_STYLE.md` and a `.gitignore` that restricts
`.agents-ctx/` to Markdown files. Together these form the initial
scaffolding for the house-style/tribal-knowledge memory that Claude
Code and other agentic tools load when working in this repo.

In this patch, `HOUSE_STYLE.md` documents the only rule so far: no
new `@`-mention imports below `AGENTS.md`. `@`-syntax loads eagerly
at the start of every session regardless of relevance, so letting
it spread would pollute the context window; everything past
`HOUSE_RULES.md` links out with plain Markdown instead, to be
pulled in on demand. This mirrors the approach outlined in [1].

The `.gitignore` also gains a `CLAUDE.local.md` entry, matching how
`ansible/ansible` keeps that personal override file untracked.

The naming and idea behind `HOUSE_STYLE.md` come from Bernát Gábor
(@gaborbernat), as discussed in the PR thread linked below [2].

[1] https://www.humanlayer.dev/blog/writing-a-good-claude-md
[2] tox-dev/turbohtml#482 (comment)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants