Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,16 @@ remains at `#/pcp-reading-guide`. The algebraic proof is due to Amireddy,
Behera, Srinivasan, Sudan and Willumsgaard; Lee wrote the Lean formalization.
AI agents wrote the three explanations under Will Cook's direction.

On a desktop, the paper is on the left and the proof diagram is on the right.
As you scroll, the diagram highlights the step linked to the passage you are
reading. Click a statement or diagram card to see its Lean code. Use **Back**
On a desktop, each visit opens with the paper on the left and the proof pane on
the right: the graph above the Lean, or beside it on a very wide screen. Drag
the divider between the paper and the proof to give either side more room, and
the handle between the graph and the Lean to share the pane; both are
remembered. Closing the proof pane lasts for that visit.
As you scroll, the proof pane follows the linked passage you are reading: the
Lean shows its declaration, and the graph shows that declaration with what it
uses. Choosing another card or result holds the graph there until you choose
**Follow the paper again**. Click a statement or diagram
card to see its Lean code. Use **Back**
to return to the diagram without losing your place, or the **Graph / Lean**
toggle to choose what stays beside the paper.

Expand Down
103 changes: 99 additions & 4 deletions tests/journey.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,8 @@ def lean_open(page: Page) -> bool:

GRAPH_FIRST = """
try {
if (!localStorage.getItem('proof-reader-pane-2'))
localStorage.setItem('proof-reader-pane-2', JSON.stringify({graph: true, lean: false, share: 0, sideShare: 0}));
if (!localStorage.getItem('proof-reader-pane-3'))
localStorage.setItem('proof-reader-pane-3', JSON.stringify({graph: true, lean: false, share: 0, sideShare: 0}));
} catch { /* a frame with no storage, such as the PDF's: nothing to set there */ }
"""

Expand Down Expand Up @@ -146,6 +146,93 @@ def opening(page: Page, url: str) -> None:
check(page.evaluate("location.hash") == hashes[0], "and stays in the address bar")


def reopen_proof(page: Page, url: str) -> None:
"""A previously closed proof pane must not hide the split on later visits."""
print("Reopening the proof pane")
page.goto(f"{url}#/{SLUG}", wait_until="load")
page.locator(".proof").wait_for()
page.evaluate("""localStorage.setItem('proof-reader-pane-3', JSON.stringify({
graph: false, lean: false, share: 0.3, sideShare: 0.4
}))""")
for slug in ("pcp-exposition", "pcp-specialist", "pcp-full-argument", "pcp-reading-guide", SLUG):
page.goto(f"{url}#/{slug}", wait_until="load")
page.locator(".proof").wait_for()
paper = page.locator(".left").bounding_box()
proof = page.locator(".proof").bounding_box()
check(paper is not None and proof is not None
and proof["x"] >= paper["x"] + paper["width"] - 1
and abs(proof["y"] - paper["y"]) <= 1,
f"{slug} opens beside its proof despite a saved closed pane")
check(not lean_open(page), "the saved Graph mode is preserved")
page.locator(".proof").get_by_role("button", name="Close", exact=True).click()
page.locator(".proof").wait_for(state="hidden")
check(True, "Close still hides the proof for the current visit")
page.reload(wait_until="load")
page.locator(".proof").wait_for()
check(True, "reloading opens the proof pane again")
check(page.evaluate("""() => {
const layout = JSON.parse(localStorage.getItem('proof-reader-pane-3'));
return layout.share === 0.3 && layout.sideShare === 0.4 && layout.lean === false
&& !Object.hasOwn(layout, 'graph');
}"""), "mode and split sizes persist without a saved closed pane")


