Skip to main content

flm_core/stdlib/features/metadata/
text.rs

1//! The lowering rules of the metadata's pieces on the `text` backend: the title set
2//! between two rule lines, and each author's parts under the name.
3//!
4//! - The title ([`TitleTextRule`]) is the block of the default rule ([`MetaTextRule`])
5//!   holding a [`flm.pres.text.ruled`](RULED) node with a rule line above and under it,
6//!   over one paragraph whose content is the title in a span of style
7//!   [`Bold`](SpanStyle::Bold):
8//!
9//!   ```text
10//!   flm.pres.block { role: flm.metadata.title, kinds: [metadata] }
11//!     └ flm.pres.text.ruled { above: true, below: true }
12//!         └ flm.pres.par
13//!             └ flm.pres.span { style: bold }
14//!                 └ the title, lowered through the rules
15//!   ```
16//!
17//!   Each rule line repeats the character of a heading at the root level
18//!   ([`TextRenderOptions::heading_rules`](crate::stdlib::backends::text::TextRenderOptions::heading_rules))
19//!   to the width of the title's widest printed line.
20//! - One author ([`EntryTextRule`]) is the lines of the default rule ([`EntryRule`]),
21//!   the name at the margin and each other part (each affiliation, the email address,
22//!   each note) indented by `1em`, two columns:
23//!
24//!   ```text
25//!   ===========================
26//!   Bounds on approximate codes
27//!   ===========================
28//!
29//!   First Author
30//!     University of X and Z, Somewhere
31//!     first@example.com
32//!   ```
33//!
34//! The date and the authors' block keep the default rules ([`rules`](super::rules)).
35//!
36//! The rules are the defaults on the `text` backend only. The rule for the title stages
37//! the construct element `flm.pres.text.ruled`. The feature registers the rules with
38//! peer declarations ([`PeerName`](crate::core::registry::PeerName)) of the backend and
39//! of that element. A registry without the `text` feature therefore still builds, with
40//! the rules set aside (excluded from rule selection).
41//!
42//! [`MetaTextRule`]: super::rules::MetaTextRule
43//! [`EntryRule`]: super::rules::EntryRule
44
45use std::sync::Arc;
46
47use techy::core::node::NodeRef;
48
49use crate::core::lowering::{
50    DefaultMark, Lower, LowerCx, LowerError, Lowerer, Lowering, LoweringDecl, LoweringRule,
51};
52use crate::core::registry::{BuildError, FeatureBuilder};
53use crate::lang::Flm;
54use crate::model::names::{ElementName, Name};
55use crate::stdlib::backends::pres::{self, I48F16, Length, LengthUnit, ParPayload, SpanPayload, SpanStyle};
56use crate::stdlib::backends::text::construct::{RULED, RuledPayload};
57use crate::stdlib::backends::text::BACKEND;
58
59use super::rules::{block, entry_lines, inline_content};
60use super::{AUTHOR_ENTRY, AUTHOR_NAME, STYLE, TITLE};
61
62/// Returns the name of the lowering rule of the element `element` on the `text`
63/// backend, `<element>.lower.text`.
64pub fn text_rule_name(element: &str) -> String {
65    format!("{element}.lower.text")
66}
67
68/// The lowering rule of the title on the `text` backend: the block of the default
69/// rule, holding a `flm.pres.text.ruled` node with a rule line above and under it,
70/// over one paragraph whose content is the title in a span of style `bold` (see the
71/// [module documentation](self)).
72#[derive(Debug, Default)]
73pub struct TitleTextRule;
74
75impl Lower for TitleTextRule {
76    fn lower(
77        &self,
78        node: NodeRef<'_, Flm>,
79        cx: &mut LowerCx<'_>,
80        lw: &mut dyn Lowerer,
81    ) -> Result<Lowering, LowerError> {
82        lw.require(&Name::from(STYLE));
83        let content = inline_content(&node, cx, lw)?;
84        let bold = SpanPayload { style: Some(SpanStyle::Bold), ..SpanPayload::default() };
85        let span = lw.pres_with(cx, &ElementName::from(pres::SPAN), bold, content)?;
86        let par = lw.pres_with(cx, &ElementName::from(pres::PAR), ParPayload::default(), vec![span])?;
87        let ruled = lw.pres_with(cx, &ElementName::from(RULED), RuledPayload { above: true, below: true }, vec![par])?;
88        Ok(Lowering::Emit(vec![lw.pres_with(cx, &ElementName::from(pres::BLOCK), block(TITLE), vec![ruled])?]))
89    }
90}
91
92/// The lowering rule of one author on the `text` backend: the lines of the default
93/// rule, the name at the margin and each other part indented by `1em`.
94#[derive(Debug, Default)]
95pub struct EntryTextRule;
96
97impl Lower for EntryTextRule {
98    fn lower(
99        &self,
100        node: NodeRef<'_, Flm>,
101        cx: &mut LowerCx<'_>,
102        lw: &mut dyn Lowerer,
103    ) -> Result<Lowering, LowerError> {
104        let indent = |element: &str| {
105            if element == AUTHOR_NAME { Length::default() } else { Length::new(I48F16::ONE, LengthUnit::Ems) }
106        };
107        Ok(Lowering::Emit(vec![entry_lines(&node, cx, lw, indent)?]))
108    }
109}
110
111/// Registers the rules, with the peer declarations of the backend and of the
112/// construct element they need.
113pub(super) fn register(b: &mut FeatureBuilder<'_>) -> Result<(), BuildError> {
114    b.peer_backend(BACKEND, "the text backend, on which the metadata's text rules are the default")?;
115    b.peer_element::<RuledPayload>(RULED, "the text backend's ruled construct, which the title's rule stages")?;
116    let on_backend = || DefaultMark::OnBackends(vec![BACKEND.into()]);
117    b.lowering_rule(
118        LoweringRule::new(
119            text_rule_name(TITLE),
120            TITLE,
121            LoweringDecl::new().produces(pres::BLOCK).produces(RULED).produces(pres::PAR).produces(pres::SPAN),
122            Arc::new(TitleTextRule),
123            on_backend(),
124        )
125        .docs(
126            "on the text output, the title is a `flm.pres.block` holding a `flm.pres.text.ruled` with a rule \
127             above and under, over one paragraph whose content is the title in a bold span",
128        ),
129    )?;
130    b.lowering_rule(
131        LoweringRule::new(
132            text_rule_name(AUTHOR_ENTRY),
133            AUTHOR_ENTRY,
134            LoweringDecl::new().produces(pres::LINES).produces(pres::LINE),
135            Arc::new(EntryTextRule),
136            on_backend(),
137        )
138        .docs(
139            "on the text output, an author is a `flm.pres.lines`, the name at the margin and each other part \
140             on a `flm.pres.line` indented by `1em`",
141        ),
142    )?;
143    Ok(())
144}