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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
use markup5ever_rcdom::Node;
use markup5ever_rcdom::NodeData;
use markup5ever_rcdom::RcDom;
use html5ever::tendril::TendrilSink;
use html5ever::tree_builder::TreeBuilderOpts;
use html5ever::{parse_document, ParseOpts};

use lazy_static::lazy_static;
use linkify::{LinkFinder, LinkKind};
use maplit::{hashmap, hashset};

use regex::Regex;

const AMP: &str = "(&)";
const DOMAIN: &str = "[^\\s,)(\"]+";
const HASH: &str = "(#[\\w._-]+)?";

pub mod block;

/// Sanitize the input using [`ammonia`][ammonia]'s defaults,
/// Convert the input `&str` to pango format and parse
/// URLS to show as `pango` markup links(removes rel attributes).
///
/// If you want to parse a pre-sanitized input, you can use `markup_from_raw`.
///
/// Currently it support conversion of the following `html` tags:
/// * `<p>` and `</p>` => `""`
/// * `<i>`, `<em>` and `</i>`,` </em>` => `<i>` and `</i>``
/// * `<b>`, `<strong>` and `</b>`, `</strong>` => `<b>` and `</b>`
/// * `<br>` => `\n`
/// * rest `<` => `&lt`
/// * rest `>` => `&gt`
/// * `&nbsp;` => ` ` (non breaking space)
///
/// # Examples
///
/// ```rust
/// # use html2pango::markup;
///
/// let m = markup("this is parsed");
/// assert_eq!(&m, "this is parsed");
///
/// let m = markup("<b>this <i>is &ssd<f;</i></b>");
/// assert_eq!(&m, "<b>this <i>is &amp;ssd</i></b>");
///
/// let m = markup("this is <span>parsed</span>");
/// assert_eq!(&m, "this is &lt;span&gt;parsed&lt;/span&gt;");
///
/// let m = markup("with links: http://gnome.org");
/// assert_eq!(&m, "with links: <a href=\"http://gnome.org\">http://gnome.org</a>");
/// ```
///
/// [ammonia]: https://docs.rs/ammonia/1.1.0/ammonia/fn.clean.html
pub fn markup(s: &str) -> String {
    let sanitized_html = ammonia::Builder::new().link_rel(None).clean(s).to_string();
    markup_from_raw(&sanitized_html)
}

/// Same as `markup` but without sanizing the input.
///
/// # Examples
///
/// ```
/// # use html2pango::markup_from_raw;
///
/// let m = markup_from_raw("this is parsed");
/// assert_eq!(&m, "this is parsed");
///
/// let m = markup_from_raw("<b>this <i>is &ssd<f;</i></b>");
/// assert_eq!(&m, "<b>this <i>is &ssd&lt;f;</i></b>");
///
/// let m = markup_from_raw("this is <span>parsed</span>");
/// assert_eq!(&m, "this is &lt;span&gt;parsed&lt;/span&gt;");
///
/// let m = markup_from_raw("with links: http://gnome.org");
/// assert_eq!(&m, "with links: <a href=\"http://gnome.org\">http://gnome.org</a>");
/// ```
pub fn markup_from_raw(s: &str) -> String {
    lazy_static! {
        static ref PARAM: String = format!("({amp}?\\w+(=[\\w._-]+)?)", amp = AMP);
        static ref PARAMS: String = format!("(\\?{param}*)*", param = *PARAM);
        static ref REURL: String = format!(
            "(https?://{domain}{params}{hash})",
            domain = DOMAIN,
            params = *PARAMS,
            hash = HASH
        );
        static ref RE: Regex = Regex::new(&REURL).unwrap();
        static ref MATCH: Regex = Regex::new(
            r"<p>|</p>|<br>|<b>|</b>|<strong>|</strong>|<code>|</code>|<i>|</i>|<em>|</em>|<|>|&nbsp;"
        ).unwrap();
    }

    let s = s.trim();

    let mut previous_end = 0;
    let mut foo = Vec::with_capacity(s.as_bytes().len());
    for mat in MATCH.find_iter(s) {
        foo.push(&s[previous_end..mat.start()]);
        // Kepp in sync with the regex query or it will panic
        foo.push(match mat.as_str() {
            "<p>" | "</p>" => "",
            "<br>" => "\n",
            "<b>" | "<strong>" => "<b>",
            "</b>" | "</strong>" => "</b>",
            "<code>" => "<tt>",
            "</code>" => "</tt>",
            "<i>" | "<em>" => "<i>",
            "</i>" | "</em>" => "</i>",
            "<" => "&lt;",
            ">" => "&gt;",
            "&nbsp;" => " ",
            _ => unreachable!(),
        });
        previous_end = mat.end();
    }

    foo.push(&s[previous_end..]);
    let mut out = String::with_capacity(foo.iter().map(|s| s.len()).sum());
    out.extend(foo.into_iter());

    String::from(RE.replace_all(&out.trim(), "<a href=\"$0\">$0</a>"))
}

