flm-core 0.7.0

FLM semantic markup framework — core library, built on techy
Documentation
//! The lowering rules of the metadata's pieces on the `text` backend: the title set
//! between two rule lines, and each author's parts under the name.
//!
//! - The title ([`TitleTextRule`]) is the block of the default rule ([`MetaTextRule`])
//!   holding a [`flm.pres.text.ruled`](RULED) node with a rule line above and under it,
//!   over one paragraph whose content is the title in a span of style
//!   [`Bold`](SpanStyle::Bold):
//!
//!   ```text
//!   flm.pres.block { role: flm.metadata.title, kinds: [metadata] }
//!     └ flm.pres.text.ruled { above: true, below: true }
//!         └ flm.pres.par
//!             └ flm.pres.span { style: bold }
//!                 └ the title, lowered through the rules
//!   ```
//!
//!   Each rule line repeats the character of a heading at the root level
//!   ([`TextRenderOptions::heading_rules`](crate::stdlib::backends::text::TextRenderOptions::heading_rules))
//!   to the width of the title's widest printed line.
//! - One author ([`EntryTextRule`]) is the lines of the default rule ([`EntryRule`]),
//!   the name at the margin and each other part (each affiliation, the email address,
//!   each note) indented by `1em`, two columns:
//!
//!   ```text
//!   ===========================
//!   Bounds on approximate codes
//!   ===========================
//!
//!   First Author
//!     University of X and Z, Somewhere
//!     first@example.com
//!   ```
//!
//! The date and the authors' block keep the default rules ([`rules`](super::rules)).
//!
//! The rules are the defaults on the `text` backend only. The rule for the title stages
//! the construct element `flm.pres.text.ruled`. The feature registers the rules with
//! peer declarations ([`PeerName`](crate::core::registry::PeerName)) of the backend and
//! of that element. A registry without the `text` feature therefore still builds, with
//! the rules set aside (excluded from rule selection).
//!
//! [`MetaTextRule`]: super::rules::MetaTextRule
//! [`EntryRule`]: super::rules::EntryRule

use std::sync::Arc;

use techy::core::node::NodeRef;

use crate::core::lowering::{
    DefaultMark, Lower, LowerCx, LowerError, Lowerer, Lowering, LoweringDecl, LoweringRule,
};
use crate::core::registry::{BuildError, FeatureBuilder};
use crate::lang::Flm;
use crate::model::names::{ElementName, Name};
use crate::stdlib::backends::pres::{self, I48F16, Length, LengthUnit, ParPayload, SpanPayload, SpanStyle};
use crate::stdlib::backends::text::construct::{RULED, RuledPayload};
use crate::stdlib::backends::text::BACKEND;

use super::rules::{block, entry_lines, inline_content};
use super::{AUTHOR_ENTRY, AUTHOR_NAME, STYLE, TITLE};

/// Returns the name of the lowering rule of the element `element` on the `text`
/// backend, `<element>.lower.text`.
pub fn text_rule_name(element: &str) -> String {
    format!("{element}.lower.text")
}

/// The lowering rule of the title on the `text` backend: the block of the default
/// rule, holding a `flm.pres.text.ruled` node with a rule line above and under it,
/// over one paragraph whose content is the title in a span of style `bold` (see the
/// [module documentation](self)).
#[derive(Debug, Default)]
pub struct TitleTextRule;

impl Lower for TitleTextRule {
    fn lower(
        &self,
        node: NodeRef<'_, Flm>,
        cx: &mut LowerCx<'_>,
        lw: &mut dyn Lowerer,
    ) -> Result<Lowering, LowerError> {
        lw.require(&Name::from(STYLE));
        let content = inline_content(&node, cx, lw)?;
        let bold = SpanPayload { style: Some(SpanStyle::Bold), ..SpanPayload::default() };
        let span = lw.pres_with(cx, &ElementName::from(pres::SPAN), bold, content)?;
        let par = lw.pres_with(cx, &ElementName::from(pres::PAR), ParPayload::default(), vec![span])?;
        let ruled = lw.pres_with(cx, &ElementName::from(RULED), RuledPayload { above: true, below: true }, vec![par])?;
        Ok(Lowering::Emit(vec![lw.pres_with(cx, &ElementName::from(pres::BLOCK), block(TITLE), vec![ruled])?]))
    }
}

/// The lowering rule of one author on the `text` backend: the lines of the default
/// rule, the name at the margin and each other part indented by `1em`.
#[derive(Debug, Default)]
pub struct EntryTextRule;

impl Lower for EntryTextRule {
    fn lower(
        &self,
        node: NodeRef<'_, Flm>,
        cx: &mut LowerCx<'_>,
        lw: &mut dyn Lowerer,
    ) -> Result<Lowering, LowerError> {
        let indent = |element: &str| {
            if element == AUTHOR_NAME { Length::default() } else { Length::new(I48F16::ONE, LengthUnit::Ems) }
        };
        Ok(Lowering::Emit(vec![entry_lines(&node, cx, lw, indent)?]))
    }
}

/// Registers the rules, with the peer declarations of the backend and of the
/// construct element they need.
pub(super) fn register(b: &mut FeatureBuilder<'_>) -> Result<(), BuildError> {
    b.peer_backend(BACKEND, "the text backend, on which the metadata's text rules are the default")?;
    b.peer_element::<RuledPayload>(RULED, "the text backend's ruled construct, which the title's rule stages")?;
    let on_backend = || DefaultMark::OnBackends(vec![BACKEND.into()]);
    b.lowering_rule(
        LoweringRule::new(
            text_rule_name(TITLE),
            TITLE,
            LoweringDecl::new().produces(pres::BLOCK).produces(RULED).produces(pres::PAR).produces(pres::SPAN),
            Arc::new(TitleTextRule),
            on_backend(),
        )
        .docs(
            "on the text output, the title is a `flm.pres.block` holding a `flm.pres.text.ruled` with a rule \
             above and under, over one paragraph whose content is the title in a bold span",
        ),
    )?;
    b.lowering_rule(
        LoweringRule::new(
            text_rule_name(AUTHOR_ENTRY),
            AUTHOR_ENTRY,
            LoweringDecl::new().produces(pres::LINES).produces(pres::LINE),
            Arc::new(EntryTextRule),
            on_backend(),
        )
        .docs(
            "on the text output, an author is a `flm.pres.lines`, the name at the margin and each other part \
             on a `flm.pres.line` indented by `1em`",
        ),
    )?;
    Ok(())
}