ftml/parsing/rule/impls/block/blocks/
ifcategory.rs

1/*
2 * parsing/rule/impls/block/blocks/ifcategory.rs
3 *
4 * ftml - Library to parse Wikidot text
5 * Copyright (C) 2019-2025 Wikijump Team
6 *
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU Affero General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Affero General Public License for more details.
16 *
17 * You should have received a copy of the GNU Affero General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 */
20
21use 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    // Parse out tag conditions
47    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                    // Because a page can be in at most one category,
55                    // the required condition type is not useful here
56                    // beyond a single instance.
57                    //
58                    // Thus, we convert all required -> present,
59                    // effectively making "+" and no prefix the same thing.
60                    if condition.ctype == ElementConditionType::Required {
61                        condition.ctype = ElementConditionType::Present;
62                    }
63                });
64
65                Ok(conditions)
66            }
67        })?;
68
69    // Get body content, never with paragraphs
70    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    // Return elements based on condition
80    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}