// WIP: only allow the html subset that matrix uses.
pub fn matrix_html_to_markup(s: &str) -> String {
    // https://github.com/matrix-org/matrix-react-sdk/blob/4bf5e44b2043bbe95faa66943878acad23dfb823/src/HtmlUtils.js#L178-L184
    #[cfg_attr(rustfmt, rustfmt_skip)]
    let allowed_tags = hashset![
        "font", // custom to matrix for IRC-style font coloring
        "del", // for markdown
        "h1", "h2", "h3", "h4", "h5", "h6", "blockquote", "p", "a", "ul", "ol", "sup", "sub",
        "nl", "li", "b", "i", "u", "strong", "em", "strike", "code", "hr", "br", "div",
        "table", "thead", "caption", "tbody", "tr", "th", "td", "pre", "span", "img",
    ];

    // https://github.com/matrix-org/matrix-react-sdk/blob/4bf5e44b2043bbe95faa66943878acad23dfb823/src/HtmlUtils.js#L185-L193
    let allowed_attributes = hashmap![
        // custom ones first:
        "font" => hashset!["color", "data-mx-bg-color", "data-mx-color", "style"],
        "span" => hashset!["data-mx-bg-color", "data-mx-color", "style"],
        // remote target: custom to matrix
        "a" => hashset!["href", "name", "target", "rel"],
        "img" => hashset!["src", "width", "height", "alt", "title"],
        "ol" => hashset!["start"],
        // We don't actually allow all classes, TODO: we should filter them afterwards
        "code" => hashset!["class"],
    ];

    // https://github.com/matrix-org/matrix-react-sdk/blob/4bf5e44b2043bbe95faa66943878acad23dfb823/src/HtmlUtils.js#L48
    let allowed_urls = hashset!["http", "https", "ftp", "mailto", "magnet"];

    let sanitized_html = ammonia::Builder::new()
        .url_schemes(allowed_urls)
        .tags(allowed_tags)
        .tag_attributes(allowed_attributes)
        .link_rel(None)
        .clean(s)
        .to_string();

    markup_html(&sanitized_html)
        .map(|x| x.trim_matches('\n').to_string())
        .unwrap_or_else(|_| markup_from_raw(&sanitized_html))
}

/// Escape the html entities of `s`
pub fn html_escape(s: &str) -> String {
    s.to_string()
        .replace('&', "&amp;")
        .replace('<', "&lt;")
        .replace('>', "&gt;")
        .replace('"', "&quot;")
}

/// Converts links to <a href="LINK">LINK</a>
pub fn markup_links(s: &str) -> String {
    let mut parsed = String::with_capacity(s.len());
    let finder = LinkFinder::new();
    let mut prepend_str: Option<String> = None;

    for span in finder.spans(s) {
        let mut s = span.as_str().to_string();
        match span.kind() {
            Some(&LinkKind::Url) => {
                if s.ends_with("&amp") {
                    prepend_str = Some("&amp".to_string());
                    let t = s.len() - 4;
                    s.truncate(t);
                }
                if s.ends_with("&lt") {
                    prepend_str = Some("&lt".to_string());
                    let t = s.len() - 3;
                    s.truncate(t);
                }
                if s.ends_with("&gt") {
                    prepend_str = Some("&gt".to_string());
                    let t = s.len() - 3;
                    s.truncate(t);
                }
                if s.ends_with("&quot") {
                    prepend_str = Some("&quot".to_string());
                    let t = s.len() - 5;
                    s.truncate(t);
                }
                // This is to manage "> or "< or "&
                if s.ends_with("&quot;") {
                    prepend_str = Some("&quot;".to_string() + &prepend_str.unwrap_or_default());
                    let t = s.len() - 6;
                    s.truncate(t);
                }
                parsed.push_str(&format!("<a href=\"{0}\">{0}</a>", s))
            }
            _ => {
                if let Some(s) = prepend_str {
                    parsed.push_str(&s);
                    prepend_str = None;
                }
                parsed.push_str(&s);
            }
        };
    }

    parsed
}

