flm_core/stdlib/features/toc/html.rs
1//! The stylesheet of the [`toc`](super) feature on the `html` backend,
2//! `flm.toc.style` ([`stylesheet`]), and the markup it selects on.
3//!
4//! On the HTML page the table of contents is written with the same
5//! presentation elements as on every other output ([`rules`](super::rules)),
6//! and the HTML backend writes them as its HTML contract states (see
7//! [`CONVENTIONS`](crate::stdlib::backends::html::CONVENTIONS)): the block of
8//! role `flm.toc` is a `<nav>` carrying the role `doc-toc`, each list a `<ul>`,
9//! each entry an `<li>` holding the link to its heading, the number in a span
10//! of role `flm.toc.entry.number`. Each `<li>` carries its heading's level as
11//! `data-flm-level`, the attribute the heading itself carries, so that one
12//! selector `[data-flm-level="N"]` matches the headings of level N and their
13//! entries. For `\section{One}\subsection{Two}` under numbering:
14//!
15//! ```html
16//! <nav class="flm-pres-block flm-toc" role="doc-toc" data-flm-kind="toc">
17//! <ul class="flm-pres-list" data-flm-kind="unnumbered toc">
18//! <li data-flm-level="1"><a class="flm-pres-link" href="#sec--One" data-flm-kind="toc"><span class="flm-pres-span flm-toc-entry-number">1</span> One</a>
19//! <ul class="flm-pres-list" data-flm-kind="unnumbered toc">
20//! <li data-flm-level="2"><a class="flm-pres-link" href="#sec--Two" data-flm-kind="toc"><span class="flm-pres-span flm-toc-entry-number">1.1</span> Two</a></li>
21//! </ul>
22//! </li>
23//! </ul>
24//! </nav>
25//! ```
26//!
27//! The general rule of the root ([`RootRule`](super::rules::RootRule))
28//! requires the feature's style identifier [`STYLE`] (`flm.toc.style`), which
29//! on this backend names the stylesheet: one cascade layer that removes the
30//! list markers, indents each nested list and leaves a space below the table,
31//! the indentation and the space custom properties it declares
32//! (`--flm-toc-indent`, `--flm-toc-space-below`). It sets structure only; a
33//! page that wants another presentation adds a stylesheet of its own, or
34//! replaces this one through the render option
35//! [`definitions`](crate::stdlib::backends::html::HtmlRenderOptions::definitions).
36
37use crate::core::registry::{BuildError, DefContent, FeatureBuilder, StyleDef};
38use crate::model::names::Name;
39
40use super::STYLE;
41
42/// The feature's style definition, `flm.toc.style` ([`STYLE`]): the lists
43/// without a marker, each nested list indented and a space below the table, in
44/// one cascade layer, with the indentation and the space custom properties it
45/// declares (`--flm-toc-indent`, `--flm-toc-space-below`). The
46/// space after an entry's number is text the entry's rule writes, not a rule
47/// of the stylesheet. See the [module documentation](self).
48pub fn stylesheet() -> StyleDef {
49 StyleDef {
50 id: Name::from(STYLE),
51 content: vec![DefContent::Inline(String::from(include_str!("css/toc.css")))],
52 docs: String::from(
53 "the lists of the table of contents without a marker, each nested list indented, and a \
54 space after an entry's number",
55 ),
56 }
57}
58
59/// Registers the stylesheet ([`stylesheet`]).
60///
61/// # Errors
62///
63/// The [`BuildError`] of the registry builder when a declaration is made
64/// outside the registration round that accepts it.
65pub(super) fn register(b: &mut FeatureBuilder<'_>) -> Result<(), BuildError> {
66 b.style_def(stylesheet())
67}
68
69#[cfg(test)]
70mod tests {
71 use super::*;
72
73 #[test]
74 fn the_stylesheet_names_itself_and_is_one_layer() {
75 let style = stylesheet();
76 assert_eq!(style.id.as_str(), STYLE);
77 let [DefContent::Inline(css)] = style.content.as_slice() else { panic!("one inline part") };
78 assert_eq!(css.lines().next(), Some("/* flm.toc.style */"));
79 assert_eq!(css.matches("@layer flm {").count(), 1);
80 }
81}