def graph_follows(page: Page, url: str, shots: Path | None) -> None:
"""A paper with many results: the graph shows the statement being read, and the
divider between paper and proof moves, is remembered and can be put back."""
print("The graph follows the reading")
page.goto(f"{url}#/pcp-exposition", wait_until="load")
page.locator(".nearmap__card.is-head").first.wait_for()
head = lambda: page.eval_on_selector_all(".nearmap__card.is-head .nearmap__title", "els => els.map(e => e.textContent)")
check(page.locator(".proof__graph").bounding_box()["height"] > 150 and lean_open(page),
"a fresh visit opens with the graph above the Lean")
check(page.locator(".claims--many select").count() == 1, "many results share one line with a menu")
seen = set()
for _ in range(8):
page.mouse.move(400, 500)
page.mouse.wheel(0, 2200)
page.wait_for_timeout(700)
heads = head()
seen.add(tuple(heads))
if "Following" in caption(page) and heads:
check(any(title in caption(page) for title in heads), f"the graph shows the statement being read: {heads[0]}")
check(len(seen) >= 4, f"the graph changed {len(seen)} times as the paper scrolled")
page.get_by_role("button", name="Next result").click()
page.wait_for_timeout(300)
held = head()
check(page.locator(".graph__paused").count() == 1, "choosing a result holds the graph there")
page.mouse.move(400, 500)
page.mouse.wheel(0, -4000)
page.wait_for_timeout(900)
check(head() == held, "a held graph stays put while the paper scrolls")
page.get_by_role("button", name="Follow the paper again").click()
wait_until(page, lambda: head() != held and page.locator(".graph__paused").count() == 0, "following again returns to the statement being read")
shot(page, shots, "05-graph-follows")
width = lambda: page.locator(".left").bounding_box()["width"]
start = width()
grip = page.locator(".papersplit").bounding_box()
page.mouse.move(grip["x"] + 13, 400)
page.mouse.down()
page.mouse.move(grip["x"] - 107, 400, steps=6)
page.mouse.up()
page.wait_for_timeout(300)
moved = width()
check(abs((start - moved) - 120) < 4, f"dragging the divider narrows the paper by the drag ({start:.0f} to {moved:.0f})")
page.reload(wait_until="load")
page.locator(".papersplit").wait_for()
page.wait_for_timeout(600)
check(abs(width() - moved) < 2, "the divider's place survives a reload")
page.locator(".papersplit").dblclick()
page.wait_for_timeout(300)
check(abs(width() - start) < 2, "a double click puts the divider back")
page.locator(".papersplit").focus()
page.keyboard.press("ArrowRight")
page.wait_for_timeout(200)
check(width() > start + 20, "the arrow keys move the divider")
page.keyboard.press("Home")
check(page.evaluate("document.documentElement.scrollWidth <= innerWidth"), "no sideways scroll after resizing")


def active(page: Page) -> str:
return page.evaluate("document.querySelector('[data-paper-pane] .q.is-active')?.dataset.p ?? ''")

Expand Down Expand Up @@ -931,7 +1018,9 @@ def rested(want: dict, how: str) -> None:
reload(page)
page.wait_for_selector(".sheet__text .q", state="attached")
page.wait_for_timeout(400)
check(page.locator(".proof").count() == 0, "and stays closed")
check(page.locator(".proof__graph").is_visible(), "a reload restores the side-by-side view")
page.locator(".proof__close").click()
page.locator(".proof").wait_for(state="hidden")
page.locator(".bar__open").click()
wait_until(page, lambda: page.locator(".proof__graph").is_visible(), "and opens again")

Expand Down Expand Up @@ -1149,7 +1238,7 @@ def monitors(page: Page, url: str, shots: Path | None, size: tuple[int, int]) ->
page.wait_for_selector(".decl")
page.wait_for_timeout(1400)
check(page.evaluate("document.documentElement.scrollWidth <= innerWidth"), "no sideways scroll")
check(page.locator(".proof[data-side]").count() == (1 if w >= 1920 else 0), "stacked or side by side by the room on the right page")
check(page.locator(".proof[data-side]").count() == (1 if w >= 2560 else 0), "stacked or side by side by the room on the right page")
box = page.locator(".proof").bounding_box()
check(box["width"] >= 500, f"the right page is {box['width']:.0f}px wide")
shot(page, shots, f"30-{w}")
Expand Down Expand Up @@ -1379,6 +1468,12 @@ def main() -> int:
context = browser.new_context(viewport={"width": 1440, "height": 790})
opening(context.new_page(), url)
context.close()
context = browser.new_context(viewport={"width": 1440, "height": 790})
reopen_proof(context.new_page(), url)
context.close()
context = browser.new_context(viewport={"width": 1440, "height": 790})
graph_follows(context.new_page(), url, args.shots)
context.close()
context = graph_first(browser.new_context(viewport={"width": 1440, "height": 790}))
journey(context.new_page(), url, args.shots)
context.close()
Expand Down
39 changes: 39 additions & 0 deletions tests/test_markdown.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,42 @@ def test_markdown_formatting_is_rendered_without_rewording(self):
self.assertNotIn("p", built["code"])
self.assertEqual(self.path.read_text(), source)