fn convert_tag<'a>(t: &'a str) -> Option<(&'a str, &'a str)> {
    let allowed = [
        "a",
        "br",
        "em",
        "i",
        "p",
        "code",
        "strong",
        "b",
        "blockquote",
        "li",
    ];

    if !allowed.contains(&t) {
        return Some(("", ""));
    }

    match t {
        "em" | "i" => Some(("<i>", "</i>")),
        "blockquote" => Some(("<i>", "</i>")),
        "p" => Some(("\n", "\n")),
        "br" => Some(("\n", "")),
        "code" => Some(("<tt>", "</tt>")),
        "strong" | "b" => Some(("<b>", "</b>")),
        "li" => Some(("", "\n")),
        _ => None,
    }
}

fn match_tag(node: &Node, tags: &[&str]) -> bool {
    match node.data {
        NodeData::Element { name: ref n, .. } => {
            let node_tag = n.local.to_string();
            tags.contains(&node_tag.as_str())
        }
        _ => false,
    }
}

fn convert_node(node: &Node, autolinks: bool) -> String {
    let mut output = String::new();

    match node.data {
        NodeData::Text { contents: ref c } => {
            let escaped = &html_escape(&c.borrow().replace("\n", ""));
            if autolinks {
                output.push_str(&markup_links(escaped));
            } else {
                output.push_str(&escaped);
            }
        }
        NodeData::Element {
            name: ref n,
            attrs: ref a,
            ..
        } => {
            let mut content = String::new();
            let tag = n.local.to_string();

            match &tag[..] {
                "ul" => {
                    content.push_str("\n");
                    for child in node.children.borrow().iter() {
                        if match_tag(child, &["li", "ul", "ol"]) {
                            content.push_str(&format!(" • {}", &convert_node(child, true)));
                        }
                    }
                }
                "ol" => {
                    let mut counter = 1;
                    content.push_str("\n");
                    for child in node.children.borrow().iter() {
                        if match_tag(child, &["li", "ul", "ol"]) {
                            content.push_str(&format!(
                                " {}. {}",
                                counter.to_string(),
                                &convert_node(child, true)
                            ));
                            counter += 1;
                        }
                    }
                }
                _ => {
                    for child in node.children.borrow().iter() {
                        content.push_str(&convert_node(child, true));
                    }
                }
            }

            match &tag[..] {
                "body" => {
                    output.push_str(&content);
                }
                "a" => {
                    let mut link = "".to_string();
                    for attr in a.borrow().iter() {
                        let s = attr.name.local.to_string();
                        match &s[..] {
                            "href" => {
                                link = attr.value.to_string();
                            }
                            _ => {}
                        }
                    }

                    let mut no_link_content = String::new();
                    for child in node.children.borrow().iter() {
                        no_link_content.push_str(&convert_node(child, false));
                    }

                    output.push_str(&format!("<a href=\"{}\">{}</a>", html_escape(&link), no_link_content));
                }
                "font" => {
                    let mut color = "".to_string();
                    for attr in a.borrow().iter() {
                        let s = attr.name.local.to_string();
                        match &s[..] {
                            "color" => {
                                color = attr.value.to_string();
                            }
                            _ => {}
                        }
                    }
                    output.push_str(&format!(
                        "<span foreground=\"{}\">{1}</span>",
                        color, content
                    ));
                }
                _ => {
                    if let Some((t1, t2)) = convert_tag(&tag) {
                        output.push_str(&format!("{}{}{}", t1, content, t2));
                    } else {
                        output.push_str(&format!("<{0}>{1}</{0}>", tag, content));
                    }
                }
            };
        }
        _ => {}
    }

    output
}

pub fn markup_html(s: &str) -> Result<String, anyhow::Error> {
    let opts = ParseOpts {
        tree_builder: TreeBuilderOpts {
            drop_doctype: true,
            ..Default::default()
        },
        ..Default::default()
    };
    let dom = parse_document(RcDom::default(), opts)
        .from_utf8()
        .read_from(&mut s.as_bytes())?;

    let document = &dom.document;
    let html = &document.children.borrow()[0];
    let body = &html.children.borrow()[1];

    Ok(convert_node(body, true))
}

