Skip to main content

flm_core/stdlib/features/theorems/
html.rs

1//! The stylesheet of the [`theorems`](super) feature on the `html` backend,
2//! `flm.theorems.style` ([`stylesheet`]), and the markup it selects on.
3//!
4//! A theorem is lowered on every backend by the general rule
5//! ([`TheoremRule`](super::TheoremRule)), which writes presentation
6//! elements; the `html` backend writes them as HTML following [the HTML
7//! contract](crate::guide::rendering#the-html-contract) (the CSS classes and
8//! attributes a stylesheet may rely on). The general rule also requires
9//! (records a requirement for) the feature's style identifier [`STYLE`]
10//! (`flm.theorems.style`) at every theorem; on the `html` backend the
11//! identifier names this stylesheet, so that a page holding a theorem carries
12//! it. The rendering guide describes this output under [Theorems in
13//! HTML](crate::guide::rendering#theorems-in-html).
14//!
15//! # The markup
16//!
17//! A theorem is a block of role `flm.theorems.theorem`: a `<div>` whose
18//! second CSS class is the role's (`flm-theorems-theorem`), with the anchor of
19//! the theorem's reference point as its `id` and the theorem class's name then
20//! the theorem kind's as its kind tokens (`data-flm-kind`). Its heading is the
21//! run-in decoration at the start of its first paragraph: it holds the kind's
22//! word and number in a span of role `flm.theorems.name` and the title in a span of role `flm.theorems.title`,
23//! joined as the theorem class's heading template says. The backend writes its
24//! run-in stop (the render option `run_in_stop`, a period by default) after
25//! the heading:
26//!
27//! ```html
28//! <div class="flm-pres-block flm-theorems-theorem" id="theorem-1" data-flm-kind="theoremlike theorem">
29//!   <p class="flm-pres-par"><span class="flm-pres-deco-runin"><span class="flm-pres-span flm-theorems-name">Theorem&#xA0;1</span> (<span class="flm-pres-span flm-theorems-title">My theorem</span>).</span>The square root of two is irrational.</p>
30//! </div>
31//! ```
32//!
33//! A proof's shown back-reference is a link of kind `ref` to the statement's
34//! anchor inside the title span, written as any reference is written, and the
35//! closing mark is the final decoration at the end of the last paragraph:
36//!
37//! ```html
38//! <div class="flm-pres-block flm-theorems-theorem" data-flm-kind="prooflike proof">
39//!   <p class="flm-pres-par"><span class="flm-pres-deco-runin"><span class="flm-pres-span flm-theorems-name">Proof</span> (<span class="flm-pres-span flm-theorems-title"><a class="flm-pres-link" href="#theorem-1" data-flm-kind="ref">Theorem&#xA0;1</a></span>).</span>We argue by contradiction.<span class="flm-pres-deco-final">□</span></p>
40//! </div>
41//! ```
42//!
43//! A theorem with no anchor (a proof, which accepts no label, or any theorem
44//! in a registry without the references feature) has no `id`. Where the body
45//! opens with another block, such as a list, the heading is placed in a
46//! paragraph of its own before it; a displayed formula stands inside its
47//! paragraph, so a body that opens with one has the heading in that
48//! paragraph, before the formula. An empty body is one paragraph holding the
49//! heading and the mark. The feature adds no tag and no CSS class of its own:
50//! a stylesheet selects a theorem by the role's CSS class, the two spans' CSS
51//! classes and the kind tokens, which carry a host's own theorem kinds and
52//! theorem classes as they carry the shipped ones.
53//!
54//! # The stylesheet
55//!
56//! [`stylesheet`] (`flm.theorems.style`) is one cascade layer. It sets the
57//! kind's word and number in bold and the rest of the heading in the weight of
58//! the text, and, for a kind of the `prooflike` class, the word in italics in
59//! the weight of the text. Every value it uses is a custom property declared
60//! in it (`--flm-theorems-heading-weight`, `--flm-theorems-name-weight`,
61//! `--flm-theorems-prooflike-name-weight`,
62//! `--flm-theorems-prooflike-name-style`), which a page sets to change the
63//! look without writing a CSS rule. The general rule requires the stylesheet
64//! at every theorem, so a page that holds no theorem does not carry it.
65
66use crate::core::registry::{BuildError, DefContent, FeatureBuilder, StyleDef};
67use crate::model::names::Name;
68
69/// The identifier of the feature's stylesheet, `flm.theorems.style`
70/// ([`stylesheet`]), a style definition ([`StyleDef`]) that the general rule
71/// [`TheoremRule`](super::TheoremRule) requires at every theorem.
72pub const STYLE: &str = "flm.theorems.style";
73
74/// The feature's stylesheet, `flm.theorems.style`, as a style definition
75/// whose text the library holds (the file `css/theorems.css`, included at
76/// compile time); the backend decides how the page receives it. It sets a
77/// theorem's word and number in bold and a proof's in italics, in one
78/// cascade layer, every value a custom property it declares. See the [module
79/// documentation](self#the-stylesheet).
80pub fn stylesheet() -> StyleDef {
81    StyleDef {
82        id: Name::from(STYLE),
83        content: vec![DefContent::Inline(String::from(include_str!("css/theorems.css")))],
84        docs: String::from("the heading of a theorem: its name set apart from its title"),
85    }
86}
87
88/// Registers what the feature contributes to the `html` backend: the
89/// stylesheet ([`stylesheet`]).
90///
91/// # Errors
92///
93/// The [`BuildError`] of the registry builder when a declaration is made
94/// outside the registration round that accepts it.
95pub(super) fn register(b: &mut FeatureBuilder<'_>) -> Result<(), BuildError> {
96    b.style_def(stylesheet())
97}
98
99#[cfg(test)]
100mod tests {
101    use super::*;
102
103    #[test]
104    fn the_stylesheet_names_itself_and_is_one_layer() {
105        let style = stylesheet();
106        assert_eq!(style.id.as_str(), STYLE);
107        let [DefContent::Inline(css)] = style.content.as_slice() else { panic!("one inline part") };
108        assert_eq!(css.lines().next(), Some("/* flm.theorems.style */"));
109        assert_eq!(css.matches("@layer flm {").count(), 1);
110    }
111}