use std::sync::Arc;
use techy::core::node::{BuildId, 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;
use crate::stdlib::backends::latex::construct::{ARG_SLOT, MACRO};
use crate::stdlib::backends::latex::{LatexArgShape, LatexMacroPayload};
use crate::stdlib::backends::latex_recomposed::BACKEND;
use super::rules::{each_child, email_of, inline_content};
use super::{AUTHOR, AUTHOR_AFFILIATION, AUTHOR_EMAIL, AUTHOR_ENTRY, AUTHOR_NAME, AUTHOR_NOTE, DATE, TITLE};
const LINE_BREAK: &str = "\\";
const THANKS: &str = "thanks";
pub fn latex_rule_name(element: &str) -> String {
format!("{element}.lower.latex-recomposed")
}
fn command(cx: &mut LowerCx<'_>, lw: &mut dyn Lowerer, name: &str) -> Result<BuildId, LowerError> {
lw.pres_slots_with(cx, &ElementName::from(MACRO), LatexMacroPayload::new(name, []), Vec::new())
}
#[derive(Debug, Default)]
pub struct MetaTextLatexRule;
impl Lower for MetaTextLatexRule {
fn lower(
&self,
node: NodeRef<'_, Flm>,
cx: &mut LowerCx<'_>,
lw: &mut dyn Lowerer,
) -> Result<Lowering, LowerError> {
Ok(Lowering::Emit(inline_content(&node, cx, lw)?))
}
}
#[derive(Debug, Default)]
pub struct AuthorLatexRule;
impl Lower for AuthorLatexRule {
fn lower(
&self,
node: NodeRef<'_, Flm>,
cx: &mut LowerCx<'_>,
lw: &mut dyn Lowerer,
) -> Result<Lowering, LowerError> {
let mut joined = Vec::new();
for one in each_child(&node)? {
if !joined.is_empty() {
joined.push(lw.text(cx, " ")?);
joined.push(command(cx, lw, "and")?);
joined.push(lw.text(cx, " ")?);
}
joined.extend(lw.lower_nodes(cx, one)?);
}
Ok(Lowering::Emit(joined))
}
}
#[derive(Debug, Default)]
pub struct EntryLatexRule;
impl Lower for EntryLatexRule {
fn lower(
&self,
node: NodeRef<'_, Flm>,
cx: &mut LowerCx<'_>,
lw: &mut dyn Lowerer,
) -> Result<Lowering, LowerError> {
let mut name = Vec::new();
let mut notes = Vec::new();
let mut lines = Vec::new();
for (one, child) in each_child(&node)?.into_iter().zip(node.children().iter()) {
let part = lw.lower_nodes(cx, one)?;
match child.ext().element.name.as_str() {
AUTHOR_NOTE => notes.extend(part),
AUTHOR_AFFILIATION | AUTHOR_EMAIL => lines.push(part),
_ => name.extend(part),
}
}
let mut written = name;
written.extend(notes);
for line in lines {
written.push(lw.text(cx, " ")?);
written.push(command(cx, lw, LINE_BREAK)?);
written.push(lw.text(cx, " ")?);
written.extend(line);
}
Ok(Lowering::Emit(written))
}
}
#[derive(Debug, Default)]
pub struct NoteLatexRule;
impl Lower for NoteLatexRule {
fn lower(
&self,
node: NodeRef<'_, Flm>,
cx: &mut LowerCx<'_>,
lw: &mut dyn Lowerer,
) -> Result<Lowering, LowerError> {
let content = inline_content(&node, cx, lw)?;
let thanks = LatexMacroPayload::new(THANKS, [LatexArgShape::Mandatory]);
let call = lw.pres_slots_with(cx, &ElementName::from(MACRO), thanks, vec![(String::from(ARG_SLOT), content)])?;
Ok(Lowering::Emit(vec![call]))
}
}
#[derive(Debug, Default)]
pub struct EmailLatexRule;
impl Lower for EmailLatexRule {
fn lower(
&self,
node: NodeRef<'_, Flm>,
cx: &mut LowerCx<'_>,
lw: &mut dyn Lowerer,
) -> Result<Lowering, LowerError> {
let address = email_of(&node)?;
Ok(Lowering::Emit(vec![lw.text(cx, address)?]))
}
}
pub(super) fn register(b: &mut FeatureBuilder<'_>) -> Result<(), BuildError> {
b.peer_backend(
BACKEND,
"the latex-recomposed backend, on which the metadata's LaTeX rules are the default",
)?;
b.peer_element::<LatexMacroPayload>(
MACRO,
"the LaTeX family's macro-call construct, which the rules of the authors stage for `\\and`, `\\\\` \
and `\\thanks`",
)?;
let on_backend = || DefaultMark::OnBackends(vec![BACKEND.into()]);
for element in [TITLE, DATE, AUTHOR_NAME, AUTHOR_AFFILIATION] {
b.lowering_rule(
LoweringRule::new(
latex_rule_name(element),
element,
LoweringDecl::new(),
Arc::new(MetaTextLatexRule),
on_backend(),
)
.docs(format!("on the recomposed LaTeX, `{element}` is its content, written back as LaTeX")),
)?;
}
let rules: [(&str, Arc<dyn Lower>, &str); 4] = [
(AUTHOR, Arc::new(AuthorLatexRule), "on the recomposed LaTeX, the authors are each author, joined by `\\and`"),
(
AUTHOR_ENTRY,
Arc::new(EntryLatexRule),
"on the recomposed LaTeX, an author is the name, each note, then each affiliation and the email \
address after `\\\\`",
),
(AUTHOR_NOTE, Arc::new(NoteLatexRule), "on the recomposed LaTeX, a note is `\\thanks` around its content"),
(AUTHOR_EMAIL, Arc::new(EmailLatexRule), "on the recomposed LaTeX, an email address is its text"),
];
for (element, rule, docs) in rules {
let decl = if element == AUTHOR_EMAIL { LoweringDecl::new() } else { LoweringDecl::new().produces(MACRO) };
b.lowering_rule(LoweringRule::new(latex_rule_name(element), element, decl, rule, on_backend()).docs(docs))?;
}
Ok(())
}