def test_quote_html_sets_formulas_as_the_reading_copy_does(self):
source = ("## Section\n\nThen $\\operatorname{Val}$ vanishes on $H^m$ for **every** `x`:\n"
"$$\\sum_i Z_H(X_i) = 0.$$\n")
quote = source.split("\n\n", 1)[1].rstrip("\n")
done, passages = self.resolve(source, quote)
built = reading.build_markdown(done, "test", passages, [], "https://example.org/guide.md")
markup = built["quote_html"]["p"]
self.assertIn('class="katex"', markup)
self.assertIn('class="katex-display"', markup)
self.assertIn("<strong>every</strong>", markup)
self.assertIn("<code>", markup)
# Inline: no paragraph, no reading-copy display block, no placeholder left.
self.assertNotRegex(markup, r"<(?:p|div)\b")
self.assertNotIn('data-m="', markup)
# Each formula is exactly the one the reading copy shows.
for formula in done["math"]:
self.assertIn(formula, markup)
self.assertIn(formula, built["html"])
# The plain text is unchanged: labels, search and the text checks read it.
self.assertTrue(built["display_quotes"]["p"].startswith("Then Val"))
self.assertNotIn("<", built["display_quotes"]["p"])

def test_quote_html_keeps_no_link_inside_it(self):
done, passages = self.resolve("## Section\n\nSee [the source](code.lean) for $x$.\n", "See [the source](code.lean) for $x$.")
built = reading.build_markdown(done, "test", passages, [], "https://example.org/guide.md")
self.assertNotRegex(built["quote_html"]["p"], r"</?a\b")
self.assertIn("the source", built["quote_html"]["p"])

def test_a_formula_that_cannot_be_set_stops_the_build(self):
with self.assertRaisesRegex(reading.ReadingError, "could not set"):
self.resolve("## Section\n\nBroken $\\frac{1}{$ here.\n", "Broken $\\frac{1}{$ here.")

def test_a_quote_whose_formula_was_not_typeset_stops_the_build(self):
with self.assertRaisesRegex(reading.ReadingError, "formula 3 in the quote was not typeset"):
reading.quote_markup("p", 'x <span class="m" data-m="3">x</span>', ["<span class=\"katex\">x</span>"])

def test_rendered_collision_inside_one_block_fails(self):
done, passages = self.resolve("## Section\n\nA **word** and a `word`.\n", "**word**")
with self.assertRaisesRegex(reading.ReadingError, "rendered quote found 2 times"):
Expand Down Expand Up @@ -132,6 +168,7 @@ def test_fenced_quote_stays_code_and_has_no_pdf_coordinates(self):
self.assertEqual(passages[0]["rects"], [])
self.assertEqual(built["pages"], 0)
self.assertNotIn('class="pg"', built["html"])
self.assertEqual(built["quote_html"]["p"], "def x_y : Nat := 0")

def test_dotted_heading_links_use_reader_routes(self):
source = "## 3. The definitions\n\n[Continue](#4-the-pcp-theorem).\n\n## 4. The PCP theorem\n\nIt holds.\n"
Expand Down Expand Up @@ -240,6 +277,8 @@ def only_node(args, **kwargs):
self.assertEqual(edition["paper"]["sizes"], [])
self.assertEqual(edition["passages"][0]["quote"], "Here is Demo.statement.")
self.assertEqual(edition["passages"][0]["source_quote"], "Here is `Demo.statement`.")
self.assertEqual(edition["passages"][0]["quote_html"],
'Here is <code><span class="lk lk--type">Demo</span><span class="lk lk--arrow">.</span>statement</code>.')
self.assertEqual(edition["shelf"]["example"]["page_label"], "")
self.assertEqual(json.loads((paper.PUBLIC / "index.json").read_text())["papers"][0]["source_kind"], "markdown")

