//! Library for sanitizing and converting HTML strings to something that [Pango] can render. //! //! This library contains several functions to (pre)process text to [Pango Markup]. //! What to use and when depends on the type of input and the desired result. This can range from //! just escaping to converting and sanitizing. See the examples below for what is available //! based on the input type. //! //! The functions below convert strings to strings. If your input can contain several block //! elements such as headings, lists, code or quote blocks, see the [`block`] module to convert an //! input string into a list of these blocks. //! //! # Markdown/body HTML //! //! To handle more HTML, use the [`markup_html`] function. This function supports HTML body markup //! such as HTML resulting from a [Markdown]-to-HTML conversion. It tries to convert the input to //! [Pango Markup] such that rendering by Pango will make it similar like what a browser would. //! This involves adding newlines for paragraphs and lists, converting font styles, etc. //! //! ```rust //! # use html2pango::markup_html; //! let m = markup_html("this is some red text!").unwrap(); //! assert_eq!(m, "this is some red text!"); //! //! let m = markup_html("a nice link").unwrap(); //! assert_eq!(m, "a nice link"); //! //! let m = markup_html("some items: & this is \"quoted text\""); //! assert_eq!(s, "this is a <tag> & this is "quoted text""); //! ``` //! //! ## Matrix custom HTML //! //! For [Matrix], its [specification][Matrix specification] defines a custom HTML format that //! specifies the tags and attributes that can be used. Use [`matrix_html_to_markup`] to handle //! this custom HTML input so that input is sanitized before it is converted. //! //! This function is still work-in-progress! //! //! # Simple HTML //! //! By simple HTML, we mean plain text that only contains some formatting tags such as //! ``, ``, ``, etc. //! For the full list of supported tags and how they are replaced, see [`markup_from_raw`]. //! //! //! ## With sanitization //! //! If you use [`markup`], supported tags are replaced (if necessary), malformed tags are removed //! and HTML reserved characters are escaped. //! //! ```rust //! # use html2pango::markup; //! let m = markup("

this is &sanitized

"); //! assert_eq!(m, "this is &sanitized"); //! ``` //! //! Other unsupported, but valid tags are escaped. //! //! ```rust //! # use html2pango::markup; //! let m = markup("this is a tag"); //! assert_eq!(m, "this is <span>a tag</span>"); //! ``` //! //! URIs are replaced by links. //! //! ```rust //! # use html2pango::markup; //! let m = markup("go to: https://gnome.org"); //! assert_eq!(m, "go to: https://gnome.org"); //! ``` //! //! ## Without sanitization //! //! Use [`markup_from_raw`] if you have already sanitized input: //! //! ```rust //! # use html2pango::markup_from_raw; //! let m = markup_from_raw("

this is already sanitized

"); //! assert_eq!(m, "this is <unsupported>already sanitized</unsupported>"); //! ``` //! //! ## Links //! //! To just replace URIs by links, use [`markup_links`]: //! //! ```rust //! # use html2pango::markup_links; //! let m = markup_links("go to: https://gnome.org"); //! assert_eq!(m, "go to: https://gnome.org"); //! ``` //! //! [Markdown]: https://daringfireball.net/projects/markdown/ //! [Matrix]: https://matrix.org/ //! [Matrix specification]: https://spec.matrix.org/v1.1/client-server-api/#mroommessage-msgtypes //! [Pango]: https://pango.gnome.org //! [Pango Markup]: https://docs.gtk.org/Pango/pango_markup.html use html5ever::tendril::TendrilSink; use html5ever::tree_builder::TreeBuilderOpts; use html5ever::{parse_document, ParseOpts}; use markup5ever_rcdom::Node; use markup5ever_rcdom::NodeData; use markup5ever_rcdom::RcDom; use linkify::{LinkFinder, LinkKind}; use maplit::{hashmap, hashset}; use once_cell::sync::Lazy; use regex::{Captures, Regex}; use std::fmt::Write as _; use std::borrow::Cow; pub mod block; /// Sanitizes and converts simple HTML markup to Pango Markup. /// /// This will sanitize the input string using the [`ammonia`][ammonia] crate's defaults and then /// use [`markup_from_raw`] to convert the sanitized string to [Pango Markup]. /// /// # Examples /// /// ```rust /// # use html2pango::markup; /// let m = markup("this is parsed"); /// assert_eq!(m, "this is parsed"); /// /// let m = markup("this is &ssd"); /// assert_eq!(m, "this is &ssd"); /// /// let m = markup("this is parsed"); /// assert_eq!(m, "this is <span>parsed</span>"); /// /// let m = markup("with links: http://gnome.org"); /// assert_eq!(m, "with links: http://gnome.org"); /// ``` /// /// [Pango Markup]: https://docs.gtk.org/Pango/pango_markup.html /// [ammonia]: https://docs.rs/ammonia/3.1.2/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) } /// Converts simple HTML markup to Pango Markup. /// /// This will convert the input string to [Pango Markup] and replace URIs by links. /// /// Currently it support conversion of the following HTML tags: /// * `

