flm_core/stdlib/features/citations/html.rs
1//! The stylesheet of the [`citations`](super) feature on the `html` backend,
2//! `flm.citations.style` ([`stylesheet`]), and the markup it selects on.
3//!
4//! On the HTML page, a citation is written in the shared presentation
5//! vocabulary, as on every other output: the general rules of `\cite` and
6//! `\R` ([`CiteRule`](super::CiteRule), [`RefCiteRule`](super::RefCiteRule))
7//! stage the citation mark, and the backend writes the HTML as its [HTML
8//! contract](crate::guide::rendering#the-html-contract) says. The general
9//! rules also require (record a requirement for; an identifier a rule
10//! records to say that its output needs a definition, see [`StyleDef`]) the
11//! feature's style identifier [`STYLE`] (`flm.citations.style`), which on
12//! this backend names the stylesheet, so a page holding no citation does not
13//! carry it. The list of cited works is lowered by its general rule
14//! ([`BibliographyRule`](super::BibliographyRule)).
15//!
16//! # The markup
17//!
18//! `Codes of this family are well studied \cite{a,b}.` writes a no-break
19//! space (U+00A0, written ` `) in a span of role `flm.citations.space`,
20//! then the citation mark: a span of role `flm.citations.mark` carrying the
21//! kind token `attached`, holding the delimiters, the join texts and the
22//! numbers. Each number is a link of kind `cite` to its work's entry in the
23//! list of cited works, with the document role `doc-biblioref`:
24//!
25//! ```html
26//! <p class="flm-pres-par">Codes of this family are well studied<span class="flm-pres-span flm-citations-space"> </span><span class="flm-pres-span flm-citations-mark" data-flm-kind="attached">[<a class="flm-pres-link" href="#citation-1" role="doc-biblioref" data-flm-kind="cite">1</a>,<a class="flm-pres-link" href="#citation-2" role="doc-biblioref" data-flm-kind="cite">2</a>]</span>.</p>
27//! ```
28//!
29//! The space is a character of the text, in a span of its own before the
30//! citation mark's span: the text a reader copies from the page holds it, the
31//! line does not break between the word and the citation mark, and a
32//! stylesheet selects it by its class `flm-citations-space` (to hide it, or to
33//! change its width).
34//!
35//! Under the superscript form ([`MarkForm::Superscript`](super::MarkForm)) of
36//! the listed placement, the citation mark of a `\cite` is a `<sup>` element
37//! (with `data-flm-style="superscript"`) holding no delimiters, and no space
38//! is written before it. The citation mark of an `\R` is never raised and
39//! shows its numbers with no delimiters; the word that `\R` writes is placed
40//! before it, in a span of role `flm.citations.word` (`refs. 1,2`).
41//! Under the in-place placement, the citation mark carries the kind token
42//! `in-place` after `attached` or `standalone`, and holds the citation
43//! entries themselves, with no link.
44//!
45//! The list of cited works is placed after the document's content (after the
46//! lists of xnotes among the standard features; an xnote is a note set apart from
47//! the running text, such as a footnote, see
48//! [`xnotes`](crate::stdlib::features::xnotes)): a block with the document
49//! role `doc-bibliography`, holding the title as a heading and a labeled list
50//! whose entries carry the works' identifiers. The entries carry no document
51//! role:
52//!
53//! ```html
54//! <div class="flm-pres-block flm-citations-bibliography" role="doc-bibliography" data-flm-kind="citations">
55//! <h2 class="flm-pres-block--heading" data-flm-heading="citations" data-flm-level="2" data-flm-toc="false">References</h2>
56//! <ol class="flm-pres-list" data-flm-kind="numbered citations labeled">
57//! <li id="citation-1"><span class="flm-pres-list-label"><span class="flm-pres-span flm-citations-entry-number">[1]</span></span>
58//! <p class="flm-pres-par">Knuth, The Art of Computer Programming, 1984</p>
59//! </li>
60//! </ol>
61//! </div>
62//! ```
63//!
64//! # The stylesheet
65//!
66//! The stylesheet [`STYLE`] (`flm.citations.style`, [`stylesheet`]) is one
67//! cascade layer that keeps a raised citation mark from pushing its line apart
68//! from the lines around it. It sets the line height of a raised citation mark
69//! to the custom property `--flm-citations-raised-line-height`, declared in it
70//! with the value `0`, which a page's own stylesheet sets to change that line
71//! height without repeating the selector. A citation mark wraps where text
72//! does. The raising itself, and the size of the raised numbers, come from the
73//! backend's own stylesheet, for every span of the superscript style.
74
75use crate::core::registry::{BuildError, DefContent, FeatureBuilder, StyleDef};
76use crate::model::names::Name;
77
78/// The identifier of the feature's stylesheet, `flm.citations.style`
79/// ([`stylesheet`]), which the general rules of `\cite` and `\R` require.
80pub const STYLE: &str = "flm.citations.style";
81
82/// Returns the style definition of the feature's stylesheet,
83/// `flm.citations.style` ([`StyleDef`]): one cascade layer that keeps a raised
84/// citation mark from pushing its line apart, with its one value held in a
85/// custom property it declares. See the [module
86/// documentation](self#the-stylesheet).
87pub fn stylesheet() -> StyleDef {
88 StyleDef {
89 id: Name::from(STYLE),
90 content: vec![DefContent::Inline(String::from(include_str!("css/citations.css")))],
91 docs: String::from(
92 "the line height of a raised citation mark, which keeps the mark from pushing its \
93 line apart from the lines around it",
94 ),
95 }
96}
97
98/// Registers the feature's part of the HTML page: the stylesheet
99/// ([`stylesheet`]).
100///
101/// # Errors
102///
103/// The [`BuildError`] of the registry builder when a declaration is made
104/// outside the round that takes it.
105pub(super) fn register(b: &mut FeatureBuilder<'_>) -> Result<(), BuildError> {
106 b.style_def(stylesheet())
107}
108
109#[cfg(test)]
110mod tests {
111 use super::*;
112
113 #[test]
114 fn the_stylesheet_names_itself_and_is_one_layer() {
115 let style = stylesheet();
116 assert_eq!(style.id.as_str(), STYLE);
117 let [DefContent::Inline(css)] = style.content.as_slice() else { panic!("one inline part") };
118 assert_eq!(css.lines().next(), Some("/* flm.citations.style */"));
119 assert_eq!(css.matches("@layer flm {").count(), 1);
120 }
121}