summaryrefslogtreecommitdiff
path: root/src/directive/img.rs
blob: 0b352fad46d9e59a18d3095e0f45fa75d9cd4246 (plain)
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
use crate::directive::{DirectiveError, DirectiveImplementation, Processed};
use crate::page::PageMeta;
use crate::site::Site;
use crate::wikitext::ParsedDirective;
use html_escape::encode_double_quoted_attribute;
use log::trace;
use std::path::Path;

#[derive(Debug, Eq, PartialEq)]
pub struct Img {
    src: String,
    link: bool,
    align: Option<String>,
    alt: Option<String>,
    class: Option<String>,
    hspace: Option<String>,
    height: Option<usize>,
    id: Option<String>,
    title: Option<String>,
    vspace: Option<String>,
    width: Option<usize>,
}

impl DirectiveImplementation for Img {
    const REQUIRED: &'static [&'static str] = &[];
    const ALLOWED: &'static [&'static str] = &[
        "align", "alt", "class", "hspace", "id", "link", "size", "title", "vspace",
    ];
    const ALLOW_ANY_UNNAMED: bool = true;

    fn from_parsed(p: &ParsedDirective) -> Self {
        let unnamed = p.unnamed_args().pop().unwrap();
        let mut img = Img::new(unnamed.into());
        let args = p.args();

        if let Some(link) = args.get("link") {
            if *link == "no" {
                img.link(false);
            }
        }

        if let Some(size) = args.get("size") {
            if let Some((w, h)) = size.split_once('x') {
                if let Ok(w) = w.parse() {
                    img.width(w);
                }
                if let Ok(h) = h.parse() {
                    img.height(h);
                }
            }
        }

        if let Some(align) = args.get("align") {
            img.align(align.to_string());
        }

        if let Some(alt) = args.get("alt") {
            img.alt(alt.to_string());
        }

        if let Some(class) = args.get("class") {
            img.class(class.to_string());
        }

        if let Some(hspace) = args.get("hspace") {
            img.hspace(hspace.to_string());
        }

        if let Some(id) = args.get("id") {
            img.id(id.to_string());
        }

        if let Some(title) = args.get("title") {
            img.title(title.to_string());
        }

        if let Some(vspace) = args.get("vspace") {
            img.vspace(vspace.to_string());
        }

        img
    }

    fn process(&self, site: &Site, meta: &mut PageMeta) -> Result<Processed, DirectiveError> {
        trace!(
            "verify image exists: {} on {}",
            self.src,
            meta.path().display()
        );
        let src = site
            .resolve(meta.path(), Path::new(&self.src))
            .map_err(|e| DirectiveError::Site(Box::new(e)))?;
        trace!("img src={:?}", src.display());

        let mut img = String::new();
        let src = Some(self.src.clone());

        if self.link {
            img.push_str("<a");
            push_attr(&mut img, "href", &src);
            img.push('>');
        }

        img.push_str("<img");
        push_attr(&mut img, "src", &src);
        if let Some(w) = self.width {
            img.push_str(&format!(" width=\"{}\"", w));
        }
        if let Some(h) = self.height {
            img.push_str(&format!(" height=\"{}\"", h));
        }
        push_attr(&mut img, "align", &self.align);
        push_attr(&mut img, "alt", &self.alt);
        push_attr(&mut img, "class", &self.class);
        push_attr(&mut img, "hspace", &self.hspace);
        push_attr(&mut img, "id", &self.id);
        push_attr(&mut img, "title", &self.title);
        push_attr(&mut img, "vspace", &self.vspace);
        img.push('>');

        if self.link {
            img.push_str("</a>");
        }

        Ok(Processed::Markdown(img))
    }
}

impl Img {
    fn new(src: String) -> Self {
        Self {
            src,
            link: true,
            align: None,
            alt: None,
            class: None,
            height: None,
            hspace: None,
            id: None,
            title: None,
            vspace: None,
            width: None,
        }
    }

    fn link(&mut self, link: bool) {
        self.link = link;
    }

    fn align(&mut self, align: String) {
        self.align = Some(align);
    }

    fn alt(&mut self, alt: String) {
        self.alt = Some(alt);
    }

    fn class(&mut self, class: String) {
        self.class = Some(class);
    }

    fn height(&mut self, h: usize) {
        self.height = Some(h);
    }

    fn hspace(&mut self, hspace: String) {
        self.hspace = Some(hspace);
    }

    fn id(&mut self, id: String) {
        self.id = Some(id);
    }

    fn title(&mut self, title: String) {
        self.title = Some(title);
    }

    fn vspace(&mut self, vspace: String) {
        self.vspace = Some(vspace);
    }

    fn width(&mut self, w: usize) {
        self.width = Some(w);
    }
}

fn push_attr(s: &mut String, name: &str, value: &Option<String>) {
    if let Some(v) = value {
        s.push_str(&format!(
            " {}=\"{}\"",
            name,
            encode_double_quoted_attribute(v)
        ));
    }
}