flm_core/stdlib/features/metadata/
text.rs1use 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
62pub fn text_rule_name(element: &str) -> String {
65 format!("{element}.lower.text")
66}
67
68#[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#[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
111pub(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}