#[cfg(test)]
mod test {
    use super::*;
    use pretty_assertions::assert_eq;

    #[test]
    fn test_markup() {
        let m = markup("this is parsed");
        assert_eq!(&m, "this is parsed");

        let m = markup("this is <span>parsed</span>");
        assert_eq!(&m, "this is &lt;span&gt;parsed&lt;/span&gt;");

        let m = markup("this is &ssdf;");
        assert_eq!(&m, "this is &amp;ssdf;");

        // TODO: add more tests
        let m = markup("<p>this <br>is &ssdf;</p>");
        assert_eq!(&m, "this \nis &amp;ssdf;");

        let m = markup("<b>this <i>is &ssd<f;</i></b>");
        assert_eq!(&m, "<b>this <i>is &amp;ssd</i></b>");

        let url = "http://url.com/test?foo1&foo2=test&foo3#hashing";
        let m = markup(&format!("this is &ssdf; {}", url));
        assert_eq!(
            &m,
            &format!(
                "this is &amp;ssdf; <a href=\"{0}\">{0}</a>",
                url.replace('&', "&amp;")
            )
        );

        for l in &[
            ("with links: http://gnome.org :D", "http://gnome.org"),
            (
                "with links: http://url.com/test.html&stuff :D",
                "http://url.com/test.html&stuff",
            ),
        ] {
            let m = markup(l.0);
            assert_eq!(
                &m,
                &format!(
                    "with links: <a href=\"{0}\">{0}</a> :D",
                    l.1.replace('&', "&amp;")
                )
            );
        }
    }

    #[test]
    // FIXME: Write specific tests instead of copying the above
    fn test_matrix() {
        let markup = matrix_html_to_markup;
        let m = markup("this is parsed");
        assert_eq!(&m, "this is parsed");

        let m = markup("this is <span>parsed</span>");
        assert_eq!(&m, "this is parsed");

        let m = markup("this is &ssdf;");
        assert_eq!(&m, "this is &amp;ssdf;");

        // TODO: add more tests
        let m = markup("<p>this <br>is &ssdf;</p>");
        assert_eq!(&m, "this \nis &amp;ssdf;");

        let m = markup("<b>this <i>is &ssd<f;</i></b>");
        assert_eq!(&m, "<b>this <i>is &amp;ssd</i></b>");

        let m = markup("hello <font color=\"#112233\">world</font>");
        assert_eq!(&m, "hello <span foreground=\"#112233\">world</span>");

        let m = markup("hello <em><font color=\"#112233\">http://gnome.org</font></em>");
        assert_eq!(&m, "hello <i><span foreground=\"#112233\"><a href=\"http://gnome.org\">http://gnome.org</a></span></i>");

        let url = "http://url.com/test?foo1&foo2=test&foo3#hashing";
        let m = markup(&format!("this is &ssdf; {}", url));
        assert_eq!(
            &m,
            &format!(
                "this is &amp;ssdf; <a href=\"{0}\">{0}</a>",
                url.replace('&', "&amp;")
            )
        );

        for l in &[
            ("with links: http://gnome.org :D", "http://gnome.org"),
            (
                "with links: http://url.com/test.html&stuff :D",
                "http://url.com/test.html&stuff",
            ),
        ] {
            let m = markup(l.0);
            assert_eq!(
                &m,
                &format!(
                    "with links: <a href=\"{0}\">{0}</a> :D",
                    l.1.replace('&', "&amp;")
                )
            );
        }
    }

