flm_core/stdlib/features/textstyles/latex.rs
1//! The recomposition rule of the [`textstyles`](super) feature for the
2//! `latex-recomposed` backend: each formatting command is written back as the
3//! LaTeX macro of the same name.
4//!
5//! [`LATEX_RULE`] (`flm.textstyle.lower.latex-recomposed`) is the default rule of
6//! `flm.textstyle` on the `latex-recomposed` backend only. It stages (builds into
7//! the lowered tree; see [`Lowerer`]) a macro
8//! construct, [`flm.pres.latex.macro`](crate::stdlib::backends::latex::construct::MACRO),
9//! named after the command the author wrote (`\emph`, `\textbf`, `\textit` or
10//! `\texttt`; [`TextStyle::command`](super::TextStyle::command)), with one
11//! mandatory argument that holds the command's argument lowered through the
12//! rules. A construct element is a presentation element that mirrors one
13//! construct of LaTeX, here a macro call ([backends, lowering and
14//! outputs](crate::guide::concepts_overview#backends-lowering-and-outputs)). The
15//! LaTeX kernel defines the four commands, so the rule requests no LaTeX package.
16//!
17//! When the argument is missing (a parse recovery left it absent and the parse
18//! reported it), the macro is written with an empty argument, `\emph{}`, and
19//! nothing more is reported.
20//!
21//! The feature does not own the backend or the macro construct. [`register`]
22//! declares both as peer declarations ([`PeerName`](crate::core::registry::PeerName)),
23//! so that a registry without the LaTeX features still builds; the rule is then
24//! set aside (excluded from rule selection; see
25//! [`PeerName`](crate::core::registry::PeerName)).
26
27use std::sync::Arc;
28
29use techy::core::node::NodeRef;
30
31use crate::core::lowering::{
32 DefaultMark, Lower, LowerCx, LowerError, Lowerer, Lowering, LoweringDecl, LoweringRule,
33};
34use crate::core::registry::{BuildError, FeatureBuilder};
35use crate::lang::Flm;
36use crate::model::element::element_value;
37use crate::model::names::ElementName;
38use crate::stdlib::backends::latex::construct::{ARG_SLOT, MACRO};
39use crate::stdlib::backends::latex::{LatexArgShape, LatexMacroPayload};
40use crate::stdlib::backends::latex_recomposed::BACKEND;
41use crate::stdlib::backends::lowering::lower_arg_or_nothing;
42
43use super::{TEXT_STYLE, TextStyleElement};
44
45/// The name of the recomposition rule of `flm.textstyle`,
46/// `flm.textstyle.lower.latex-recomposed`, whose body is
47/// [`TextStyleLatexRule`].
48pub const LATEX_RULE: &str = "flm.textstyle.lower.latex-recomposed";
49
50/// The body of [`LATEX_RULE`]: it stages the formatting command as a macro
51/// construct of the same name, over the command's argument lowered through
52/// the rules (see the module documentation).
53///
54/// The rule returns a [`LowerError`] when the node carries no
55/// [`TextStyleElement`], or when the argument or the construct could not be
56/// lowered or staged.
57#[derive(Debug, Default, Clone, Copy)]
58pub struct TextStyleLatexRule;
59
60impl Lower for TextStyleLatexRule {
61 fn lower(
62 &self,
63 node: NodeRef<'_, Flm>,
64 cx: &mut LowerCx<'_>,
65 lw: &mut dyn Lowerer,
66 ) -> Result<Lowering, LowerError> {
67 let value = element_value::<TextStyleElement>(&node)
68 .ok_or_else(|| LowerError::structure("a text-style node without its element's data"))?;
69 let content = lower_arg_or_nothing(cx, lw, &node, 0)?;
70 let payload = LatexMacroPayload::new(value.style.command(), [LatexArgShape::Mandatory]);
71 let macro_call = lw.pres_slots_with(
72 cx,
73 &ElementName::from(MACRO),
74 payload,
75 vec![(String::from(ARG_SLOT), content)],
76 )?;
77 Ok(Lowering::Emit(vec![macro_call]))
78 }
79}
80
81/// The recomposition rule [`LATEX_RULE`], ready to be registered: it targets
82/// `flm.textstyle`, produces `flm.pres.latex.macro` and is the default on the
83/// `latex-recomposed` backend only.
84pub fn latex_rule() -> LoweringRule {
85 LoweringRule::new(
86 LATEX_RULE,
87 TEXT_STYLE,
88 LoweringDecl::new().produces(MACRO),
89 Arc::new(TextStyleLatexRule),
90 DefaultMark::OnBackends(vec![BACKEND.into()]),
91 )
92 .docs("a formatting command lowers to the LaTeX macro of the same name over its argument")
93}
94
95/// Registers what the `textstyles` feature contributes to the
96/// `latex-recomposed` backend: a peer declaration
97/// ([`PeerName`](crate::core::registry::PeerName)) of the backend and of the
98/// macro construct, with [`LatexMacroPayload`] as its payload type, then
99/// [`latex_rule`].
100///
101/// In a registry without the LaTeX features the two peer declarations resolve
102/// to nothing and the rule is set aside (excluded from rule selection); the
103/// registry still builds.
104///
105/// # Errors
106///
107/// Returns the [`BuildError`] the feature builder raises, for instance for a
108/// rule already registered under the same name.
109pub fn register(b: &mut FeatureBuilder<'_>) -> Result<(), BuildError> {
110 b.peer_backend(
111 BACKEND,
112 "the latex-recomposed backend, on which the recomposition rule of the formatting \
113 commands is the default",
114 )?;
115 b.peer_element::<LatexMacroPayload>(
116 MACRO,
117 "the LaTeX family's macro construct, which the recomposition rule of the formatting \
118 commands stages",
119 )?;
120 b.lowering_rule(latex_rule())
121}
122
123#[cfg(test)]
124mod tests {
125 use super::*;
126
127 #[test]
128 fn the_rule_is_the_default_on_the_recomposed_backend_only() {
129 let rule = latex_rule();
130 assert_eq!(rule.name, LATEX_RULE);
131 assert_eq!(rule.element, TEXT_STYLE);
132 assert_eq!(rule.decl.produces, [ElementName::from(MACRO)]);
133 assert!(rule.default.is_specific_to(&BACKEND.into()));
134 }
135}