use std::sync::Arc;
use techy::core::node::NodeRef;
use techy::latexlike::MathGroupForm;
use crate::lang::Flm;
use crate::model::aspects::{BlockRole, BlockRoleAspect, FixedBlockRole};
use crate::model::element::ElementSpec;
use crate::model::names::ElementName;
pub const TEXT: &str = "flm.text";
pub const COMMENT: &str = "flm.comment";
pub const CONTAINER: &str = "flm.container";
pub const GROUP: &str = "flm.group";
pub const MATH: &str = "flm.math";
pub const VERBATIM: &str = "flm.verbatim";
pub const OPAQUE_WRAPPER: &str = "flm.opaque-wrapper";
pub const PARAGRAPH: &str = "flm.paragraph";
pub const PARBREAK: &str = "flm.parbreak";
pub const PLACEHOLDER: &str = "flm.placeholder";
pub const FIELD: &str = "flm.field";
pub const UNKNOWN_MACRO: &str = "flm.unknown.macro";
pub const UNKNOWN_ENVIRONMENT: &str = "flm.unknown.environment";
pub const NAMES: [&str; 13] = [
TEXT,
COMMENT,
CONTAINER,
GROUP,
MATH,
VERBATIM,
OPAQUE_WRAPPER,
PARAGRAPH,
PARBREAK,
PLACEHOLDER,
FIELD,
UNKNOWN_MACRO,
UNKNOWN_ENVIRONMENT,
];
pub(crate) fn type_text(name: &str) -> Option<&'static str> {
let text = match name {
TEXT => "A run of text characters.",
COMMENT => "A comment written in the source, from `%` to the end of the line.",
CONTAINER => "A container: content held together, with no meaning of its own.",
GROUP => "A content group, written `{…}`.",
MATH => {
"A mathematics group: inline mathematics, written `\\(…\\)` or `$…$`, or display \
mathematics, written `\\[…\\]` or `$$…$$`."
}
VERBATIM => "A verbatim group: raw text, kept exactly as written.",
OPAQUE_WRAPPER => "Parsed content kept as written, in a group with no delimiters.",
PARAGRAPH => "A paragraph.",
PARBREAK => "A paragraph break: a blank line in the source.",
PLACEHOLDER => {
"A placeholder of a template, such as `#1` or `#{name}`, replaced by content when \
the template is expanded."
}
FIELD => {
"A field: a macro that a construct reads as data, not as content, such as the \
`\\label` written after a heading or the `\\tag` written in a line of displayed \
equations.\n\nEvery field has this type, \
whichever construct declares it; only the construct that declares a field reads it."
}
UNKNOWN_MACRO => "A macro that no feature of the document defines, reported as an error.",
UNKNOWN_ENVIRONMENT => {
"An environment that no feature of the document defines, reported as an error; its \
body is block content."
}
_ => return None,
};
Some(text)
}
#[derive(Debug, Clone)]
pub struct StructuralElementSpecs {
pub text: ElementSpec,
pub comment: ElementSpec,
pub container: ElementSpec,
pub group: ElementSpec,
pub math: ElementSpec,
pub verbatim: ElementSpec,
pub opaque_wrapper: ElementSpec,
pub paragraph: ElementSpec,
pub parbreak: ElementSpec,
pub placeholder: ElementSpec,
pub field: ElementSpec,
pub unknown_macro: ElementSpec,
pub unknown_environment: ElementSpec,
}
impl StructuralElementSpecs {
pub fn get_mut(&mut self, name: &str) -> Option<&mut ElementSpec> {
Some(match name {
TEXT => &mut self.text,
COMMENT => &mut self.comment,
CONTAINER => &mut self.container,
GROUP => &mut self.group,
MATH => &mut self.math,
VERBATIM => &mut self.verbatim,
OPAQUE_WRAPPER => &mut self.opaque_wrapper,
PARAGRAPH => &mut self.paragraph,
PARBREAK => &mut self.parbreak,
PLACEHOLDER => &mut self.placeholder,
FIELD => &mut self.field,
UNKNOWN_MACRO => &mut self.unknown_macro,
UNKNOWN_ENVIRONMENT => &mut self.unknown_environment,
_ => return None,
})
}
}
impl Default for StructuralElementSpecs {
fn default() -> StructuralElementSpecs {
let inline = || Arc::new(FixedBlockRole(BlockRole::DEFAULT));
let block = || Arc::new(FixedBlockRole(BlockRole::Block));
let element = |name: &str, docs: &str| ElementSpec::new(ElementName::from(name)).docs(docs);
StructuralElementSpecs {
text: element(TEXT, "a run of text characters").with::<dyn BlockRoleAspect>(inline()),
comment: element(COMMENT, "a source comment").with::<dyn BlockRoleAspect>(inline()),
container: element(
CONTAINER,
"a container that holds content without meaning anything itself",
),
group: element(GROUP, "a `{…}` content group").with::<dyn BlockRoleAspect>(inline()),
math: element(MATH, "a mathematics group").with::<dyn BlockRoleAspect>(Arc::new(MathGroupRole)),
verbatim: element(VERBATIM, "a verbatim group, the parent of a run of raw text")
.with::<dyn BlockRoleAspect>(inline()),
opaque_wrapper: element(
OPAQUE_WRAPPER,
"a group with no delimiters that holds parsed content kept as written",
)
.with::<dyn BlockRoleAspect>(inline()),
paragraph: element(PARAGRAPH, "a paragraph").with::<dyn BlockRoleAspect>(block()),
parbreak: element(PARBREAK, "a paragraph break (a blank line in the source)"),
placeholder: element(
PLACEHOLDER,
"a placeholder of a template (`#1`, `#{name}`), replaced by content when the \
template is expanded",
),
field: element(
FIELD,
"a field: a macro that a construct absorbs as data, such as the `\\label` written \
after a heading; only the construct that declared the field reads it",
)
.with::<dyn BlockRoleAspect>(inline()),
unknown_macro: element(
UNKNOWN_MACRO,
"a macro the parse could not attribute to any feature, reported as an error",
)
.with::<dyn BlockRoleAspect>(inline()),
unknown_environment: element(
UNKNOWN_ENVIRONMENT,
"an environment the parse could not attribute to any feature, reported as an \
error; its body is block content",
)
.with::<dyn BlockRoleAspect>(block()),
}
}
}
#[derive(Debug)]
struct MathGroupRole;
impl BlockRoleAspect for MathGroupRole {
fn block_role(&self, node: &NodeRef<'_, Flm>) -> BlockRole {
match node.math_form() {
Some(MathGroupForm::Display) => BlockRole::Displayed,
Some(MathGroupForm::Inline) | None => BlockRole::DEFAULT,
}
}
}
#[derive(Debug)]
pub struct StructuralElements {
pub text: Arc<ElementSpec>,
pub comment: Arc<ElementSpec>,
pub container: Arc<ElementSpec>,
pub group: Arc<ElementSpec>,
pub math: Arc<ElementSpec>,
pub verbatim: Arc<ElementSpec>,
pub opaque_wrapper: Arc<ElementSpec>,
pub paragraph: Arc<ElementSpec>,
pub parbreak: Arc<ElementSpec>,
pub placeholder: Arc<ElementSpec>,
pub field: Arc<ElementSpec>,
pub unknown_macro: Arc<ElementSpec>,
pub unknown_environment: Arc<ElementSpec>,
}
impl StructuralElements {
pub fn new(specs: StructuralElementSpecs) -> StructuralElements {
StructuralElements {
text: Arc::new(specs.text),
comment: Arc::new(specs.comment),
container: Arc::new(specs.container),
group: Arc::new(specs.group),
math: Arc::new(specs.math),
verbatim: Arc::new(specs.verbatim),
opaque_wrapper: Arc::new(specs.opaque_wrapper),
paragraph: Arc::new(specs.paragraph),
parbreak: Arc::new(specs.parbreak),
placeholder: Arc::new(specs.placeholder),
field: Arc::new(specs.field),
unknown_macro: Arc::new(specs.unknown_macro),
unknown_environment: Arc::new(specs.unknown_environment),
}
}
pub fn all(&self) -> [&Arc<ElementSpec>; 13] {
[
&self.text,
&self.comment,
&self.container,
&self.group,
&self.math,
&self.verbatim,
&self.opaque_wrapper,
&self.paragraph,
&self.parbreak,
&self.placeholder,
&self.field,
&self.unknown_macro,
&self.unknown_environment,
]
}
pub fn by_name(&self, name: &str) -> Option<&Arc<ElementSpec>> {
self.all().into_iter().find(|element| element.name == name)
}
}
impl Default for StructuralElements {
fn default() -> StructuralElements {
StructuralElements::new(StructuralElementSpecs::default())
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::model::aspects::BlockRoleAspect;
#[test]
fn the_default_set_names_every_shape_in_order() {
let set = StructuralElements::default();
let names: Vec<&str> = set.all().iter().map(|element| element.name.as_str()).collect();
assert_eq!(names, NAMES);
assert!(set.by_name(PARBREAK).is_some());
assert!(set.by_name("flm.nope").is_none());
}
#[test]
fn the_paragraph_is_a_block_and_the_marker_carries_no_role() {
let set = StructuralElements::default();
assert!(set.paragraph.has_aspect::<dyn BlockRoleAspect>());
assert!(set.text.has_aspect::<dyn BlockRoleAspect>());
assert!(!set.parbreak.has_aspect::<dyn BlockRoleAspect>());
}
#[test]
fn display_mathematics_is_a_displayed_item_and_inline_mathematics_inline_content() {
use crate::lang::{FlmSource, LangInputs, ParseOptions, build_language, parse_fragment};
use crate::model::aspects::block_role_of;
use crate::model::doc::FragmentLayout;
let mut inputs = LangInputs::new();
inputs.parsing.dollar_math = true;
let lang = build_language(inputs);
let source = Arc::new(FlmSource::new(r"\(a\) $b$ \[c\] $$d$$"));
let fragment = parse_fragment(&lang, source, FragmentLayout::Inline, &ParseOptions::default())
.expect("a parse");
let roles: Vec<BlockRole> = fragment
.tree()
.root()
.descendants()
.filter(|node| node.ext().element.name.as_str() == MATH)
.map(|node| block_role_of(&node))
.collect();
assert_eq!(roles, [BlockRole::DEFAULT, BlockRole::DEFAULT, BlockRole::Displayed, BlockRole::Displayed]);
}
}