` and `

` => `""` /// * ``, `` and ``,` ` => `` and `` /// * ``, `` and ``, `` => `` and `` /// * `` and `` => `` and `` /// * `` and `` => `` and `` /// * `` and `` => `` and `` /// * `
` => `\n` /// /// It will also convert the following characters and HTML entities: /// * `<` => `<` /// * `>` => `>` /// * ` ` => ` ` (non breaking space) /// /// # 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("this is &ssd"); /// assert_eq!(m, "this is &ssd<f;"); /// /// let m = markup_from_raw("this is parsed"); /// assert_eq!(m, "this is <span>parsed</span>"); /// /// let m = markup_from_raw("with links: http://gnome.org"); /// assert_eq!(m, "with links: http://gnome.org"); /// ``` /// /// [Pango Markup]: https://docs.gtk.org/Pango/pango_markup.html pub fn markup_from_raw(s: &str) -> String { static TAGS: Lazy = Lazy::new(|| { Regex::new(concat![ r"

|

|", r"
|", r"||", r"||", r"||", r"||", r"||", r"||", r"||", r"<|>|", r" |", // Url r"https?://", // Protocol r#"[^\s,)("]+"#, // Domain r"(\?((&)?\w+(=[\w._-]+)?)*)*", // Parameters r"(#[\w._-]+)?", // Hash ]) .unwrap() }); TAGS.replace_all(s.trim(), |captures: &Captures<'_>| { let m = captures.get(0).unwrap().as_str(); // Keep this in sync with the TAGS regex match m { "

" | "

