summaryrefslogtreecommitdiff
path: root/src/elements/line_element.rs
blob: cc47b4bee5295dc4afe418b52338acbaad66cdb2 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
pub enum LineElement {
    Normal(String),
    Bold(String),
    Italic(String),
    Monospace(String),
    Math(String),
    InternalLink(String),
    ExternalLink(ExternalLink),
}

impl LineElement {
    /// Return only the character content, with none of the styling information.
    pub fn as_plain_text(&self) -> &str {
        match self {
            LineElement::Normal(text) => text,
            LineElement::Bold(text) => text,
            LineElement::Italic(text) => text,
            LineElement::Monospace(text) => text,
            LineElement::Math(text) => text,
            LineElement::InternalLink(label) => label,
            LineElement::ExternalLink(ExternalLink { label, ..}) => label,
        }
    }
}

pub struct ExternalLink {
    pub label: String,
    pub target: String,
}

impl std::fmt::Display for LineElement {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> {
        let string = match self {
            LineElement::Normal(text) => format!("{text}"),
            LineElement::Bold(text) => format!("**{text}**"),
            LineElement::Italic(text) => format!("_{text}_"),
            LineElement::Monospace(text) => format!("`{text}`"),
            LineElement::Math(text) => format!("${text}$"),
            LineElement::InternalLink(text) => format!("[[{text}]]"),
            LineElement::ExternalLink(ExternalLink { label, target }) => {
                format!("[{label}]({target})") }
        };
        f.write_str(&string)
    }
}

impl std::fmt::Debug for LineElement {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> {
        let string = match self {
            LineElement::Normal(text) => format!("Normal ('{text}')"),
            LineElement::Bold(text) => format!("Bold ('{text}')"),
            LineElement::Italic(text) => format!("Italic ('{text}')"),
            LineElement::Monospace(text) => format!("Monospace ('{text}')"),
            LineElement::Math(text) => format!("Math ('{text}')"),
            LineElement::InternalLink(text) => format!("InternalLink ('{text}')"),
            LineElement::ExternalLink(ExternalLink { label, target }) => {
                format!("ExternalLink (label:'{label}',  target:'{target}')") }
        };
        f.write_str(&string)
    }
}