flm_core/stdlib/features/annotations/html.rs
1//! The stylesheet of the [`annotations`](super) feature on the `html`
2//! backend, `flm.annotations.style` ([`stylesheet`]), and the markup it
3//! selects on.
4//!
5//! On the `html` backend an annotation is lowered by the general rule
6//! [`AnnotationRule`](super::AnnotationRule), into the shared presentation
7//! vocabulary; the rule also requires (records a
8//! requirement for) the feature's style identifier [`STYLE`]
9//! (`flm.annotations.style`), which on the `html` backend names this
10//! stylesheet, a style definition ([`StyleDef`]) that the backend delivers
11//! with the page
12//! ([style and script definitions](crate::guide::concepts_overview#style-and-script-definitions)).
13//! The HTML itself comes from the backend's rules for the presentation
14//! elements, the same for every feature
15//! ([the HTML contract](crate::guide::rendering#the-html-contract)):
16//!
17//! - a **highlight** is a span of role `flm.annotations.highlight` in the flow
18//! of the text, whose kind tokens are the annotator's command and number;
19//! the annotator's initials are at its start, in a margin decoration
20//! written where its decoration mark is:
21//!
22//! ```html
23//! <span class="flm-pres-span flm-annotations-highlight" data-flm-kind="phf annotator-0"><span class="flm-pres-deco-margin"><span class="flm-pres-span flm-annotations-initials" data-flm-kind="phf annotator-0">PhF</span></span>a passage</span>
24//! ```
25//!
26//! - a **comment** is the same under the role `flm.annotations.comment`;
27//! - a **cut part around a block** (a list inside a highlighted passage) is a
28//! `<div>` of the role, with the initials, when it is the first cut part, at
29//! the start of the `<div>`;
30//! - a **cut part of a cut annotation** also carries the kind token
31//! `mark-continues` when a cut part follows it and `mark-continued` when one
32//! precedes it ([`MARK_CONTINUES_KIND`](super::MARK_CONTINUES_KIND),
33//! [`MARK_CONTINUED_KIND`](super::MARK_CONTINUED_KIND)); an annotation that
34//! was not cut carries neither:
35//!
36//! ```html
37//! <p class="flm-pres-par">Some text <span class="flm-pres-span flm-annotations-comment" data-flm-kind="phf annotator-0 mark-continues"><span class="flm-pres-deco-margin"><span class="flm-pres-span flm-annotations-initials" data-flm-kind="phf annotator-0">PhF</span></span>A comment.</span></p>
38//! <p class="flm-pres-par"><span class="flm-pres-span flm-annotations-comment" data-flm-kind="phf annotator-0 mark-continued">With a second paragraph.</span> More text.</p>
39//! ```
40//!
41//! The rendering guide describes the same markup
42//! ([Annotations in HTML](crate::guide::rendering#annotations-in-html)).
43//!
44//! # The stylesheet
45//!
46//! The stylesheet [`stylesheet`] (`flm.annotations.style`) is one cascade
47//! layer. It holds the **palette**, one custom property per annotator number,
48//! `--flm-annotation-color-0` to `--flm-annotation-color-7`: eight colors,
49//! those that the `phfcc` LaTeX package presets, in its order, so that an
50//! annotator has the same color on the page as in the recomposed LaTeX
51//! ([`latex`](super::latex)). An annotation and its initials select their
52//! color by the kind token `annotator-<n>` (`[data-flm-kind~="annotator-0"]`).
53//! Every annotation first takes the fallback color,
54//! `--flm-annotation-color-fallback`, and a number within the palette then
55//! takes its own color, so that an annotator numbered eight or more takes the
56//! fallback: red, the color `phfcc` gives an annotator once its presets run
57//! out ([`PALETTE_SIZE`]).
58//!
59//! A **highlight**'s text is in the color; its background is tinted with the
60//! color only where a page turns the tint on
61//! (`--flm-annotation-highlight-tint`, `0%` by default). A **comment** is in
62//! the color, in the annotations' font family (`--flm-annotation-family`,
63//! sans-serif by default) and in a heavier weight
64//! (`--flm-annotation-comment-weight`), between brackets that the stylesheet
65//! draws: the opening bracket on a cut part not marked `mark-continued`, the
66//! closing bracket on a cut part not marked `mark-continues`, so that a cut
67//! comment is bracketed once. A cut part around a block carries its bracket
68//! on a line of its own, above or below the block. The **initials** are
69//! smaller than the note they are shown in, upright, of normal weight, in the
70//! annotations' font family, in a thin box of the color; they are shown in
71//! the margin decoration, which the HTML backend's core stylesheet
72//! ([`STYLE_CORE`](crate::stdlib::backends::html::conventions::STYLE_CORE))
73//! places beside the text. Every color and length is a custom property that the
74//! stylesheet declares, which a page sets to change the look without writing
75//! a rule; every selector begins with a class of the HTML contract.
76//!
77//! The general rule requires the stylesheet wherever it writes an
78//! annotation, so that a page holding none carries none of it; when the
79//! render option [`hidden`](super::AnnotationsRenderOptions::hidden) is true,
80//! it requires nothing, since nothing of an annotation's own reaches the page.
81
82use crate::core::registry::{BuildError, DefContent, FeatureBuilder, StyleDef};
83use crate::model::names::Name;
84
85/// The identifier of the feature's stylesheet, `flm.annotations.style`
86/// ([`stylesheet`]), which the general rule
87/// [`AnnotationRule`](super::AnnotationRule) requires at every annotation it
88/// writes.
89pub const STYLE: &str = "flm.annotations.style";
90
91/// How many annotator numbers the stylesheet's palette gives a color of their
92/// own: eight, `--flm-annotation-color-0` to `--flm-annotation-color-7`; an
93/// annotator with a higher number takes the fallback color,
94/// `--flm-annotation-color-fallback`.
95pub const PALETTE_SIZE: usize = 8;
96
97/// Returns the feature's stylesheet, `flm.annotations.style`, as a style
98/// definition: the palette and the look of a highlight, a comment and the
99/// initials, in one cascade layer, every color and length a custom property
100/// it declares. See the [module documentation](self#the-stylesheet).
101pub fn stylesheet() -> StyleDef {
102 StyleDef {
103 id: Name::from(STYLE),
104 content: vec![DefContent::Inline(String::from(include_str!("css/annotations.css")))],
105 docs: String::from(
106 "a reviewer's highlights and comments in the annotator's color, and the initials beside the line",
107 ),
108 }
109}
110
111/// Registers what the feature contributes to the `html` backend: the
112/// stylesheet ([`stylesheet`]).
113///
114/// # Errors
115///
116/// Never fails: the declaration it makes ([`FeatureBuilder::style_def`]) is
117/// checked when the registry is built.
118pub(super) fn register(b: &mut FeatureBuilder<'_>) -> Result<(), BuildError> {
119 b.style_def(stylesheet())
120}
121
122#[cfg(test)]
123mod tests {
124 use super::*;
125
126 #[test]
127 fn the_stylesheet_names_itself_and_is_one_layer() {
128 let style = stylesheet();
129 assert_eq!(style.id.as_str(), STYLE);
130 let [DefContent::Inline(css)] = style.content.as_slice() else { panic!("one inline part") };
131 assert_eq!(css.lines().next(), Some("/* flm.annotations.style */"));
132 assert_eq!(css.matches("@layer flm {").count(), 1);
133 }
134
135 #[test]
136 fn the_palette_holds_one_color_per_number_and_the_fallback_is_phfcc_s() {
137 let style = stylesheet();
138 let [DefContent::Inline(css)] = style.content.as_slice() else { panic!("one inline part") };
139 for number in 0..PALETTE_SIZE {
140 assert!(css.contains(&format!("--flm-annotation-color-{number}: ")), "{number}");
141 assert!(css.contains(&format!("[data-flm-kind~=\"annotator-{number}\"]")), "{number}");
142 }
143 assert!(!css.contains(&format!("--flm-annotation-color-{PALETTE_SIZE}:")));
144 assert!(css.contains("--flm-annotation-color: var(--flm-annotation-color-fallback);"));
145 // `phfcc` falls back to xcolor's `red`.
146 assert!(css.contains("--flm-annotation-color-fallback: rgb(255 0 0);"));
147 }
148}