" => Cow::Borrowed(""), "
" => Cow::Borrowed("\n"), "" | "" => Cow::Borrowed(""), "" | "" => Cow::Borrowed(""), "" => Cow::Borrowed(""), "" => Cow::Borrowed(""), "" | "" => Cow::Borrowed(""), "" | "" => Cow::Borrowed(""), "" => Cow::Borrowed(""), "" => Cow::Borrowed(""), "" => Cow::Borrowed(""), "" => Cow::Borrowed(""), "<" => Cow::Borrowed("<"), ">" => Cow::Borrowed(">"), " " => Cow::Borrowed(" "), _ => Cow::Owned(format!(r#"{}"#, m, m)), } }) .into() } /// Sanitizes and convert Matrix custom HTML markup (formatted body) to Pango Markup. /// /// First sanitizes the input string by leaving in only the HTML tags and attributes allowed /// by the [Matrix specification] and then uses [`markup_html`] to convert the sanitized string to /// [Pango Markup]. /// /// # Errors /// /// Returns an error if reading from the strings fails. /// /// # Examples /// /// TODO /// /// [Matrix specification]: https://spec.matrix.org/v1.1/client-server-api/#mroommessage-msgtypes /// [Pango Markup]: https://docs.gtk.org/Pango/pango_markup.html // 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 #[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)) } /// Escapes the HTML reserved characters. /// /// This replaces the HTML reserved characters by their respective entities in the input string. /// /// # Examples /// /// ```rust /// # use html2pango::html_escape; /// let s = html_escape("this is a "); /// assert_eq!(s, "this is a <tag>"); /// /// let s = html_escape("he says: \"this & that\""); /// assert_eq!(s, "he says: "this & that""); /// ``` pub fn html_escape(s: &str) -> String { s.to_string() .replace('&', "&") .replace('<', "<") .replace('>', ">") .replace('"', """) } /// Replaces URIs by HTML link tags. /// /// This function assumes that HTML entities in input string have been escaped (see also: /// [`html_escape`]). It will leave URIs in already existing links alone. /// /// # Examples /// /// ```rust /// # use html2pango::{html_escape, markup_links}; /// let m = markup_links("go to https://gnome.org"); /// assert_eq!(m, "go to https://gnome.org"); /// ``` pub fn markup_links(s: &str) -> String { let mut parsed = String::with_capacity(s.len()); let finder = LinkFinder::new(); let mut prepend_str: Option = 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("&") { prepend_str = Some("&".to_string()); let t = s.len() - 4; s.truncate(t); } if s.ends_with("<") { prepend_str = Some("<".to_string()); let t = s.len() - 3; s.truncate(t); } if s.ends_with(">") { prepend_str = Some(">".to_string()); let t = s.len() - 3; s.truncate(t); } if s.ends_with(""") { prepend_str = Some(""".to_string()); let t = s.len() - 5; s.truncate(t); } // This is to manage "> or "< or "& if s.ends_with(""") { prepend_str = Some(""".to_string() + &prepend_str.unwrap_or_default()); let t = s.len() - 6; s.truncate(t); } let _ = write!(parsed, "{0}", s); } _ => { if let Some(s) = prepend_str { parsed.push_str(&s); prepend_str = None; } parsed.push_str(&s); } }; } parsed } fn convert_tag(t: &str) -> Option<(&str, &str)> { let allowed = [ "a", "br", "em", "i", "p", "code", "strong", "b", "blockquote", "li", "sub", "sup", ]; if !allowed.contains(&t) { return Some(("", "")); } match t { "em" | "i" => Some(("", "")), "blockquote" => Some(("", "")), "p" => Some(("\n", "\n")), "br" => Some(("\n", "")), "code" => Some(("", "")), "strong" | "b" => Some(("", "")), "li" => Some(("", "\n")), "sub" => Some(("", "")), "sup" => Some(("", "")), _ => 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('\n'); for child in node.children.borrow().iter() { if match_tag(child, &["li", "ul", "ol"]) { let _ = write!(content, " • {}", &convert_node(child, true)); } } } "ol" => { let mut counter = 1; content.push('\n'); for child in node.children.borrow().iter() { if match_tag(child, &["li", "ul", "ol"]) { let _ = write!(content, " {}. {}", counter, &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 = String::new(); let attrs = a.borrow(); for attr in attrs.iter() { if &*attr.name.local == "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)); } let _ = write!( output, "{}", html_escape(&link), no_link_content ); } "font" => { let attrs = a.borrow(); let span_attrs = attrs .iter() .flat_map(|attr| match &*attr.name.local { "color" | "data-mx-color" => Some(("foreground", &*attr.value)), "data-mx-bg-color" => Some(("background", &*attr.value)), _ => None, }) .collect::>(); format_span(&mut output, content, span_attrs); } "span" => { let attrs = a.borrow(); let span_attrs = attrs .iter() .flat_map(|attr| match &*attr.name.local { "data-mx-color" => Some(("foreground", &*attr.value)), "data-mx-bg-color" => Some(("background", &*attr.value)), _ => None, }) .collect::>(); format_span(&mut output, content, span_attrs); } _ => { if let Some((t1, t2)) = convert_tag(&tag) { let _ = write!(output, "{}{}{}", t1, content, t2); } else { let _ = write!(output, "<{0}>{1}", tag, content); } } }; } _ => {} } output } fn format_span(output: &mut String, content: String, attrs: Vec<(&str, &str)>) { if attrs.is_empty() { output.push_str(&content); return; } let _ = write!(output, "{}", content); } /// Converts HTML body markup to Pango Markup. /// /// While [`markup`]/[`markup_from_raw`] only supports simple HTML tags such as `

