ftml/parsing/rule/impls/block/blocks/
ifcategory.rs1use super::prelude::*;
22use crate::data::PageInfo;
23use crate::parsing::{ElementCondition, ElementConditionType};
24
25pub const BLOCK_IFCATEGORY: BlockRule = BlockRule {
26 name: "block-ifcategory",
27 accepts_names: &["ifcategory"],
28 accepts_star: false,
29 accepts_score: false,
30 accepts_newlines: true,
31 parse_fn,
32};
33
34fn parse_fn<'r, 't>(
35 parser: &mut Parser<'r, 't>,
36 name: &'t str,
37 flag_star: bool,
38 flag_score: bool,
39 in_head: bool,
40) -> ParseResult<'r, 't, Elements<'t>> {
41 debug!("Parsing ifcategory block (name '{name}', in-head {in_head})");
42 assert!(!flag_star, "IfCategory doesn't allow star flag");
43 assert!(!flag_score, "IfCategory doesn't allow score flag");
44 assert_block_name(&BLOCK_IFCATEGORY, name);
45
46 let conditions =
48 parser.get_head_value(&BLOCK_IFCATEGORY, in_head, |parser, spec| match spec {
49 None => Err(parser.make_err(ParseErrorKind::BlockMissingArguments)),
50 Some(spec) => {
51 let mut conditions = ElementCondition::parse(spec);
52
53 conditions.iter_mut().for_each(|condition| {
54 if condition.ctype == ElementConditionType::Required {
61 condition.ctype = ElementConditionType::Present;
62 }
63 });
64
65 Ok(conditions)
66 }
67 })?;
68
69 let (elements, errors, paragraph_safe) =
71 parser.get_body_elements(&BLOCK_IFCATEGORY, false)?.into();
72
73 trace!(
74 "IfCategory conditions parsed (conditions length {}, elements length {})",
75 conditions.len(),
76 elements.len(),
77 );
78
79 let elements = if check_ifcategory(parser.page_info(), &conditions) {
81 trace!("Conditions passed, including elements");
82
83 Elements::Multiple(elements)
84 } else {
85 trace!("Conditions failed, excluding elements");
86
87 Elements::None
88 };
89
90 ok!(paragraph_safe; elements, errors)
91}
92
93pub fn check_ifcategory(info: &PageInfo, conditions: &[ElementCondition]) -> bool {
94 let category = match &info.category {
95 Some(category) => category,
96 None => "_default",
97 };
98
99 trace!("Checking ifcategory (category '{category}')");
100 ElementCondition::check(conditions, &[cow!(category)])
101}