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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
//! Helper functions dealing with HTML conversion.

use crate::markup_language::MarkupLanguage;
use parse_hyperlinks::parser::Link;
use parse_hyperlinks_extras::iterator_html::HyperlinkInlineImage;
use parse_hyperlinks_extras::parser::parse_html::take_link;
use percent_encoding::{percent_decode_str, utf8_percent_encode, NON_ALPHANUMERIC};
use std::{
    collections::HashSet,
    path::{Path, PathBuf},
    sync::{Arc, RwLock},
};

pub const HTML_EXT: &str = ".html";

#[inline]
/// Helper function that converts relative local HTML links to absolute
/// links. If successful, we return `Some(converted_anchor_tag, target)`. If
/// `prepend_dirpath` is empty, no convertion is perfomed. Local absolute links
/// and external URLs always remain untouched. If `rewrite_ext` is true and
/// the link points to a known Tp-Note file extension, then `.html` is appended
/// to the converted link. In case of error, we return `None`.
/// Contract: Links must be local. They may have a scheme.
fn rel_link_to_abs_link(
    link: &str,
    prepend_dirpath: &Path,
    rewrite_ext: bool,
) -> Option<(String, PathBuf)> {
    //
    const ASCIISET: percent_encoding::AsciiSet = NON_ALPHANUMERIC
        .remove(b'/')
        .remove(b'.')
        .remove(b'_')
        .remove(b'-');

    let mut dirpath_link = prepend_dirpath.to_owned();

    match take_link(link) {
        Ok(("", ("", Link::Text2Dest(text, dest, title)))) => {
            // Check contract. Panic if link is not local.
            debug_assert!(!link.contains("://"));

            // Only rewrite file extensions for Tp-Note files.
            let rewrite_ext = rewrite_ext
                && !matches!(
                    MarkupLanguage::from(Path::new(&*dest)),
                    MarkupLanguage::None
                );

            // Local ones are ok. Trim URL scheme.
            let dest = dest
                .trim_start_matches("http:")
                .trim_start_matches("https:");

            let mut short_text = text.to_string();

            // Example: display `my text` for the local relative URL: `<http:my%20text.md>`.
            if text.starts_with("http:") || text.starts_with("https:") {
                // Improves pretty printing:
                let text = text
                    .trim_start_matches("http:")
                    .trim_start_matches("https:");
                let text = PathBuf::from(&*percent_decode_str(text).decode_utf8().unwrap());
                let text = text
                    .file_stem()
                    .unwrap_or_default()
                    .to_str()
                    .unwrap_or_default();
                short_text = text.to_string();
            }

            // Concat `abspath` and `relpath`.
            let relpath_link = &*percent_decode_str(dest).decode_utf8().unwrap();

            // Append ".html" if `rewrite_ext`.
            let relpath_link = if rewrite_ext {
                let mut relpath_link = relpath_link.to_string();
                relpath_link.push_str(HTML_EXT);
                PathBuf::from(relpath_link)
            } else {
                PathBuf::from(relpath_link)
            };

            // If `dirpath_link` is empty, use relpath_link instead.
            if dirpath_link.to_str().unwrap_or_default().is_empty() {
                dirpath_link = relpath_link;
            } else {
                for p in relpath_link.iter() {
                    if p == "." {
                        continue;
                    }
                    if p == ".." {
                        dirpath_link.pop();
                    } else {
                        dirpath_link.push(p);
                    }
                }
            }

            let abspath_link_encoded =
                utf8_percent_encode(dirpath_link.to_str().unwrap_or_default(), &ASCIISET)
                    .to_string();
            Some((
                format!("<a href=\"{abspath_link_encoded}\" title=\"{title}\">{short_text}</a>"),
                dirpath_link,
            ))
        }

        Ok(("", ("", Link::Image(text, dest)))) => {
            // Concat `abspath` and `relpath`.
            let relpath_link = PathBuf::from(&*percent_decode_str(&dest).decode_utf8().unwrap());

            // If `dirpath_link` is empty, use relpath_link instead.
            if dirpath_link.to_str().unwrap_or_default().is_empty() {
                dirpath_link = relpath_link;
            } else {
                for p in relpath_link.iter() {
                    if p == "." {
                        continue;
                    }
                    if p == ".." {
                        dirpath_link.pop();
                    } else {
                        dirpath_link.push(p);
                    }
                }
            }

            let abspath_link_encoded =
                utf8_percent_encode(dirpath_link.to_str().unwrap_or_default(), &ASCIISET)
                    .to_string();
            Some((
                format!("<img src=\"{abspath_link_encoded}\" alt=\"{text}\" />"),
                dirpath_link,
            ))
        }
        Ok((_, (_, _))) | Err(_) => None,
    }
}

