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

1/*
2 * parsing/rule/impls/block/blocks/collapsible.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::parsing::{ParseError, ParseErrorKind};
23
24pub const BLOCK_COLLAPSIBLE: BlockRule = BlockRule {
25    name: "block-collapsible",
26    accepts_names: &["collapsible"],
27    accepts_star: false,
28    accepts_score: false,
29    accepts_newlines: true,
30    parse_fn,
31};
32
33fn parse_fn<'r, 't>(
34    parser: &mut Parser<'r, 't>,
35    name: &'t str,
36    flag_star: bool,
37    flag_score: bool,
38    in_head: bool,
39) -> ParseResult<'r, 't, Elements<'t>> {
40    debug!("Parsing collapsible block (in-head {in_head})");
41    assert!(!flag_star, "Collapsible doesn't allow star flag");
42    assert!(!flag_score, "Collapsible doesn't allow score flag");
43    assert_block_name(&BLOCK_COLLAPSIBLE, name);
44
45    let mut arguments = parser.get_head_map(&BLOCK_COLLAPSIBLE, in_head)?;
46
47    // Get display arguments
48    let show_text = arguments.get("show");
49    let hide_text = arguments.get("hide");
50
51    // Get folding arguments
52    //
53    // We invert this first argument since "folded=no" means "start_open=yes"
54    let start_open = !arguments.get_bool(parser, "folded")?.unwrap_or(true);
55    let (show_top, show_bottom) = match arguments.get("hideLocation") {
56        Some(value) => parse_hide_location(&value, parser)?,
57        None => (true, false),
58    };
59
60    // Get body content, with paragraphs.
61    // Discard paragraph_safe, since collapsibles never are.
62    let (elements, errors, _) =
63        parser.get_body_elements(&BLOCK_COLLAPSIBLE, true)?.into();
64
65    // Build element and return
66    let element = Element::Collapsible {
67        elements,
68        attributes: arguments.to_attribute_map(parser.settings()),
69        start_open,
70        show_text,
71        hide_text,
72        show_top,
73        show_bottom,
74    };
75
76    ok!(element, errors)
77}
78
79fn parse_hide_location(s: &str, parser: &Parser) -> Result<(bool, bool), ParseError> {
80    const NAMES: [(&str, (bool, bool)); 5] = [
81        ("top", (true, false)),
82        ("bottom", (false, true)),
83        ("both", (true, true)),
84        ("neither", (false, false)),
85        ("none", (false, false)),
86    ];
87
88    let s = s.trim();
89    for &(name, value) in &NAMES {
90        if name.eq_ignore_ascii_case(s) {
91            return Ok(value);
92        }
93    }
94
95    warn!("Unknown hideLocation argument '{s}'");
96    Err(parser.make_err(ParseErrorKind::BlockMalformedArguments))
97}