`, `` and /// `
`, this function tries to convert HTML body markup so that the resulting [Pango Markup] /// will render to something that is similar to what a browser would render. /// /// # Errors /// /// Returns an error if reading from the strings fails. /// /// # Examples /// /// ```rust /// # use html2pango::markup_html; /// let m = markup_html("this is some red text!").unwrap(); /// assert_eq!(m, "this is some red text!"); /// /// let m = markup_html("a nice link").unwrap(); /// assert_eq!(m, "a nice link"); /// /// let m = markup_html("some items:

  • first
  • second
Result { 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 parsed"); assert_eq!(&m, "this is <span>parsed</span>"); let m = markup("this is &ssdf;"); assert_eq!(&m, "this is &ssdf;"); // TODO: add more tests let m = markup("

this
is &ssdf;

"); assert_eq!(&m, "this \nis &ssdf;"); let m = markup("this is &ssd"); assert_eq!(&m, "this is &ssd"); 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 &ssdf; {0}", url.replace('&', "&") ) ); 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: {0} :D", l.1.replace('&', "&") ) ); } } #[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 parsed"); assert_eq!(&m, "this is parsed"); let m = markup("this is &ssdf;"); assert_eq!(&m, "this is &ssdf;"); // TODO: add more tests let m = markup("

this
is &ssdf;

"); assert_eq!(&m, "this \nis &ssdf;"); let m = markup("this is &ssd"); assert_eq!(&m, "this is &ssd"); let m = markup("hello world"); assert_eq!(&m, "hello world"); let m = markup( "hello world", ); assert_eq!( &m, "hello world" ); let m = markup("hello http://gnome.org"); assert_eq!(&m, "hello http://gnome.org"); let m = markup( "added Label", ); assert_eq!( &m, "added Label" ); 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 &ssdf; {0}", url.replace('&', "&") ) ); 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: {0} :D", l.1.replace('&', "&") ) ); } } #[test] fn test_links() { let strings = [ ("clean string without markup", "clean string without markup"), ("clean string with a markup", "clean string with a <b>markup</b>"), ("clean string with a markup and link http://gnome.org/?p=1&q#hash", "clean string with a <b>markup</b> and link http://gnome.org/?p=1&q#hash"), ("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: https://labs.parabola.nu/projects/issue-tracker/issues?set_filter=1&tracker_id=1"), ( "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](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", ), ("links with problems: http://gnome.org/?p=1&", "links with problems: http://gnome.org/?p=1&"), ("links with problems: http://gnome.org/?p=1>", "links with problems: http://gnome.org/?p=1>"), ("links with problems: http://gnome.org/?p=1<", "links with problems: http://gnome.org/?p=1<"), ]; 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 markdown
gnome
and other link http://gnome.org", "This is a test message with markdown\ngnome\nand other link http://gnome.org"), ]; 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">"), ]; for &(s, e) in strings.iter() { let m = markup_links(&html_escape(s)); assert_eq!(&m, e); } } #[test] fn test_link_scape() { let strings = [ ("https://forums.transbian.love/?page=thread&id=69", "https://forums.transbian.love/?page=thread&id=69"), ("https://forums.transbian.love/?page=thread&id=69", "https://forums.transbian.love/?page=thread&id=69"), ("https://forums.transbian.love/?page=thread&id=69", "https://forums.transbian.love/?page=thread&id=69"), ]; for &(s, e) in strings.iter() { let m = markup_html(s).unwrap(); assert_eq!(&m, e); } } #[test] fn test_superscript() { let strings = [("Une 2e entente", "Une 2e entente")]; for &(s, e) in strings.iter() { let m = markup(s); assert_eq!(&m, e); } } #[test] fn test_subscript() { let strings = [("H2O", "H2O")]; for &(s, e) in strings.iter() { let m = markup(s); assert_eq!(&m, e); } } }