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

1/*
2 * parsing/rule/impls/block/blocks/equation_ref.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::*;
22
23pub const BLOCK_EQUATION_REF: BlockRule = BlockRule {
24    name: "block-equation-ref",
25    accepts_names: &["equation", "eref", "eqref"],
26    accepts_star: false,
27    accepts_score: false,
28    accepts_newlines: false,
29    parse_fn,
30};
31
32fn parse_fn<'r, 't>(
33    parser: &mut Parser<'r, 't>,
34    name: &'t str,
35    flag_star: bool,
36    flag_score: bool,
37    in_head: bool,
38) -> ParseResult<'r, 't, Elements<'t>> {
39    debug!("Parsing equation reference block (name '{name}', in-head {in_head})");
40    assert!(!flag_star, "Equation reference doesn't allow start flag");
41    assert!(!flag_score, "Equation reference doesn't allow score flag");
42    assert_block_name(&BLOCK_EQUATION_REF, name);
43
44    let name =
45        parser.get_head_value(
46            &BLOCK_EQUATION_REF,
47            in_head,
48            |parser, value| match value {
49                Some(name) => Ok(name.trim()),
50                None => Err(parser.make_err(ParseErrorKind::BlockMissingArguments)),
51            },
52        )?;
53
54    ok!(Element::EquationReference(cow!(name)))
55}