Skip to content

Add docs hook: collect user documentation into a generated file - #115

Merged
jhidding merged 4 commits into
mainfrom
docs-hook
Nov 25, 2026
Merged

jhidding merged 4 commits into
mainfrom
docs-hook

Conversation

@jhidding

Copy link
Copy Markdown
Contributor

Closes #114.

Summary

The new docs hook collects user documentation from across the chapters into a single generated Markdown file. A snippet can be written in either of two forms:

::: {.docs file=reference.md section="The mel language" order=10}
`mel("...")` reads a melody from text.

```python
mel("g4:q bb4 d5")
```
:::

``` {.markdown .docs file=reference.md section="Patches"}
Patches are defined with `patch`.
```
  • Every snippet with the same file goes into that file. Snippets without a section come first, then each section follows under a heading (level set by the section_level hook config, default 2). Snippets are sorted by order (default 0), then by reading order. A section's position comes from the lowest order among its snippets.
  • Fenced divs are read as blocks only when the docs hook is enabled. Code fences and nested divs inside them are copied verbatim and are not tangled. Projects that don't enable the hook behave exactly as before.
  • The generated file is an ordinary annotated tangle target (<!-- ~/~ begin <<chapters/a.md#...>>[init] -->), so entangled stitch maps edits back to the chapters.

Changes

  • entangled/readers/markdown.py: new reader for fenced divs; the namespace logic is factored into a shared helper.
  • entangled/hooks/docs.py: the new hook.
  • entangled/hooks/base.py, entangled/interface/document.py: new HookBase.post_read(refs), called at the end of Document.load. The docs target has to be registered there instead of in pre_tangle, because stitch never calls pre_tangle.
  • entangled/config/language.py: adds Markdown (<!-- --> comments) to the default languages.

Caveats

  • Snippets without an #id are named unnamed-<file>:<line>. If a chapter is edited above a snippet and the generated file is then stitched before re-tangling, the names won't match. The docs should recommend giving snippets an #id.
  • Each snippet goes to a single target. The :::docs shorthand is not supported, since it has no way to give the file attribute.
  • The documentation page for the hook is not written yet.

Tests

test/hooks/test_docs_hook.py covers ordering, sections, nested fences and divs, stitching back, and leaving divs alone when the hook is disabled. The full suite passes (103 tests).

🤖 Generated with Claude Code

jhidding and others added 4 commits November 24, 2026 17:10
Snippets marked with the `docs` class and a `file` attribute, either as
Pandoc fenced divs (`::: {.docs file=...}`) or as Markdown code blocks
(``` {.markdown .docs file=...} ```), are collected into one Markdown
target, grouped by `section` and sorted by `order`. Fenced divs are only
read when the hook is enabled; code inside them is not tangled.

The generated file is a regular annotated target, so edits can be
stitched back. To support this, hooks get a `post_read` method that is
called after loading, before both tangle and stitch.

Also adds Markdown to the default languages.

Closes #114

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
On Windows the names contained backslashes, so references to them from
the generated docs file did not resolve.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@jhidding
jhidding merged commit dfb0126 into main Nov 25, 2026
9 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[feature] Documentation hook

1 participant