Skip to content

stage-site: --local-links, a consumer site's links to lectures it carries made links within it - #102

Merged
mmcky merged 8 commits into
mainfrom
stage-site/local-links
Nov 27, 2026
Merged

mmcky merged 8 commits into
mainfrom
stage-site/local-links

Conversation

@mmcky

@mmcky mmcky commented Nov 26, 2026 •

Copy link
Copy Markdown
Contributor

On Matt's "(b) rewrite at staging" (2026-11-26, relayed by the organiser session), for QuantEcon/project-monorepo#22: a consumer site's links to lectures it carries become links within the site, in the staged copy only. No site turns it on in this pull request. It changes what readers see, so dp sets local_links: true at its cutover.

Why

dp's copies are byte for byte their sources' own, so their links to the home series' sites take a reader away from dp for a lecture dp itself has. QuantEcon/project-monorepo#22's check counted 31: 5 prefixed {doc} links ({doc}`intro:short_path` three times, {doc}`intermediate:perm_income_cons` twice) and 26 markdown links by address ([this lecture](https://python.quantecon.org/os_stochastic.html)), 18 of them through python-intro.quantecon.org. dp is the only site with such links.

What it adds

  • tools/stage-site --local-links, for a consumer site alone (class: consumer in sync/manifest.yml; refused otherwise, with nothing copied). In the pool lectures the site's _toc.yml lists, it makes two rewrites:

    • a prefixed {doc} link to a lecture the site carries, whose prefix the site's intersphinx_mapping has, loses its prefix;
    • a markdown link to such a lecture's page on a host the site maps, the site's own, or an older address that still forwards (OLD_ADDRESSES: python-intro.quantecon.org) becomes [text](<page>.md).

    It reads prose only: code cells, literal blocks, inline literals and % comments are blanked before matching, as pool-check reads them. An address with an anchor is left alone. Each rewritten lecture's original goes to _build/stage-site-originals/ and into the record, and --clean puts it back only if its bytes are still the ones staging wrote, as it removes a copy. So the pool's copies stay verbatim, and the drift-check never sees a change.

  • site.yml's optional local_links (true or false). pool-check's site-yml rule allows true on a consumer's site alone. tools/site-matrix passes it to the leg, and site-build.yml stages with --local-links when it is true.

  • Docs: sites/README.md gets a section and two table rows; the workflow's notes mention it.

Checked

  • On the pool (c792764, and again at fb579c7 after the review fixes): tools/stage-site --local-links dp rewrote exactly dp's 31 links, in 13 lectures. Each is a prefix dropped or an address made a .md link, and nothing else changed. --clean then put all 13 back, and git status showed no pool file changed. A site that isn't a consumer is refused.
  • Tests: 357 passed and 1 skipped (at 31b7dbb). Twenty-seven are new; the first four: the rewrite of each form, with six forms left alone (a lecture the site doesn't carry, an anchor, an unmapped host, an inline literal, an unmapped prefix, a code cell), and --clean restoring the bytes; the consumer-only refusal, with a lecture edited after staging kept; the site.yml rule; the matrix field.
  • pool-check is clean, and tools/site-matrix gives every site local_links: false.

After review (6980406)

  • site-build.yml's note no longer says tools/stage-site needs the standard library alone: it needs PyYAML for --local-links, which every leg's environment has, since Jupyter Book needs it too. Without it, --local-links now fails with a plain error, not a traceback.
  • --clean confines each lecture's original to _build/stage-site-originals/, as it confines every path to lectures/, and checks every path the record names before it touches anything.
  • --clean's messages say where a kept lecture's original was left, and a lecture whose original is gone is reported as kept as it is (the old message contradicted itself).
  • Staging refuses when a lecture's original is left from an earlier --clean, so it never replaces one.
  • Ten more tests: % comments; plain, language and tilde fences; a {code-block} fence; a link in a {note} body beside a nested code cell; a single-backtick literal with a link after it; a line opening with inline code; the site's own address, the site's own page ({doc}`intro:intro` stays) and a nested toc, through the tool; --clean with the original gone, and with a record naming a path outside its folder.
  • Docs: the docstring's "never overwrites" now says the rewritten lectures are the only pool files changed in place, and that {math} bodies, four-space blocks and ::: fences read as prose, as pool-check reads them (no pool lecture has a link in one, 2026-11-27); README's tools table has a --local-links row; sites/README.md dates its count of dp's links.

After Copilot's review (1972dc5)

  • A lecture the site carries is a file directly in lectures/. A _toc.yml entry such as ../notes/page, a subfolder's page, or a pool symlink that leads out is not read, so nothing outside the pool is ever rewritten.
  • Inline literals are read a paragraph at a time, as CommonMark reads them: one may run across a line break, but not across a blank line, a heading or a fenced block. Over the pool this changes the reading of 7 lines in 2 lectures, none with a link (one is a real two-line literal in arellano.md).
  • --clean leaves a rewritten lecture that is no longer a file as it is and keeps its original, as it does for one edited since staging.
  • Seven more tests, and the four that test a fix fail on 6980406. dp's plan on the pool at fb579c7 is unchanged: the same 31 links in 13 lectures, byte for byte.

After the second review (4dd284e, on main 63d02d6)

  • A lecture the site carries is a name in the pool's own listing, spelled as it is there, of a file that is not a link. Before, a second name for one lecture (a link to it in the pool, or a case variant on macOS's case-insensitive disk) made two plan entries for one file, and --clean put the rewritten text back.
  • A paragraph also ends at a line that starts a block of its own: a list item, a target, +++, a $$ line, a ::: fence, a thematic break or setext underline, as well as a heading. This reading is close to CommonMark's but not the same. What is still read together (a blockquote's start, table rows, HTML, a deeply nested list item) can shift a stray backtick's pairing either way: it can mask a link, or leave one inside a literal to be rewritten. The docstring says so (corrected in 31b7dbb, see below). No pool lecture has a literal that holds a link, as markdown-it reads them.
  • A literal opens at a whole run of backticks, as pool-check reads one. --clean never restores through a link left in a lecture's place.
  • Five more tests; each fix's test fails on 1972dc5. The pool reads exactly as at 1972dc5, and dp's plan is unchanged.
  • 31b7dbb: that docstring fix, plus two tests. One checks that an item's indented next lines continue it. The other makes the link-in-a-lecture's-place test fail on 1972dc5 by writing through the link. 357 passed.

Not yet tested here: a strict Jupyter Book build of dp with the rewrite on. That comes when dp turns it on; its shadow comparison (QuantEcon/project-monorepo#26) should then list the 31 links as its expected differences. If a rewritten .md link didn't resolve under -n -W, that build would fail, not ship.

Copilot: not requested by me (Matt, 2026-11-22). Its review at 6980406 raised three points. Each is fixed in 1972dc5, and its thread has a reply.

🤖 Generated with Claude Code

https://claude.ai/code/session_01WT8bMZxoF4m8xKRb8BcHnG

mmcky and others added 4 commits November 26, 2026 22:23
…ries made links within it

dp's copies are byte for byte their sources', so 31 of their links
take a reader to another series' site for a lecture dp itself has: 5
prefixed {doc} links and 26 markdown links by address, 18 of them
through python-intro.quantecon.org. On the owner's "(b) rewrite at
staging" (2026-11-26, QuantEcon/project-monorepo#22), staging can now
rewrite them in the staged copy only:

- tools/stage-site --local-links, for a consumer site alone: a prefixed
  {doc} link to a lecture the site carries, whose prefix the site maps,
  loses its prefix; a markdown link to such a lecture's page on a host
  the site maps, its own, or an older address that still forwards
  becomes a link to the lecture's file. Prose only, never an address
  with an anchor. Originals are kept under _build/ and --clean puts
  them back by the record, as it removes a copy. On the pool today it
  rewrites exactly dp's 31 links, in 13 lectures, and --clean restores
  every byte.
- site.yml's optional local_links (true or false), which only a consumer
  may set (pool-check's site-yml); tools/site-matrix passes it to the
  leg, and site-build.yml stages with --local-links when it is true.
- No site sets it yet: it is what readers see, so dp turns it on at its
  cutover.
- Tests for the rewrite and its clean-up, the refusals, the site.yml
  rule and the matrix field; sites/README.md documents it.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01WT8bMZxoF4m8xKRb8BcHnG
…se case

- site-build.yml: tools/stage-site needs PyYAML for --local-links, which
  every leg's environment has, since Jupyter Book needs it too; without it,
  --local-links fails with a plain error, not a traceback.
- --clean confines each lecture's original to _build/stage-site-originals/,
  as it confines each path to lectures/, and checks every path the record
  names before it touches anything.
- --clean's messages say where a kept lecture's original was left, and that
  a lecture whose original is gone is kept as it is.
- Staging refuses when a lecture's original is left from an earlier --clean,
  so it never replaces one.
- Tests: % comments; plain, language and tilde fences; a link in a {note}
  body beside a nested code cell; a single-backtick literal; a line opening
  with inline code; the site's own address; the site's own page; a nested
  toc; --clean with the original gone, and with a record outside its folder.
- Docs: the docstring's "never overwrites" is now precise, and says that
  {math} bodies, four-space blocks and ::: fences read as prose, as
  pool-check reads them; README's tools table has a --local-links row;
  sites/README.md dates its count of dp's links (31, in 13 lectures, on
  2026-11-27).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01WT8bMZxoF4m8xKRb8BcHnG

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟡 Changes recommended

Unresolved issues allow TOC paths to modify files outside the pool, rewrite literal content in multiline code spans, and restore deleted lectures during cleanup.

3 open issues
What changed in this PR

Adds opt-in staging-time link rewriting so consumer sites can keep readers on lectures they carry without permanently changing pool sources. No site enables the option in this PR.

Changes:

  • Adds stage-site --local-links, with original-file backups and guarded cleanup.
  • Validates local_links in site metadata and passes it through the build matrix and workflow.
  • Adds regression coverage and documents usage and rollout behavior.
File Description
tools/​tests/​test_sites.py Tests rewriting, exclusions, cleanup, and consumer-only validation.
tools/​tests/​test_site_matrix.py Tests option propagation, defaults, and invalid values.
tools/​stage-site Implements link rewriting, backups, and restoration.
tools/​site-matrix Includes local_links in build settings.
tools/​pool-check Validates the option and restricts enablement to consumers.
sites/​README.md Documents configuration and consumer-link behavior.
README.md Documents staging and cleanup commands.
.github/​workflows/​site-build.yml Conditionally stages with --local-links.

Review effort: Balanced (adaptive)

Comment thread tools/stage-site Resolved
Comment thread tools/stage-site Resolved
Comment thread tools/stage-site Resolved
mmcky and others added 4 commits November 27, 2026 11:46
- A lecture the site carries is a file directly in lectures/: a _toc.yml
  entry such as ../notes/page, a subfolder's page or a link in the pool that
  leads out is not read, so nothing outside the pool is rewritten.
- Inline literals are read a paragraph at a time, as CommonMark reads them:
  one may run across a line break, not across a blank line, a heading or a
  fenced block. On the pool this changes the reading of 7 lines in 2
  lectures, none with a link; dp's plan is the same 31 links in 13 lectures.
- --clean leaves a rewritten lecture that is no longer a file as it is and
  keeps its original, as it does for one edited since staging.

Each has a test that fails on the code before this commit.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01WT8bMZxoF4m8xKRb8BcHnG
- A lecture the site carries is a name in the pool's own listing, spelled
  as it is there, of a file that is not a link. A second name for one
  lecture (a link to it in the pool, or a case variant on a disk that
  ignores case) made two plan entries for one file, the second backup
  overwrote the first with rewritten bytes, and --clean put back the
  rewritten text. LECTURE_NAME_RE and the resolve check go: the listing
  covers both.
- A paragraph also ends at a line that starts a block of its own: a list
  item (whose next lines continue it), a target, +++, a $$ line, a :::
  fence, a thematic break or setext underline, besides a heading. What is
  still read together (a blockquote's start, table rows, HTML) can only
  mask more; the docstring says so, and no longer claims CommonMark's
  reading.
- A literal opens at a whole run of backticks, as pool-check reads one.
- --clean never restores through a link left in a lecture's place.

On the pool the reading is the same as 1972dc5's, and dp's plan the same
31 links in 13 lectures. Each fix has a test that fails on 1972dc5.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01WT8bMZxoF4m8xKRb8BcHnG
…er way

The docstring said that lines read together "can only mask more". That is
false: a stray backtick in a joined line can shift the pairing either way,
masking a link or leaving one inside a literal to be rewritten (a stray
backtick, then a `>` line or a table row holding a literal with a link).
It now says so, and that no pool lecture has a literal holding a link, as
markdown-it reads them (2026-11-27). The tool's code is unchanged.

Tests: an item's indented next lines continue it, literal and all; and the
link-in-a-lecture's-place test links to a file holding the staged bytes, so
it fails on 1972dc5 by writing the original through the link, not only on
the message.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01WT8bMZxoF4m8xKRb8BcHnG
@mmcky
mmcky merged commit 9f3a710 into main Nov 27, 2026
12 checks passed
@mmcky
mmcky deleted the stage-site/local-links branch November 27, 2026 02:59
mmcky added a commit that referenced this pull request Nov 27, 2026
…#106 (#108)

## Why

The reviews of #102 (`tools/stage-site --local-links`) and #106 (the PDF) left points to take after their merges. This PR takes them. None changes what any site builds or publishes today.

## tools/stage-site (from the reviews of #102)

- **A left-over original that changes nothing.** An earlier `--clean` that kept an edited lecture leaves its original under `_build/stage-site-originals/`, and staging refused while it was there. It now refuses only when the original differs from the pool's lecture as it is now. Once the lecture has been put back by hand, replacing the original loses nothing.
- **The originals folder.** Staging refuses when `_build/stage-site-originals` is a link or a file. It also refuses when a lecture's place in that folder holds a link, dangling ones included. A dangling link is not `.exists()`, so copying the original would have written through it. The confinement `--clean` uses refuses a folder that is itself a link, whose target it would otherwise confine to.
- **The paragraph reading is main's.** This PR first changed how `tools/stage-site` reads paragraphs, so that inline literals are found as CommonMark finds them. After four review rounds, that hand-written reading still disagreed with markdown-it on 9 of 62 cases, against 21 for main's. The maintainer chose to split it out (Matt, 2026-11-27, in the project-monorepo session: "Split; markdown-it next (Recommended)"). So `prose_mask` is main's again, and a separate PR will read paragraphs and code spans with markdown-it and MyST's plugins, exact by construction.

## site-build.yml (from the review of #106)

- **The cache save** runs on `always()` with the outcome of the HTML build and of its execution report. A job that times out or is cancelled in a later step, the PDF build say, still keeps a cold build's execution. The header's cache paragraph and `sites/README.md` no longer say "after a green build".
- **The PDF copy step's grep:** finding nothing (exit 1) is reported by the check after it. A real error (exit 2, such as an unreadable folder) now fails the step with grep's own message. `|| true` had let that pass green.
- `sites/README.md`'s `publishes` row no longer says "the same way" twice, and a comment left short by rewrapping is rewrapped.

## Checked

- **On the pool** (at `aa281d3`): the reading is main's, so every pool lecture reads as on main (0 lines differ), and dp's `--local-links` plan is the same 31 links in 13 lectures, byte for byte.
- **Tests:** 361 passed and 1 skipped (at `5498ef2`). Each fix has a test that fails without it: the left-over original that matches (put back and staged again), a dangling link where an original goes, a folder there, and an originals folder that is a link or a file, at staging and at `--clean`.
- **The copy step**, its script taken from the workflow and run under `bash --noprofile --norc -eo pipefail` on local cases:
  - a good build passes;
  - a wrong name fails with its message;
  - no link fails with its message;
  - an unreadable folder fails with grep's "Permission denied" (with `|| true` it passed).
- actionlint is clean; pool-check is clean.

This PR's site build runs all six sites, since it touches `tools/stage-site`. Every leg restores main's cache.

Copilot: not requested (Matt, 2026-11-22).

🤖 Generated with [Claude Code](https://claude.com/claude-code)

https://claude.ai/code/session_01WT8bMZxoF4m8xKRb8BcHnG
mmcky added a commit that referenced this pull request Nov 27, 2026
## Why

`tools/stage-site --local-links` rewrites a consumer site's links to lectures it carries (#102). It must leave alone a link that isn't one, such as an address inside an inline literal or a code cell. Until now it found those with hand-written rules for paragraphs and code spans. Four review rounds of #108 each found new list-indentation cases where those rules and CommonMark disagree. On the 62 cases the reviews checked against markdown-it, main's reading disagrees on 21. The maintainer chose to split that work out of #108 and read pages with markdown-it itself (Matt, 2026-11-27, in the project-monorepo session: "Split; markdown-it next (Recommended)").

## What it changes

- **`Reader`** builds the parser MyST itself uses, myst-parser's markdown-it (`create_md_parser`), with the extensions the site's `_config.yml` enables (`parse: myst_enable_extensions`). It wraps the parser's `link` and `myst_role` inline rules so that each records where its link or role starts and ends. A link counts only where the parser makes one, so none comes from a code cell, a literal block, an inline literal, a `%` comment, raw HTML or maths.
- **Placing back.** Each paragraph, heading or table cell the parser reads is found again in its source lines, one line for one, after any list indent or `>` it carried. Cells of one row are found left to right. A directive's body is read the same way, except for `LITERAL_DIRECTIVES` (code, math, raw). An edit is made only where the source holds, at the place found, exactly what the parser read there. Anything that can't be placed is left as it is, still linking out, as every link did before `--local-links`.
- **Gone:** `prose_mask` and its fence, backtick and block-start rules.
- **Dependencies:** `myst-parser` and `linkify-it-py` (for the sites' `linkify`) join the script's uv header and `tests.yml`. Every leg's environment has them, since Jupyter Book does (myst-parser 3.0.1, markdown-it-py 3.0.0). Without myst-parser, `--local-links` fails with a plain error. The site-build comment and `sites/README.md` say so.

## Checked

- **Against markdown-it:** on all 62 cases from the two oracles of #108's reviews (`mdit-oracle.py` and the organiser's 41), the rewrite makes exactly the links markdown-it renders as links. Those 62, and four more (table cells, a list item's next line, a blockquote, a directive in a list), are now a test: 66 cases, 21 of which fail on main's `tools/stage-site`.
- **On the pool** (at `4b0ab02`): dp's `--local-links` plan is the same 31 links in 13 lectures, byte for byte.
- **Tests:** 428 passed and 1 skipped, including the existing prose cases and a test for the missing-parser error. actionlint and pool-check are clean.

This PR's site build runs all six sites, since it touches `tools/stage-site`. No site sets `local_links`, so no page changes.

Copilot: not requested (Matt, 2026-11-22).

🤖 Generated with [Claude Code](https://claude.com/claude-code)

https://claude.ai/code/session_01WT8bMZxoF4m8xKRb8BcHnG
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.

2 participants