use std::sync::Arc;
use techy::core::node::{BuildId, NodeRef, NodeSlice};
use crate::core::lowering::{
DefaultMark, Lower, LowerCx, LowerError, Lowerer, Lowering, LoweringDecl, LoweringRule,
};
use crate::core::registry::{BuildError, FeatureBuilder};
use crate::lang::mint::{ElementDef, SyntaxDef};
use crate::lang::Flm;
use crate::model::aspects::{BlockRole, BlockRoleAspect, FixedBlockRole};
use crate::model::element::ElementSpec;
use crate::model::names::{ElementName, Name};
use crate::stdlib::backends::lowering::lower_blocks_with_edges;
use crate::stdlib::backends::pres::{self, BlockPayload, LinePayload, LinesPayload, LinkPayload, SpanPayload};
use crate::stdlib::features::href::LINK_KIND;
use super::{
EmailPayload, AUTHOR, AUTHOR_AFFILIATION, AUTHOR_EMAIL, AUTHOR_ENTRY, AUTHOR_NAME, AUTHOR_NOTE, DATE,
KIND_METADATA, STYLE, TITLE,
};
const MAILTO: &str = "mailto:";
pub fn rule_name(element: &str) -> String {
format!("{element}.lower.default")
}
fn block(role: &str) -> BlockPayload {
BlockPayload { role: Some(Name::from(role)), kinds: vec![Name::from(KIND_METADATA)], anchor: None, doc_role: None }
}
fn span(role: &str) -> SpanPayload {
SpanPayload { role: Some(Name::from(role)), ..SpanPayload::default() }
}
pub(super) fn each_child<'t>(node: &NodeRef<'t, Flm>) -> Result<Vec<NodeSlice<'t, Flm>>, LowerError> {
let children = node.children();
let start = children.range().start;
(0u32..)
.zip(children.iter())
.map(|(index, _)| {
node.tree()
.slice(start + index..start + index + 1)
.ok_or_else(|| LowerError::structure("a child outside its parent's children"))
})
.collect()
}
pub(super) fn inline_content(
node: &NodeRef<'_, Flm>,
cx: &mut LowerCx<'_>,
lw: &mut dyn Lowerer,
) -> Result<Vec<BuildId>, LowerError> {
let paragraph = Arc::clone(&lw.lang().structural().paragraph);
let mut content = Vec::new();
for (one, child) in each_child(node)?.into_iter().zip(node.children().iter()) {
if child.ext().element.name == paragraph.name {
content.extend(lw.lower_children(cx, &child)?);
} else {
content.extend(lw.lower_nodes(cx, one)?);
}
}
Ok(content)
}
#[derive(Debug)]
pub struct MetaTextRule {
role: &'static str,
}
impl Lower for MetaTextRule {
fn lower(
&self,
node: NodeRef<'_, Flm>,
cx: &mut LowerCx<'_>,
lw: &mut dyn Lowerer,
) -> Result<Lowering, LowerError> {
lw.require(&Name::from(STYLE));
let content: Vec<NodeRef<'_, Flm>> = node.children().iter().collect();
let body = lower_blocks_with_edges(cx, lw, &content, Vec::new(), Vec::new())?;
Ok(Lowering::Emit(vec![lw.pres_with(cx, &ElementName::from(pres::BLOCK), block(self.role), body)?]))
}
}
#[derive(Debug, Default)]
pub struct AuthorRule;
impl Lower for AuthorRule {
fn lower(
&self,
node: NodeRef<'_, Flm>,
cx: &mut LowerCx<'_>,
lw: &mut dyn Lowerer,
) -> Result<Lowering, LowerError> {
lw.require(&Name::from(STYLE));
let body = lw.lower_children(cx, &node)?;
Ok(Lowering::Emit(vec![lw.pres_with(cx, &ElementName::from(pres::BLOCK), block(AUTHOR), body)?]))
}
}
#[derive(Debug, Default)]
pub struct EntryRule;
impl Lower for EntryRule {
fn lower(
&self,
node: NodeRef<'_, Flm>,
cx: &mut LowerCx<'_>,
lw: &mut dyn Lowerer,
) -> Result<Lowering, LowerError> {
let mut lines = Vec::new();
for one in each_child(&node)? {
let part = lw.lower_nodes(cx, one)?;
lines.push(lw.pres_with(cx, &ElementName::from(pres::LINE), LinePayload::default(), part)?);
}
Ok(Lowering::Emit(vec![lw.pres_with(cx, &ElementName::from(pres::LINES), LinesPayload, lines)?]))
}
}
#[derive(Debug)]
pub struct PartRule {
role: &'static str,
}
impl Lower for PartRule {
fn lower(
&self,
node: NodeRef<'_, Flm>,
cx: &mut LowerCx<'_>,
lw: &mut dyn Lowerer,
) -> Result<Lowering, LowerError> {
let content = inline_content(&node, cx, lw)?;
Ok(Lowering::Emit(vec![lw.pres_with(cx, &ElementName::from(pres::SPAN), span(self.role), content)?]))
}
}
#[derive(Debug, Default)]
pub struct EmailRule;
impl Lower for EmailRule {
fn lower(
&self,
node: NodeRef<'_, Flm>,
cx: &mut LowerCx<'_>,
lw: &mut dyn Lowerer,
) -> Result<Lowering, LowerError> {
let address = email_of(&node)?;
let payload = LinkPayload {
href: format!("{MAILTO}{address}"),
kind: Name::from(LINK_KIND),
doc_role: None,
label: None,
shows_target: true,
};
let text = lw.text(cx, address)?;
let link = lw.pres_with(cx, &ElementName::from(pres::LINK), payload, vec![text])?;
Ok(Lowering::Emit(vec![lw.pres_with(cx, &ElementName::from(pres::SPAN), span(AUTHOR_EMAIL), vec![link])?]))
}
}
pub(super) fn email_of<'n>(node: &'n NodeRef<'_, Flm>) -> Result<&'n str, LowerError> {
EmailPayload::of(node)
.map(|payload| payload.address.as_str())
.ok_or_else(|| LowerError::structure("missing ‘EmailPayload’ on an author’s email node"))
}
pub(super) fn declare(b: &mut FeatureBuilder<'_>) -> Result<(), BuildError> {
let owner = b.feature().clone();
let elements = [
(TITLE, "The document's title.", "The title of the document, built from its metadata."),
(
AUTHOR,
"The document's authors, one author per child.",
"The list of the document's authors, built from its metadata.",
),
(
AUTHOR_ENTRY,
"One author of the document: the name, then each affiliation, the email address and each note.",
"One author of the document, in the list of its authors.",
),
(AUTHOR_NAME, "An author's name.", "The name of one author of the document."),
(
AUTHOR_AFFILIATION,
"One affiliation of an author, with its number among the document's affiliations.",
"One affiliation of one author of the document.",
),
(
AUTHOR_EMAIL,
"An author's email address, a plain text.",
"The email address of one author of the document.",
),
(AUTHOR_NOTE, "One note about an author.", "One note about one author of the document."),
(DATE, "The document's date.", "The date of the document, built from its metadata."),
];
for (name, docs, type_docs) in elements {
b.element_type_text(name, type_docs)?;
let role = Arc::new(FixedBlockRole(BlockRole::Block)) as Arc<dyn BlockRoleAspect>;
b.element(
ElementDef::new(
owner.clone(),
Vec::<&str>::new(),
SyntaxDef::Synthesized,
Arc::new(ElementSpec::new(name).docs(docs).with(role)),
)
.docs(format!("{docs} No document can write it: a stage builds it from the metadata.")),
)?;
}
for element in [TITLE, DATE] {
b.lowering_rule(
LoweringRule::new(
rule_name(element),
element,
LoweringDecl::new().produces(pres::BLOCK).produces(pres::PAR),
Arc::new(MetaTextRule { role: element }),
DefaultMark::Everywhere,
)
.docs(format!("`{element}` lowers to a `flm.pres.block` holding the text as one paragraph")),
)?;
}
b.lowering_rule(
LoweringRule::new(
rule_name(AUTHOR),
AUTHOR,
LoweringDecl::new().produces(pres::BLOCK),
Arc::new(AuthorRule),
DefaultMark::Everywhere,
)
.docs("the authors lower to a `flm.pres.block` holding each author in order"),
)?;
b.lowering_rule(
LoweringRule::new(
rule_name(AUTHOR_ENTRY),
AUTHOR_ENTRY,
LoweringDecl::new().produces(pres::LINES).produces(pres::LINE),
Arc::new(EntryRule),
DefaultMark::Everywhere,
)
.docs("an author lowers to a `flm.pres.lines`, each part of the author on a `flm.pres.line` of its own"),
)?;
for element in [AUTHOR_NAME, AUTHOR_AFFILIATION, AUTHOR_NOTE] {
b.lowering_rule(
LoweringRule::new(
rule_name(element),
element,
LoweringDecl::new().produces(pres::SPAN),
Arc::new(PartRule { role: element }),
DefaultMark::Everywhere,
)
.docs(format!("`{element}` lowers to a `flm.pres.span` of role `{element}` holding the text")),
)?;
}
b.lowering_rule(
LoweringRule::new(
rule_name(AUTHOR_EMAIL),
AUTHOR_EMAIL,
LoweringDecl::new().produces(pres::SPAN).produces(pres::LINK),
Arc::new(EmailRule),
DefaultMark::Everywhere,
)
.docs(
"an email address lowers to a `flm.pres.span` of role `flm.metadata.author.email` holding a \
`flm.pres.link` of kind `url` to `mailto:` followed by the address, showing the address",
),
)?;
Ok(())
}