pub mod latex;
use std::sync::Arc;
use techy::core::node::NodeRef;
use crate::model::aspects::{BlockRole, BlockRoleAspect, FixedBlockRole};
use crate::model::element::{ElementSpec, element_value};
use crate::core::lowering::{
DefaultMark, Lower, LowerCx, LowerError, Lowerer, Lowering, LoweringDecl, LoweringRule,
};
use crate::model::names::ElementName;
use crate::core::registry::{BuildError, FeatureBuilder, FeatureInfo, FlmFeature};
use crate::lang::Flm;
use crate::lang::mint::{ArgDef, ElementDef, SyntaxDef};
use crate::stdlib::backends::lowering::lower_arg_or_nothing;
use crate::stdlib::backends::pres::{self, SpanPayload, SpanStyle};
pub const FEATURE: &str = "textstyles";
pub const TEXT_STYLE: &str = "flm.textstyle";
pub const RULE: &str = "flm.textstyle.lower.default";
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum TextStyle {
Emphasis,
Bold,
Italic,
Monospace,
}
impl TextStyle {
pub const ALL: [TextStyle; 4] =
[TextStyle::Emphasis, TextStyle::Bold, TextStyle::Italic, TextStyle::Monospace];
pub fn command(self) -> &'static str {
match self {
TextStyle::Emphasis => "emph",
TextStyle::Bold => "textbf",
TextStyle::Italic => "textit",
TextStyle::Monospace => "texttt",
}
}
pub fn span_style(self) -> SpanStyle {
match self {
TextStyle::Emphasis => SpanStyle::Emphasis,
TextStyle::Bold => SpanStyle::Bold,
TextStyle::Italic => SpanStyle::Italic,
TextStyle::Monospace => SpanStyle::Monospace,
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct TextStyleElement {
pub style: TextStyle,
}
fn element(style: TextStyle) -> Arc<ElementSpec> {
Arc::new(
ElementSpec::new(TEXT_STYLE)
.docs(match style {
TextStyle::Emphasis => "Semantic emphasis of its content.",
TextStyle::Bold => "Its content in bold weight.",
TextStyle::Italic => "Its content in italic shape.",
TextStyle::Monospace => "Its content in a monospaced face.",
})
.with::<dyn BlockRoleAspect>(Arc::new(FixedBlockRole(BlockRole::DEFAULT)))
.with_value(Arc::new(TextStyleElement { style })),
)
}
#[derive(Debug, Default)]
pub struct TextStyleRule;
impl Lower for TextStyleRule {
fn lower(
&self,
node: NodeRef<'_, Flm>,
cx: &mut LowerCx<'_>,
lw: &mut dyn Lowerer,
) -> Result<Lowering, LowerError> {
let value = element_value::<TextStyleElement>(&node)
.ok_or_else(|| LowerError::structure("a text-style node without its value"))?;
let body = lower_arg_or_nothing(cx, lw, &node, 0)?;
let payload =
SpanPayload { style: Some(value.style.span_style()), ..SpanPayload::default() };
let span = lw.pres_with(cx, &ElementName::from(pres::SPAN), payload, body)?;
Ok(Lowering::Emit(vec![span]))
}
}
#[derive(Debug, Default, Clone, Copy)]
pub struct TextStyles;
impl FlmFeature for TextStyles {
fn info(&self) -> FeatureInfo {
FeatureInfo::new(FEATURE, "Text styles").docs(
"The inline formatting commands `\\emph`, `\\textbf`, `\\textit` and `\\texttt`: \
four values of the element type `flm.textstyle`, each lowered to a \
`flm.pres.span` node that carries its style.",
)
}
fn register(&self, b: &mut FeatureBuilder<'_>) -> Result<(), BuildError> {
let owner = b.feature().clone();
for style in TextStyle::ALL {
b.element(
ElementDef::new(
owner.clone(),
[style.command()],
SyntaxDef::Macro { args: vec![ArgDef::mandatory("content")] },
element(style),
)
.docs(match style {
TextStyle::Emphasis => "Emphasizes its content.",
TextStyle::Bold => "Sets its content in bold weight.",
TextStyle::Italic => "Sets its content in italic shape.",
TextStyle::Monospace => "Sets its content in a monospaced face.",
}),
)?;
}
b.element_type_text(
TEXT_STYLE,
"Inline content set in a text style: emphasis, bold weight, italic shape or a \
monospaced face.",
)?;
b.lowering_rule(
LoweringRule::new(
RULE,
TEXT_STYLE,
LoweringDecl::new().produces(pres::SPAN),
Arc::new(TextStyleRule),
DefaultMark::Everywhere,
)
.docs("a formatting command lowers to `flm.pres.span` carrying its style"),
)?;
latex::register(b)?;
Ok(())
}
}
pub fn feature() -> Arc<dyn FlmFeature> {
Arc::new(TextStyles)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn every_style_names_its_command_and_span_style() {
assert_eq!(TextStyle::Emphasis.command(), "emph");
assert_eq!(TextStyle::Monospace.span_style(), SpanStyle::Monospace);
assert_eq!(TextStyle::ALL.len(), 4);
}
#[test]
fn an_element_value_carries_its_style_and_the_inline_role() {
let element = element(TextStyle::Bold);
assert_eq!(element.name, TEXT_STYLE);
assert_eq!(element.value_as::<TextStyleElement>().map(|v| v.style), Some(TextStyle::Bold));
assert!(element.has_aspect::<dyn BlockRoleAspect>());
}
}