Expand Down
5 changes: 5 additions & 0 deletions tests/test_paper.py
Original file line number Diff line number Diff line change
Expand Up @@ -490,6 +490,11 @@ def test_the_three_examples_in_order(self):
roles = {p["id"]: p["role"] for p in links["passages"]}
self.assertEqual([e["role"] for e in examples], [roles[e["id"]] for e in examples])

def test_a_pdf_sourced_paper_has_no_set_quotes(self):
passages = self.built["passages"]
self.assertTrue(passages)
self.assertFalse([p["id"] for p in passages if "quote_html" in p or p.get("source_quote")])

def test_each_example_is_its_paragraph(self):
page = reading.parse(self.built["reading_html"])
for example in self.built["shelf"]["examples"]:
Expand Down
3 changes: 3 additions & 0 deletions tools/paper.py
Original file line number Diff line number Diff line change
Expand Up @@ -749,8 +749,11 @@ def split(names: list[str], statement: bool, kind_of) -> list[str]:
setting = reading_copy.build_markdown(data["markdown_render"], slug, data["passages"], names, config["paper"].get("url", ""),
links.get("name_sections"), config.get("reading", {}).get("companions"))
display_quotes = setting.pop("display_quotes")
quote_html = setting.pop("quote_html")
for passage in data["passages"]:
passage["quote"] = display_quotes[passage["id"]]
# The same words set as the reading copy sets them; `quote` stays the plain text.
passage["quote_html"] = quote_html[passage["id"]]
elif (data["folder"] / "paper.md").is_file():
setting = reading_copy.build(data["folder"], slug, data["passages"], names, data["words"],
config.get("reading", {}), links)
Expand Down
28 changes: 28 additions & 0 deletions tools/reading.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,33 @@ def locate_markdown(page: Page, done: dict, passages: list[dict]) -> tuple[dict,
return spans, visible_quotes


MATH_SPAN = re.compile(r'<span class="m" data-m="(\d+)">.*?</span>', re.S)
DISPLAY_WRAP = re.compile(r'<div class="math-display">(<span class="m" data-m="\d+">.*?</span>)</div>', re.S)
INLINE_ONLY = re.compile(r"</?(?:p|div|li|ul|ol|pre|figure|figcaption|h[1-6]|aside|header|section|blockquote|table|thead|tbody|tr|th|td|hr|img)\b")


def quote_markup(pid: str, raw: str, math: list[str]) -> str:
"""A quoted passage as the reading copy sets it: the same inline Markdown and
the same KaTeX output, with no block around it and no link inside it, since
it is printed inside a blockquote or a link on the Lean page."""
check_tags(raw)
raw = DISPLAY_WRAP.sub(r"\1", raw)
raw = re.sub(r"</?a\b[^>]*>", "", raw)
if INLINE_ONLY.search(raw):
raise ReadingError(f"passage {pid}: quoted words hold a block, which a quotation cannot show inline")

def formula(match: re.Match) -> str:
index = int(match[1])
if index >= len(math) or 'class="katex' not in math[index]:
raise ReadingError(f"passage {pid}: formula {index} in the quote was not typeset")
return math[index]

out = MATH_SPAN.sub(formula, raw)
if re.search(r"[\ue000-\ue003]", out) or 'data-m="' in out:
raise ReadingError(f"passage {pid}: a formula in the quote was left unset")
return out


def numbered_ranges(ranges: list[tuple[str, int]], outline: list[dict]) -> list[tuple[str, int]]:
"""An unnumbered subheading belongs to the numbered section above it, so that
`name_sections` can name section 3 and cover its subheadings too."""
Expand Down Expand Up @@ -195,6 +222,7 @@ def link(match):
"heading_aliases": done["heading_aliases"],
"words": len(page.visible.split()), "formulas": len(done["math"]), "pages": 0,
"order": sorted(spans, key=lambda pid: spans[pid][0]), "code": code, "display_quotes": quotes,
"quote_html": {p["id"]: quote_markup(p["id"], done["quote_html"][p["id"]], done["math"]) for p in passages},
"paragraphs": {pid: paragraph(page, start, stop, quotes[pid]) for pid, (start, stop) in spans.items()}}


Expand Down
Loading