#import "../core/target.typ": _is-query
#import "chunk.typ": _fenced-chunks-runs, _fenced-chunk
#import "chunk-support.typ": _disable-raw-chunk-transforms
#import "code.typ": _html-themed-raw-block, _output-chrome, code-block
#import "defaults.typ": _call-defaults
#import "options.typ": _resolve-options
// Default paged styling for the labeled chunk carriers emitted by `code.typ`.
//
// Internal: the generated wrapper applies this, and only when a notebook theme
// is in play (`staging.rs`). It is not part of the public `calepin.*` surface —
// documents restyle chunks through the labels, and themes through their own
// show rules, so nobody outside Calepin needs to name this transform.
//
// A document compiled with `theme = "typst"` never has it applied, so the
// carriers stay bare and packages such as codly own code rendering completely.
// Chunks still execute either way — execution is driven by the fenced-chunk
// rules in the wrapper, not by this transform.
//
// Every rule below reconstructs from `it.body` and never re-displays `it`. That
// is a correctness condition, not a style choice: a rule that re-displays `it`
// would chain under a document author's later rule instead of being replaced by
// it, so overrides would nest rather than win.
// Code is sized relative to the captured document body size rather than to
// `1em`, which would compound: a literal ```typ block is rendered by replacing
// its source `raw` element, so it renders inside Typst's already-reduced raw
// text context, whereas executed chunks are emitted as ordinary calls at body
// size. Anchoring to the captured body size gives both paths a single, matching
// reduction instead of shrinking twice.
#let _calepin-body-size = std.state("calepin-body-size", 11pt)
#let _sized(body) = context {
set text(size: _calepin-body-size.get() * 0.8)
body
}
#let _default-chunk-chrome(body, raw-chunk-langs: ()) = {
show <calepin-input>: it => _sized(code-block(it.body))
show <calepin-output>: it => _sized(_output-chrome(it.body, kind: "stdout"))
show <calepin-result>: it => _sized(_output-chrome(it.body, kind: "result"))
show <calepin-warning>: it => _sized(_output-chrome(it.body, kind: "warning"))
show <calepin-error>: it => _sized(_output-chrome(it.body, kind: "error"))
// Fenced blocks in a themed document. The wrapper installs the same detection
// for registered languages earlier; this rule is defined last, so it is the
// outermost one and owns the query pass, where `<calepin-chunk>` metadata
// must be emitted for every fenced block.
show raw.where(block: true, theme: auto): it => {
if _disable-raw-chunk-transforms.get() {
_html-themed-raw-block(it)
} else if it.lang != none and (_is-query() or raw-chunk-langs.contains(it.lang)) and _fenced-chunks-runs(
it.lang,
_resolve-options(it.lang, _call-defaults).at("fenced-chunks"),
) {
_fenced-chunk(it.lang, it)
} else {
_html-themed-raw-block(it)
}
}
context _calepin-body-size.update(text.size)
body
}