ftml/tree/
align.rs

1/*
2 * tree/align.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 regex::Regex;
22use std::convert::TryFrom;
23
24#[derive(Serialize, Deserialize, Debug, Copy, Clone, Hash, PartialEq, Eq)]
25#[serde(rename_all = "kebab-case")]
26pub enum Alignment {
27    Left,
28    Right,
29    Center,
30    Justify,
31}
32
33impl Alignment {
34    pub fn name(self) -> &'static str {
35        match self {
36            Alignment::Left => "left",
37            Alignment::Right => "right",
38            Alignment::Center => "center",
39            Alignment::Justify => "justify",
40        }
41    }
42
43    pub fn wd_html_style(self) -> &'static str {
44        match self {
45            Alignment::Left => "text-align: left;",
46            Alignment::Right => "text-align: right;",
47            Alignment::Center => "text-align: center;",
48            Alignment::Justify => "text-align: justify;",
49        }
50    }
51
52    pub fn wj_html_class(self) -> &'static str {
53        match self {
54            Alignment::Left => "wj-align-left",
55            Alignment::Right => "wj-align-right",
56            Alignment::Center => "wj-align-center",
57            Alignment::Justify => "wj-align-justify",
58        }
59    }
60}
61
62impl TryFrom<&'_ str> for Alignment {
63    type Error = ();
64
65    fn try_from(value: &str) -> Result<Self, Self::Error> {
66        match value {
67            "<" => Ok(Alignment::Left),
68            ">" => Ok(Alignment::Right),
69            "=" => Ok(Alignment::Center),
70            "==" => Ok(Alignment::Justify),
71            _ => Err(()),
72        }
73    }
74}
75
76#[derive(Serialize, Deserialize, Debug, Copy, Clone, PartialEq, Eq)]
77#[serde(rename_all = "kebab-case")]
78pub struct FloatAlignment {
79    pub align: Alignment,
80    pub float: bool,
81}
82
83impl FloatAlignment {
84    pub fn parse(name: &str) -> Option<Self> {
85        use std::sync::LazyLock;
86
87        static IMAGE_ALIGNMENT_REGEX: LazyLock<Regex> =
88            LazyLock::new(|| Regex::new(r"^[fF]?([<=>])").unwrap());
89
90        IMAGE_ALIGNMENT_REGEX
91            .find(name)
92            .and_then(|mtch| FloatAlignment::try_from(mtch.as_str()).ok())
93    }
94
95    pub fn wd_html_class(self) -> &'static str {
96        match (self.align, self.float) {
97            (Alignment::Left, false) => "alignleft",
98            (Alignment::Right, false) => "alignright",
99            (Alignment::Center, false) => "aligncenter",
100            (Alignment::Left, true) => "floatleft",
101            (Alignment::Right, true) => "floatright",
102            (Alignment::Center, true) => "floatcenter",
103            (Alignment::Justify, _) => {
104                // When this case is reached, it means that some element
105                // permits justify alignment even though there should not
106                // be any argument settings which enable this.
107                //
108                // For instance, see FloatAlignment::try_from(&str).
109                //
110                // There is no CSS class in Wikidot for this alignment, so
111                // with both of these factors combined, we should panic.
112                panic!("Attempted to return HTML class for Wikidot justify alignment");
113            }
114        }
115    }
116
117    pub fn wj_html_class(self) -> &'static str {
118        match (self.align, self.float) {
119            (align, false) => align.wj_html_class(),
120            (Alignment::Left, true) => "wj-float-left",
121            (Alignment::Center, true) => "wj-float-center",
122            (Alignment::Right, true) => "wj-float-right",
123            (Alignment::Justify, true) => "wj-float-justify",
124        }
125    }
126}
127
128impl TryFrom<&'_ str> for FloatAlignment {
129    type Error = ();
130
131    fn try_from(value: &str) -> Result<Self, Self::Error> {
132        let (align, float) = match value {
133            "=" => (Alignment::Center, false),
134            "<" => (Alignment::Left, false),
135            ">" => (Alignment::Right, false),
136            "f<" | "F<" => (Alignment::Left, true),
137            "f>" | "F>" => (Alignment::Right, true),
138            _ => return Err(()),
139        };
140
141        Ok(FloatAlignment { align, float })
142    }
143}
144
145#[test]
146fn image_alignment() {
147    macro_rules! test {
148        ($input:expr) => {
149            test!($input => None)
150        };
151
152        ($input:expr, $align:expr, $float:expr) => {
153            test!($input => Some(FloatAlignment {
154                align: $align,
155                float: $float,
156            }))
157        };
158
159        ($input:expr => $expected:expr) => {{
160            let actual = FloatAlignment::parse($input);
161            let expected = $expected;
162
163            assert_eq!(
164                actual, expected,
165                "Actual image alignment result does not match expected",
166            );
167        }};
168    }
169
170    test!("");
171    test!("image");
172
173    test!("=image", Alignment::Center, false);
174    test!(">image", Alignment::Right, false);
175    test!("<image", Alignment::Left, false);
176    test!("f>image", Alignment::Right, true);
177    test!("f<image", Alignment::Left, true);
178
179    test!("=IMAGE", Alignment::Center, false);
180    test!(">IMAGE", Alignment::Right, false);
181    test!("<IMAGE", Alignment::Left, false);
182    test!("f>IMAGE", Alignment::Right, true);
183    test!("f<IMAGE", Alignment::Left, true);
184
185    test!("F>IMAGE", Alignment::Right, true);
186    test!("F<IMAGE", Alignment::Left, true);
187}