#[inline]
/// Helper function that scans the input `html` string and converts
/// all relative local HTML links to absolute links by prepending
/// `prepend_dirpath`. If `prepend_dirpath` is empty, no convertion is
/// perfomed. Local absolute links and external URLs always remain untouched.
/// If `rewrite_ext` is true and  the link points to a known Tp-Note file
/// extension, then `.html` is appended to the converted link. The resulting
/// HTML string contains all rewritten links, whose targets are finally added
/// to the `allowed_urls`.
pub fn rewrite_links(
    html: String,
    prepend_dirpath: &Path,
    rewrite_ext: bool,
    allowed_urls: Arc<RwLock<HashSet<PathBuf>>>,
) -> String {
    let mut allowed_urls = allowed_urls
        .write()
        .expect("Can not write `allowed_urls`. RwLock is poisoned. Panic.");

    // Search for hyperlinks and inline images in the HTML rendition
    // of this note.
    let mut rest = &*html;
    let mut html_out = String::new();
    for ((skipped, consumed, remaining), link) in HyperlinkInlineImage::new(&html) {
        html_out.push_str(skipped);
        rest = remaining;

        // We skip absolute URLs.
        if link.contains("://") {
            html_out.push_str(consumed);
            continue;
        }

        if let Some((consumed_new, url)) =
            rel_link_to_abs_link(consumed, prepend_dirpath, rewrite_ext)
        {
            html_out.push_str(&consumed_new);
            allowed_urls.insert(url);
        } else {
            log::debug!("Viewer: can not parse URL: {}", consumed);
            html_out.push_str("<i>INVALID URL</i>");
        }
    }
    // Add the last `remaining`.
    html_out.push_str(rest);

    if allowed_urls.is_empty() {
        log::debug!(
            "Viewer: note file has no local hyperlinks. No additional local files are served.",
        );
    } else {
        log::debug!(
            "Viewer: referenced allowed local files: {}",
            allowed_urls
                .iter()
                .map(|p| {
                    let mut s = "\n    '".to_string();
                    s.push_str(p.as_path().to_str().unwrap_or_default());
                    s
                })
                .collect::<String>()
        );
    }

    html_out
    // The `RwLockWriteGuard` is released here.
}

#[cfg(test)]
mod tests {

    use std::{
        collections::HashSet,
        path::{Path, PathBuf},
        sync::{Arc, RwLock},
    };

    use crate::html::rel_link_to_abs_link;
    use crate::html::rewrite_links;

    #[test]
    #[should_panic(expected = "assertion failed: !link.contains(\\\"://\\\")")]
    fn test_rel_link_to_abs_link1() {
        let absdir = Path::new("/my/abs/note path/");

        // Should panic: this is not a relative path.
        let input = "<a href=\"ftp://getreu.net\">Blog</a>";
        let _ = rel_link_to_abs_link(input, absdir, false).unwrap();
    }

