feat(render): rebuild typst render package cph-render (WU-2)

Single entry `display(info, target, parts)` over an ordered parts array, the
frozen contract the generated typst driver calls. Per-kind dispatch for
segment/example/lemma/sop; student/teacher matrix derived internally from
`target` (student hides example solution + lemma proof; teacher shows all).
Content-field values are consumed as already-evaluated content (driver
`include`s them ⇒ module body, ADR-0006), never imported/stringified. Optional
fields (lemma proof, example source) handled gracefully; unknown kind/target
degrade without crashing. Zero @preview deps (CI-robust); CJK + math styled.
Smoke compiles to non-empty student/teacher PDFs.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-06-22 01:33:40 +08:00
parent c151963967
commit c17af60bb0
12 changed files with 453 additions and 0 deletions
+103
View File
@@ -0,0 +1,103 @@
// cph-render — curriculum lesson render package.
//
// Single public entry: `display(info, target, parts)`.
//
// A lesson is an ORDERED array of typed parts. Each part is a dict with a
// `kind` plus that kind's content/scalar fields. Content field VALUES are
// already-evaluated typst content (the driver produces them via `include`) —
// never modules or strings; we render them directly.
//
// kind -> fields (MVP):
// segment : textbook (content, required)
// example : problem (content, required), solution (content, required),
// source (string, optional)
// lemma : stmt (content, required), proof (content, optional)
// sop : sop (content, required)
//
// target -> show/hide (the (kind x target) render matrix; defaults per
// ADR-0005/0008, derived internally — caller passes no flags):
// student : example problem only (hide solution); lemma stmt only (hide
// proof); segment textbook; sop shown.
// teacher : everything shown.
// unknown : rendered conservatively == student (show only required-public
// fields). Never crashes. The "no render rule => warning"
// diagnostic is the Rust side's job, not ours.
#import "src/style.typ": base-style, title-block, subtitle-block
#import "src/elements/segment.typ": display-segment
#import "src/elements/example.typ": display-example
#import "src/elements/lemma.typ": display-lemma
#import "src/elements/sop.typ": display-sop
#import "src/elements/common.typ": example-counter, lemma-counter
/// Resolve a target string to the set of show/hide booleans.
/// Unknown targets fall back to the conservative (student-like) profile.
#let _flags-for(target) = {
if target == "teacher" {
(show-solution: true, show-proof: true, subtitle: [教师版讲义])
} else if target == "student" {
(show-solution: false, show-proof: false, subtitle: [学生版讲义])
} else {
// Unknown target: conservative — show only required-public fields.
(show-solution: false, show-proof: false, subtitle: [讲义])
}
}
/// Render one part according to its `kind`. Unknown kinds render an inline
/// note instead of crashing.
#let _render-part(part, flags) = {
let kind = part.at("kind", default: none)
if kind == "segment" {
display-segment(part)
} else if kind == "example" {
display-example(part, show-solution: flags.show-solution)
} else if kind == "lemma" {
display-lemma(part, show-proof: flags.show-proof)
} else if kind == "sop" {
display-sop(part)
} else {
// Don't crash on an unrecognised kind; surface it visibly instead.
block(
width: 100%,
inset: 0.6em,
fill: red.lighten(85%),
stroke: red.darken(20%) + 0.6pt,
radius: 0.4em,
text(fill: red.darken(30%), weight: "bold")[
[未知部件类型 / unknown kind: #repr(kind)]
],
)
}
}
/// THE ENTRY POINT.
///
/// - `info`: dict, e.g. (title: "…", author: "…"). `author` may be absent.
/// - `target`: string. MVP: "student" | "teacher". Unknown => conservative.
/// - `parts`: ordered array of part dicts (see file header).
#let display(info: (:), target: "student", parts: ()) = {
let title = info.at("title", default: [])
let author = info.at("author", default: none)
// `document` author wants a string/array; normalise the optional field.
set document(
title: title,
author: if author == none { () } else { author },
)
show: base-style
// Reset shared counters so each rendered lesson numbers from 1.
example-counter.update(0)
lemma-counter.update(0)
let flags = _flags-for(target)
title-block()
subtitle-block(flags.subtitle)
// Render strictly in array order.
for part in parts {
_render-part(part, flags)
}
}