flm_core/stdlib/features/toc/latex.rs
1//! The rules of the three elements of the table of contents on the
2//! `latex-recomposed` backend: each writes nothing.
3//!
4//! The recomposed LaTeX never shows FLM's table of contents: LaTeX builds its
5//! own from the headings (`\tableofcontents`, written by the author or by a
6//! template). The piece is not placed on that output unless a render request
7//! or a template names it for that backend, and where it is, it writes
8//! nothing. The rules exist because a plan checks that its backend can lower
9//! every element the registry holds, as the list of xnotes has a rule on that
10//! backend that writes nothing
11//! ([`ListLatexRule`](crate::stdlib::features::xnotes::latex::ListLatexRule)).
12//!
13//! The rules are marked default on the `latex-recomposed` backend only, under
14//! a peer declaration of that backend, so that a registry without it still
15//! builds, with the rules set aside. They read nothing and stage nothing.
16
17use std::sync::Arc;
18
19use techy::core::node::NodeRef;
20
21use crate::core::lowering::{
22 DefaultMark, Lower, LowerCx, LowerError, Lowerer, Lowering, LoweringDecl, LoweringRule,
23};
24use crate::core::registry::{BuildError, FeatureBuilder};
25use crate::lang::Flm;
26use crate::stdlib::backends::latex_recomposed::BACKEND;
27
28use super::{ENTRY, LIST, PIECE};
29
30/// Returns the name of the rule of the element `element` on the
31/// `latex-recomposed` backend, `<element>.lower.latex-recomposed`.
32pub fn rule_name(element: &str) -> String {
33 format!("{element}.lower.latex-recomposed")
34}
35
36/// The rule of each element of the table of contents on the
37/// `latex-recomposed` backend: it writes nothing (see the [module
38/// documentation](self)).
39#[derive(Debug, Default, Clone, Copy)]
40pub struct NothingRule;
41
42impl Lower for NothingRule {
43 fn lower(
44 &self,
45 _node: NodeRef<'_, Flm>,
46 _cx: &mut LowerCx<'_>,
47 _lw: &mut dyn Lowerer,
48 ) -> Result<Lowering, LowerError> {
49 Ok(Lowering::Emit(Vec::new()))
50 }
51}
52
53/// Registers the rule [`NothingRule`] of each of the three elements, marked
54/// default on the `latex-recomposed` backend only, under the peer declaration
55/// of that backend.
56pub(super) fn register(b: &mut FeatureBuilder<'_>) -> Result<(), BuildError> {
57 b.peer_backend(BACKEND, "the recomposed-LaTeX backend, on which the table of contents writes nothing")?;
58 for element in [PIECE, LIST, ENTRY] {
59 b.lowering_rule(
60 LoweringRule::new(
61 rule_name(element),
62 element,
63 LoweringDecl::new(),
64 Arc::new(NothingRule),
65 DefaultMark::OnBackends(vec![BACKEND.into()]),
66 )
67 .docs("the table of contents writes nothing in the recomposed LaTeX: LaTeX builds its own"),
68 )?;
69 }
70 Ok(())
71}