Skip to main content

ftml/parsing/rule/impls/
strikethrough.rs

1/*
2 * parsing/rule/impls/strikethrough.rs
3 *
4 * ftml - Library to parse Wikidot text
5 * Copyright (C) 2019-2026 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
21//! Rules for strikethrough.
22//!
23//! Wikidot had implemented strikethrough using --text--
24//! however we also added the more conventional way ~~text~~
25
26use super::prelude::*;
27
28pub const RULE_STRIKETHROUGH_DASH: Rule = Rule {
29    name: "strikethrough-dash",
30    position: LineRequirement::Any,
31    try_consume_fn: dash,
32};
33
34pub const RULE_STRIKETHROUGH_TILDE: Rule = Rule {
35    name: "strikethrough-tilde",
36    position: LineRequirement::Any,
37    try_consume_fn: tilde,
38};
39
40fn dash<'r, 't>(parser: &mut Parser<'r, 't>) -> ParseResult<'r, 't, Elements<'t>> {
41    trace!("Trying to create a double dash strikethrough");
42    try_consume_strikethrough(parser, RULE_STRIKETHROUGH_DASH, Token::DoubleDash)
43}
44
45fn tilde<'r, 't>(parser: &mut Parser<'r, 't>) -> ParseResult<'r, 't, Elements<'t>> {
46    trace!("Trying to create a double tilde strikethrough");
47    try_consume_strikethrough(parser, RULE_STRIKETHROUGH_TILDE, Token::DoubleTilde)
48}
49
50/// Build a strikethrough with the given rule and token.
51fn try_consume_strikethrough<'r, 't>(
52    parser: &mut Parser<'r, 't>,
53    rule: Rule,
54    token: Token,
55) -> ParseResult<'r, 't, Elements<'t>> {
56    debug!("Trying to create a strikethrough (token {})", token.name());
57    assert_step(parser, token)?;
58    collect_container(
59        parser,
60        rule,
61        ContainerType::Strikethrough,
62        &[ParseCondition::current(token)],
63        &[
64            ParseCondition::current(Token::ParagraphBreak),
65            ParseCondition::token_pair(token, Token::Whitespace),
66            ParseCondition::token_pair(Token::Whitespace, token),
67        ],
68        None,
69    )
70}