    #[test]
    fn test_links() {
        let strings = [
            ("clean string without markup",
             "clean string without markup"),

            ("clean string with a <b>markup</b>",
             "clean string with a &lt;b&gt;markup&lt;/b&gt;"),

            ("clean string with a <b>markup</b> and link http://gnome.org/?p=1&q#hash",
             "clean string with a &lt;b&gt;markup&lt;/b&gt; and link <a href=\"http://gnome.org/?p=1&amp;q#hash\">http://gnome.org/?p=1&amp;q#hash</a>"),

            ("report-bug is: please report bugs with parabola packages on the packaging bug tracker at: https://labs.parabola.nu/projects/issue-tracker/issues?set_filter=1&tracker_id=1",
             "report-bug is: please report bugs with parabola packages on the packaging bug tracker at: <a href=\"https://labs.parabola.nu/projects/issue-tracker/issues?set_filter=1&amp;tracker_id=1\">https://labs.parabola.nu/projects/issue-tracker/issues?set_filter=1&amp;tracker_id=1</a>"),

             (
             "bill-auger, isacdaavid: there are two major issues I see with gnome-software. The first issue is that flathub, the largest repo for flatpaks, has nonfree software. If flathub isn't included by default, I think this is fine. The second is archlinux-appstream-data. The [PKGBUILD](https://git.archlinux.org/svntogit/packages.git/tree/trunk/PKGBUILD?h=packages/archlinux-appstream-data) does not use appstream-generator at all. However, it does require grabbing files from sources.archlinux.org",
             "bill-auger, isacdaavid: there are two major issues I see with gnome-software. The first issue is that flathub, the largest repo for flatpaks, has nonfree software. If flathub isn't included by default, I think this is fine. The second is archlinux-appstream-data. The [PKGBUILD](<a href=\"https://git.archlinux.org/svntogit/packages.git/tree/trunk/PKGBUILD?h=packages/archlinux-appstream-data\">https://git.archlinux.org/svntogit/packages.git/tree/trunk/PKGBUILD?h=packages/archlinux-appstream-data</a>) does not use appstream-generator at all. However, it does require grabbing files from sources.archlinux.org",
             ),

             ("links with problems: http://gnome.org/?p=1&",
              "links with problems: <a href=\"http://gnome.org/?p=1\">http://gnome.org/?p=1</a>&amp;"),
             ("links with problems: http://gnome.org/?p=1>",
              "links with problems: <a href=\"http://gnome.org/?p=1\">http://gnome.org/?p=1</a>&gt;"),
             ("links with problems: http://gnome.org/?p=1<",
              "links with problems: <a href=\"http://gnome.org/?p=1\">http://gnome.org/?p=1</a>&lt;"),
        ];

        for &(s, e) in strings.iter() {
            let m = markup_links(&html_escape(s));
            assert_eq!(&m, e);
        }
    }

    #[test]
    fn test_markup_links() {
        let strings = [
            ("This is a test message with <em>markdown</em><br /><a href=\"http://gnome.org\">gnome</a><br />and other link http://gnome.org",
             "This is a test message with <i>markdown</i>\n<a href=\"http://gnome.org\">gnome</a>\nand other link <a href=\"http://gnome.org\">http://gnome.org</a>"),
        ];

        for &(s, e) in strings.iter() {
            let m = markup_html(s).unwrap();
            assert_eq!(&m, e);
        }
    }

    #[test]
    fn test_ending_quote_link() {
        let strings = [
            ("<boxes:gnome-boxes xmlns:boxes=\"https://wiki.gnome.org/Apps/Boxes\">",
             "&lt;boxes:gnome-boxes xmlns:boxes=&quot;<a href=\"https://wiki.gnome.org/Apps/Boxes\">https://wiki.gnome.org/Apps/Boxes</a>&quot;&gt;"),
        ];

        for &(s, e) in strings.iter() {
            let m = markup_links(&html_escape(s));
            assert_eq!(&m, e);
        }
    }

    #[test]
    fn test_link_scape() {
        let strings = [
            ("<a href=\"https://forums.transbian.love/?page=thread&id=69\">https://forums.transbian.love/?page=thread&id=69</a>",
             "<a href=\"https://forums.transbian.love/?page=thread&amp;id=69\">https://forums.transbian.love/?page=thread&amp;id=69</a>"),
            ("<a href=\"https://forums.transbian.love/?page=thread&id=69\">https://forums.transbian.love/?page=thread&amp;id=69</a>",
             "<a href=\"https://forums.transbian.love/?page=thread&amp;id=69\">https://forums.transbian.love/?page=thread&amp;id=69</a>"),
            ("https://forums.transbian.love/?page=thread&id=69",
             "<a href=\"https://forums.transbian.love/?page=thread&amp;id=69\">https://forums.transbian.love/?page=thread&amp;id=69</a>"),
        ];

        for &(s, e) in strings.iter() {
            let m = markup_html(s).unwrap();
            assert_eq!(&m, e);
        }
    }
}