    #[test]
    fn test_rel_link_to_abs_link2() {
        // Check relative path to image.
        let absdir = Path::new("/my/abs/note path/");

        let input = "<img src=\"down/./down/../../t%20m%20p.jpg\" alt=\"Image\" />";
        let expected = "<img src=\"/my/abs/note%20path/t%20m%20p.jpg\" \
            alt=\"Image\" />";
        let (outhtml, outpath) = rel_link_to_abs_link(input, absdir, false).unwrap();

        assert_eq!(outhtml, expected);
        assert_eq!(outpath, PathBuf::from("/my/abs/note path/t m p.jpg"));

        // Check relative path to image, with empty prepend path.
        let input = "<img src=\"down/./../../t%20m%20p.jpg\" alt=\"Image\" />";
        let expected = "<img src=\"down/./../../t%20m%20p.jpg\" alt=\"Image\" />";
        let (outhtml, outpath) = rel_link_to_abs_link(input, Path::new(""), false).unwrap();

        assert_eq!(outhtml, expected);
        assert_eq!(outpath, PathBuf::from("down/./../../t m p.jpg"));

        // Check relative path to note file.
        let input = "<a href=\"./down/./../my%20note%201.md\">my note 1</a>";
        let expected = "<a href=\"/my/abs/note%20path/my%20note%201.md\" \
            title=\"\">my note 1</a>";
        let (outhtml, outpath) = rel_link_to_abs_link(input, absdir, false).unwrap();

        assert_eq!(outhtml, expected);
        assert_eq!(outpath, PathBuf::from("/my/abs/note path/my note 1.md"));

        // Check absolute path to note file.
        let input = "<a href=\"/dir/./down/../my%20note%201.md\">my note 1</a>";
        let expected = "<a href=\"/dir/my%20note%201.md\" \
            title=\"\">my note 1</a>";
        let (outhtml, outpath) = rel_link_to_abs_link(input, absdir, false).unwrap();

        assert_eq!(outhtml, expected);
        assert_eq!(outpath, PathBuf::from("/dir/my note 1.md"));

        // Check relative path to note file, with `./` for prepend path.
        let input = "<a href=\"./down/./../dir/my%20note%201.md\">my note 1</a>";
        let expected = "<a href=\"./dir/my%20note%201.md\" \
            title=\"\">my note 1</a>";
        let (outhtml, outpath) = rel_link_to_abs_link(input, Path::new("./"), false).unwrap();

        assert_eq!(outhtml, expected);
        assert_eq!(outpath, PathBuf::from("./dir/my note 1.md"));

        // Check `rewrite_ext=true`.
        let input = "<a href=\"./down/./../dir/my%20note%201.md\">my note 1</a>";
        let expected = "<a href=\"./dir/my%20note%201.md.html\" \
            title=\"\">my note 1</a>";
        let (outhtml, outpath) = rel_link_to_abs_link(input, Path::new("./"), true).unwrap();

        assert_eq!(outhtml, expected);
        assert_eq!(outpath, PathBuf::from("./dir/my note 1.md.html"));

        // Check relative path to note file, with empty prepend path.
        let input = "<a href=\"./down/./../dir/my%20note%201.md\">my note 1</a>";
        let expected = "<a href=\"./down/./../dir/my%20note%201.md\" title=\"\">my note 1</a>";
        let (outhtml, outpath) = rel_link_to_abs_link(input, Path::new(""), false).unwrap();

        assert_eq!(outhtml, expected);
        assert_eq!(outpath, PathBuf::from("./down/./../dir/my note 1.md"));

        // Check relative path to note file, with `/` prepend path.
        let input = "<a href=\"./down/./../dir/my%20note%201.md\">my note 1</a>";
        let expected = "<a href=\"/dir/my%20note%201.md\" title=\"\">my note 1</a>";
        let (outhtml, outpath) = rel_link_to_abs_link(input, Path::new("/"), false).unwrap();

        assert_eq!(outhtml, expected);
        assert_eq!(outpath, PathBuf::from("/dir/my note 1.md"));
    }

    #[test]
    fn test_rewrite_abs_links() {
        let allowed_urls = Arc::new(RwLock::new(HashSet::new()));
        let input = "abc<a href=\"ftp://getreu.net\">Blog</a>\
            def<a href=\"https://getreu.net\">https://getreu.net</a>\
            ghi<img src=\"t%20m%20p.jpg\" alt=\"test 1\" />\
            jkl<a href=\"down/../down/my%20note%201.md\">my note 1</a>\
            mno<a href=\"http:./down/../dir/my%20note.md\">\
            http:./down/../dir/my%20note.md</a>"
            .to_string();
        let absdir = Path::new("/my/abs/note path/");
        let expected = "abc<a href=\"ftp://getreu.net\">Blog</a>\
            def<a href=\"https://getreu.net\">https://getreu.net</a>\
            ghi<img src=\"/my/abs/note%20path/t%20m%20p.jpg\" alt=\"test 1\" />\
            jkl<a href=\"/my/abs/note%20path/down/my%20note%201.md\" title=\"\">my note 1</a>\
            mno<a href=\"/my/abs/note%20path/dir/my%20note.md\" title=\"\">my note</a>"
            .to_string();

        let output = rewrite_links(input, absdir, false, allowed_urls.clone());
        let url = allowed_urls.read().unwrap();

        assert_eq!(output, expected);
        assert!(url.contains(&PathBuf::from("/my/abs/note path/t m p.jpg")));
        assert!(url.contains(&PathBuf::from("/my/abs/note path/dir/my note.md")));
        assert!(url.contains(&PathBuf::from("/my/abs/note path/down/my note 1.md")));
    }
}