//! Module for converting HTML markup to blocks with Pango Markup formatted content. //! //! The functions in this module turn the HTML input to a list of block elements (see //! [`HtmlBlock`]) rather then an output string. This can be useful if the blocks are rendered //! separately, for example if you have some text followed by a code block. The former may be //! rendered using a different widget than the latter. //! //! The main function of this module is [`markup_html`]. //! It yields HTML blocks for the following tags: //! * `
` => [`HtmlBlock::Code`] //! * `
` => [`HtmlBlock::Quote`] //! //! It handles the following tags as text, yielding an [`HtmlBlock::Text`] with the contents: //! * `` => `` with only the `href` attribute set //! * `` and `` => `` //! * `` and `` => removed //! * `
` => `\n` //! * `` => `` //! * `` and `` => `` //! * `` and `` => `` //! * `` and `` => `` //! * `` => contents followed by `\n` //! * `` => `` //! //! Finally, it removes all other tags and replaces URIs by HTML link tags. and merges //! subsequent text HTML blocks into a single one. //! //! # Examples //! //! Converting HTML markup with a heading and two paragraphs: //! //! ```rust //! # use html2pango::block::*; //! let blks = //! markup_html("
Heading
This is paragraph 1.
Paragraph 2
").unwrap(); //! assert_eq!(blks[0], HtmlBlock::Heading(1, String::from("Heading"))); //! assert_eq!(blks[1], HtmlBlock::Text(String::from("This is paragraph 1.\n\nParagraph 2"))); //! ``` //! //! Converting HTML markup with an unordered list and some formatting: //! //! ```rust //! # use html2pango::block::*; //! let blks = //! markup_html("").unwrap(); //! assert_eq!( //! blks[0], //! HtmlBlock::UList(vec![ //! String::from("This is bold"), //! String::from("Some code"), //! ]) //! ); //! ``` //! //! Converting HTML markup links and URIs that are converted into links: //! //! ```rust //! # use html2pango::block::*; //! let blks = markup_html("a link and \ //! another: https://gnome.org").unwrap(); //! assert_eq!( //! blks[0], //! HtmlBlock::Text( //! String::from( //! "a link and \ //! another: https://gnome.org" //! ) //! ) //! ); //! ``` //! //! For more examples, see [`markup_html`]. use std::cell::RefCell; use std::rc::Rc; use super::html_escape; use super::markup_links; use html5ever::tendril::TendrilSink; use html5ever::tree_builder::Attribute; use html5ever::tree_builder::TreeBuilderOpts; use html5ever::{parse_document, ParseOpts}; use markup5ever_rcdom::Node; use markup5ever_rcdom::NodeData; use markup5ever_rcdom::RcDom; /// Supported HTML block elements. /// /// Each block element contains a textual representation of the contained inline HTML elements in /// [Pango Markup]. The exception is [`HtmlBlock::Quote`], which contains the list of block /// elements that are contained in the quote. /// /// [Pango Markup]: https://docs.gtk.org/Pango/pango_markup.html #[derive(Debug, Clone, PartialEq)] pub enum HtmlBlock { /// Just some text. Text(String), /// Heading with its level and text. Heading(u32, String), /// Unordered list of text. UList(Vec
- This is bold
Some code), /// Ordered list of text. OList(Vec "; let blocks = markup_html(text); assert!(blocks.is_ok()); let blocks = blocks.unwrap(); assert_eq!(blocks.len(), 1); if let HtmlBlock::Text(h) = &blocks[0] { assert_eq!(h, "GitLab labels: 1. Enhancement and 4. Newcomers"); } } #[test] fn html_non_block_tags() { let text = "How about some underlined), /// Code block with its contained (preformatted) text. Code(String), /// (Block)quote with its contained block elements. Quote(Rc >), /// Vertical separator (i.e. horizontal rule) Separator, } /// Converts HTML body markup into a list of HTML blocks while ignoring some tags. /// /// The provided tags are ignored while parsing as if they are not there, i.e. the parser just /// descends into them and leaves them out of the result. /// /// For more details on what is actually converted, see the [module documentation](crate::block). /// /// # Errors /// /// Returns an error if reading from the strings fails or if parsing somehow fails. /// /// # Examples /// /// ```rust /// # use html2pango::block::*; /// let blks = /// markup_html_ignore_tags(" this is nice ", &["body"]).unwrap(); /// assert_eq!(blks, vec![HtmlBlock::Text(String::from("this is nice"))]); /// ``` /// /// For more examples about the markup conversion, see [`markup_html`]. pub fn markup_html_ignore_tags(s: &str, tags: &[&str]) -> Result, 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 mut html = document.children.borrow().clone(); // TODO: When writing `...` in markdown, the first children is of type // Comment { contents: "raw HTML omitted" } // This should be handled more gracefully html.retain(|x| !matches!(x.data, NodeData::Comment { .. })); if let Some(h) = &html.get(0) { if let Some(node) = &h.children.borrow().get(1) { let markup = convert_node(node, tags); return Ok(trim_text_blocks(markup)); } } Err(anyhow::anyhow!(format!("Could not parse {:?}", html))) } /// Converts HTML body markup into a list of HTML blocks. /// /// Calls [`markup_html_ignore_tags`] to do the actual work instructing it to ignore the `` /// and `mx-reply` (for Matrix) tags. /// /// For more details on what is actually converted, see the [module documentation](crate::block). /// /// # Errors /// /// Returns an error if reading from the string fails or if parsing fails. /// /// # Examples /// /// Some HTML markup of a heading and an unordered list: /// /// ```rust /// # use html2pango::block::*; /// let blks = /// markup_html(" Heading 1
- First
- Second
").unwrap(); /// assert_eq!(blks[0], HtmlBlock::Heading(1, String::from("Heading 1"))); /// assert_eq!(blks[1], HtmlBlock::UList(vec![String::from("First"), String::from("Second")])); /// ``` /// /// Pango Markup is applied, paragraphs are handled but merged: /// /// ```rust /// # use html2pango::block::*; /// let blks = /// markup_html("This is nice!
Second paragraph.
").unwrap(); /// assert_eq!( /// blks[0], /// HtmlBlock::Text(String::from("This is nice!\n\nSecond paragraph.")) /// ); /// ``` /// /// URIs are replaced by links: /// /// ```rust /// # use html2pango::block::*; /// let blks = /// markup_html("Go to https://gnome.org").unwrap(); /// assert_eq!( /// blks[0], /// HtmlBlock::Text(String::from("Go to https://gnome.org")) /// ); /// ``` /// pub fn markup_html(s: &str) -> Result, anyhow::Error> { let tags = vec!["body", "mx-reply"]; markup_html_ignore_tags(s, &tags) } fn to_pango(t: &str) -> Option<(&'static str, &'static str)> { let allowed = [ "u", "del", "s", "em", "i", "strong", "b", "code", "a", "br", "sub", "sup", ]; if !allowed.contains(&t) { return None; } match t { "a" => Some(("", "")), "br" => Some(("", "\n")), "em" | "i" => Some(("", "")), "strong" | "b" => Some(("", "")), "del" | "s" => Some((" ", "")), "u" => Some(("", "")), "code" => Some(("", "")), "sub" => Some(("", "")), "sup" => Some(("", "")), _ => None, } } fn parse_link(node: &Node, attrs: &RefCell>) -> String { let mut link = "".to_string(); for attr in attrs.borrow().iter() { let s = attr.name.local.to_string(); if &s[..] == "href" { link = attr.value.to_string(); } } format!( "{1}", html_escape(&link), get_text_content(node) ) } fn get_text_content(node: &Node) -> String { let text = node .children .borrow() .iter() .map(|node| match node.data { NodeData::Text { contents: ref c } => { markup_links(&replace_html_whitespace(&html_escape(&c.borrow()))) } NodeData::Element { name: ref n, attrs: ref a, .. } => { let inside = get_text_content(node); if &n.local == "a" { return parse_link(node, a); } match to_pango(&n.local) { Some((t1, t2)) => format!("{}{}{}", t1, inside, t2), None => inside, } } _ => get_text_content(node), }) .collect:: >() .concat(); collapse_spaces(&text) } fn get_plain_text_content(node: &Node) -> String { node.children .borrow() .iter() .map(|node| match node.data { NodeData::Text { contents: ref c } => c.borrow().to_string(), NodeData::Element { name: ref n, attrs: ref _a, .. } => { let inside = get_plain_text_content(node); match to_pango(&n.local) { Some((t1, t2)) => { // We don't want tt tags inside codeblocks which // already have . if t1 != "" { format!("{}{}{}", t1, inside, t2) } else { inside } } None => inside, } } _ => get_plain_text_content(node), }) .collect:: >() .concat() } fn get_li_elements(node: &Node) -> Vec { node.children .borrow() .iter() .filter_map(|node| match node.data { NodeData::Element { name: ref n, .. } if &n.local == "li" => { Some(get_text_content(node)) } _ => None, }) .collect:: >() } fn join_text_blocks(mut blocks: Vec ) -> Vec { use HtmlBlock::Text; blocks.drain(..).fold(Vec:: ::new(), |mut v, b| { let last = v.last(); if let (Text(c), Some(Text(a))) = (&b, last) { let t = join_collapse_spaces(a, c); v.pop(); v.push(Text(t)); } else { v.push(b); } v }) } fn join_collapse_spaces(s: &str, extra: &str) -> String { if s.ends_with(' ') && (extra.starts_with('\n') || extra.starts_with(' ')) { s.trim_end().to_string() + extra } else if s.ends_with('\n') && extra.starts_with(' ') { s.to_string() + extra.trim_start() } else { s.to_string() + extra } } fn collapse_spaces(s: &str) -> String { let mut collapsed_s = String::with_capacity(s.len()); let mut iter = s.chars().peekable(); while let Some(c) = iter.next() { match (c, iter.peek()) { (' ', Some('\n')) | (' ', Some(' ')) => continue, ('\n', Some(' ')) => { iter.next(); // Drop the following space while iter.peek() == Some(&' ') { iter.next(); } } _ => {} } collapsed_s.push(c); } collapsed_s } fn replace_html_whitespace(s: &str) -> String { let mut result = String::new(); let mut last = 0; for (index, matched) in s.match_indices(|c: char| !c.is_ascii_whitespace()) { if last != index { result.push(' '); } result.push_str(matched); last = index + matched.len(); } if last < s.len() { result.push(' '); } result } fn html_unescape(s: &str) -> String { s.to_string() .replace("&", "&") .replace("<", "<") .replace(">", ">") .replace(""", "\"") } fn trim_text_blocks(mut blocks: Vec ) -> Vec { blocks.drain(..).fold(vec![], |mut v, b| { if let HtmlBlock::Text(c) = &b { if !c.trim().is_empty() { v.push(HtmlBlock::Text(c.trim().to_string())); } } else { v.push(b); } v }) } fn convert_node(node: &Node, tags_to_ignore: &[&str]) -> Vec { let mut output = vec![]; match node.data { NodeData::Text { contents: ref c } => { let s = markup_links(&replace_html_whitespace(&html_escape(&c.borrow()))); output.push(HtmlBlock::Text(s)); } NodeData::Element { name: ref n, attrs: ref a, .. } => { match &n.local as &str { // tags to ignore tag if tags_to_ignore.contains(&tag) => { for child in node.children.borrow().iter() { for block in convert_node(child, tags_to_ignore) { output.push(block); } } } h if ["h1", "h2", "h3", "h4", "h5", "h6"].contains(&h) => { let n: u32 = h[1..].parse().unwrap_or(6); let text = get_text_content(node); output.push(HtmlBlock::Heading(n, text)); } "a" => { let link = parse_link(node, a); output.push(HtmlBlock::Text(link)); } "pre" => { let text = get_plain_text_content(node); output.push(HtmlBlock::Code(html_unescape(text.trim()))); } "ul" => { let elements = get_li_elements(node); output.push(HtmlBlock::UList(elements)); } "ol" => { let elements = get_li_elements(node); output.push(HtmlBlock::OList(elements)); } "blockquote" => { let mut content = vec![]; for child in node.children.borrow().iter() { for block in convert_node(child, tags_to_ignore) { content.push(block); } } content = trim_text_blocks(join_text_blocks(content)); output.push(HtmlBlock::Quote(Rc::new(content))); } "p" => { let content = &get_text_content(node); output.push(HtmlBlock::Text(format!("\n{}\n", content.trim()))); } "hr" => { output.push(HtmlBlock::Separator); } "br" => { output.push(HtmlBlock::Text("\n".to_string())); } "font" | "span" => { 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:: >(); let content = get_text_content(node); let mut span = String::new(); crate::format_span(&mut span, content, span_attrs); output.push(HtmlBlock::Text(span)); } tag => { let content = get_text_content(node); let block = match to_pango(tag) { Some((t1, t2)) => format!("{}{}{}", t1, content, t2), None => content, }; output.push(HtmlBlock::Text(block)); } }; } _ => {} } join_text_blocks(output) } #[cfg(test)] mod test { use super::*; use pretty_assertions::assert_eq; #[test] fn test_html_blocks() { let text = " heading 1 italic
\nheading 2
\nSome text with markup and other and more text and some
\ninline code, that's all. And maybe some links http://google.es or GNOME.\nBlock text\n\n
\n- This is a list
\n- second element
\n\n
\n- another list
\n- that's all
\n
"; let blocks = markup_html(text); assert!(blocks.is_ok()); let blocks = blocks.unwrap(); assert_eq!(blocks.len(), 7); assert_eq!( blocks[0], HtmlBlock::Heading(1, "heading 1 italic".to_string()) ); assert_eq!(blocks[1], HtmlBlock::Heading(2, "heading 2".to_string())); assert!(matches!(&blocks[2], HtmlBlock::Text(..))); assert!(matches!(&blocks[3], HtmlBlock::Code(..))); assert!(matches!(&blocks[4], HtmlBlock::UList(..))); assert!(matches!(&blocks[5], HtmlBlock::OList(..))); assert!(matches!(&blocks[6], HtmlBlock::Separator)); } #[test] fn test_html_blocks_quote() { let text = "heading 1 bold
heading 2
Some text with markup and other and more things ~~strike~~ and more text and some
inline code, that's all. And maybe some links http://google.es or <a href="http://gnome.org">GNOME</a>, GNOME.`Block text `
- This is a list
- second element
quote :D
"; let blocks = markup_html(text); assert!(blocks.is_ok()); let blocks = blocks.unwrap(); assert_eq!(blocks.len(), 2); if let HtmlBlock::Quote(blks) = &blocks[0] { assert_eq!(blks.len(), 5); } } #[test] fn test_separator() { let text = "aaa\n
foobar
baz
"; let blocks = markup_html(text); assert!(blocks.is_ok()); let blocks = blocks.unwrap(); assert_eq!(blocks.len(), 6); let expected_blocks = [ HtmlBlock::Text(String::from("aaa")), HtmlBlock::Separator, HtmlBlock::Text(String::from("foobar")), HtmlBlock::Separator, HtmlBlock::Text(String::from("baz")), HtmlBlock::Separator, ]; for (block, expected) in blocks.iter().zip(expected_blocks.iter()) { assert_eq!(block, expected); } } #[test] fn test_mxreply() { let text = " In reply to @afranke:matrix.org
Gergely Polonkai: we have https://gitlab.gnome.org/GNOME/fractal/issues/467 and https://gitlab.gnome.org/GNOME/fractal/issues/347 open, does your issue fit any of these two?#467 might be it, let me test a bit
"; let blocks = markup_html(text); assert!(blocks.is_ok()); let blocks = blocks.unwrap(); assert_eq!(blocks.len(), 2); if let HtmlBlock::Text(t) = &blocks[0] { assert_eq!(t, "In reply to @afranke:matrix.orgGergely Polonkai: we have https://gitlab.gnome.org/GNOME/fractal/issues/467 and https://gitlab.gnome.org/GNOME/fractal/issues/347 open, does your issue fit any of these two?\n#467 might be it, let me test a bit"); } } #[test] fn test_html_lists() { let text = "
"; let blocks = markup_html(text); assert!(blocks.is_ok()); let blocks = blocks.unwrap(); assert_eq!(blocks.len(), 1); if let HtmlBlock::UList(t) = &blocks[0] { assert_eq!(t.len(), 2); assert_eq!(t[0], "item 1"); assert_eq!(t[1], "item 2"); } } #[test] fn test_html_paragraphs() { let text = "
- item 1
- item 2
text
text2
text3
- li
text4
text5
"; let blocks = markup_html(text); assert!(blocks.is_ok()); let blocks = blocks.unwrap(); assert!(!blocks.is_empty()); assert_eq!(blocks.len(), 3); if let HtmlBlock::Text(t) = &blocks[0] { assert_eq!(t, "text\n\ntext2\n\ntext3"); } if let HtmlBlock::Text(t) = &blocks[2] { assert_eq!(t, "text4\n\ntext5"); } } #[test] fn newline_in_blockquote() { let text = "html was a mistake"; let matrix_text = "
Indeed\n\n"; let blocks = markup_html(text); assert!(blocks.is_ok()); let blocks = blocks.unwrap(); assert_eq!(blocks.len(), 1); if let HtmlBlock::Quote(blk) = &blocks[0] { assert_eq!(blk.len(), 1); if let HtmlBlock::Text(h) = &blk[0] { assert_eq!(h, "html was a mistake\nIndeed"); } } let blocks = markup_html(matrix_text); assert!(blocks.is_ok()); let blocks = blocks.unwrap(); assert_eq!(blocks.len(), 1); if let HtmlBlock::Quote(blk) = &blocks[0] { assert_eq!(blk.len(), 1); if let HtmlBlock::Text(h) = &blk[0] { assert_eq!(h, "a\nb\n\nc\nd"); } } } #[test] fn html_blocks_quote_multiple() { let text = "a
\n
\nbc
\nd\n\nSome
\ntext
\n\n\nNo
\nu
\n"; let blocks = markup_html(text); assert!(blocks.is_ok()); let blocks = blocks.unwrap(); assert_eq!(blocks.len(), 4); if let HtmlBlock::Quote(blk) = &blocks[0] { assert_eq!(blk.len(), 1); if let HtmlBlock::Text(h) = &blk[0] { assert_eq!(h, "Some"); } } if let HtmlBlock::Text(t) = &blocks[1] { assert_eq!(t, "text"); } if let HtmlBlock::Quote(blk) = &blocks[2] { assert_eq!(blk.len(), 1); if let HtmlBlock::Text(h) = &blk[0] { assert_eq!(h, "No"); } } if let HtmlBlock::Text(t) = &blocks[3] { assert_eq!(t, "u"); } } #[test] fn html_url_and_text() { let text = "GNOMEtext"; let blocks = markup_html(text); assert!(blocks.is_ok()); let blocks = blocks.unwrap(); assert_eq!(blocks.len(), 1); if let HtmlBlock::Text(h) = &blocks[0] { assert_eq!( h, "GNOMEtext" ); } } #[test] fn html_font_span() { let text = "GitLab labels: 1. Enhancement\n and \ 4. Newcomersdeletedorstrikethroughtext? \ Maybe emphasized, italic, strong or bold text? \ Possibly a newline
with some morecode? \ Finally some sub or super par lines!"; let blocks = markup_html(text); assert!(blocks.is_ok()); let blocks = blocks.unwrap(); assert_eq!(blocks.len(), 1); let HtmlBlock::Text(h) = &blocks[0] else { panic!("No text block found") }; assert_eq!( h, "How about some underlineddeletedorstrikethroughtext? \ Maybe emphasized, italic, strong or bold text? \ Possibly a newline\nwith some more code? \ Finally some sub or super par lines!" ); } #[test] fn codeblock_empty_whitespace() { let text = ""; let blocks = markup_html(text); assert!(blocks.is_ok()); let blocks = blocks.unwrap(); assert_eq!(blocks.len(), 1); if let HtmlBlock::Code(t) = &blocks[0] { assert_eq!(t, "Block text `a` i\n\nspace?") } } #[test] fn escape_amp() { let text = "text:Block text `a` i space?&was not scaped as&"; let blocks = markup_html(text); assert!(blocks.is_ok()); let blocks = blocks.unwrap(); assert_eq!(blocks.len(), 1); if let HtmlBlock::Text(t) = &blocks[0] { assert_eq!( t, "text: & was not scaped as &" ); } } #[test] fn dont_escape_codeblocks() { let text = "&&"; let blocks = markup_html(text); assert!(blocks.is_ok()); let blocks = blocks.unwrap(); assert_eq!(blocks.len(), 2); if let HtmlBlock::Code(t) = &blocks[0] { assert_eq!(t, "&") } if let HtmlBlock::Code(t) = &blocks[1] { assert_eq!(t, "&") } } #[test] fn newlines_in_text() { let text = " 1 2 34 5
6
"; let text_matrix = "a
\n
\nbc
\nd"; let text_matrix_gitlab = "[GNOME/fractal] user\n added Fractal-next \n to\n\n issue #194: Replies support"; let blocks = markup_html(text); assert!(blocks.is_ok()); let blocks = blocks.unwrap(); assert_eq!(blocks.len(), 1); if let HtmlBlock::Text(t) = &blocks[0] { assert_eq!(t, "1 2 3\n4 5\n\n6") } let blocks = markup_html(text_matrix); assert!(blocks.is_ok()); let blocks = blocks.unwrap(); assert_eq!(blocks.len(), 1); if let HtmlBlock::Text(t) = &blocks[0] { assert_eq!(t, "a\nb\n\nc\nd") } let blocks = markup_html(text_matrix_gitlab); assert!(blocks.is_ok()); let blocks = blocks.unwrap(); assert_eq!(blocks.len(), 1); if let HtmlBlock::Text(t) = &blocks[0] { assert_eq!(t, "[GNOME/fractal] user added \u{a0}Fractal-next\u{a0} to issue #194: Replies support"); } let text_h1 = "foo \n bar
"; let blocks = markup_html(text_h1).unwrap(); if let HtmlBlock::Heading(1, t) = &blocks[0] { assert_eq!(t, "foo bar"); } let text_ul = ""; let blocks = markup_html(text_ul).unwrap(); if let HtmlBlock::UList(ts) = &blocks[0] { assert_eq!(ts[0], "first element"); assert_eq!(ts[1], "second\nelement"); } let text_ol = "
- first \n element
- second
element"; let blocks = markup_html(text_ol).unwrap(); if let HtmlBlock::OList(ts) = &blocks[0] { assert_eq!(ts[0], "first element"); assert_eq!(ts[1], "second\nelement"); } let text_bq = "
- first \n element
- second
element"; let blocks = markup_html(text_bq).unwrap(); if let HtmlBlock::Quote(contents) = &blocks[0] { assert_eq!(contents[0], HtmlBlock::Text(String::from("Foo:"))); assert_eq!( contents[1], HtmlBlock::UList(vec![String::from("First item")]) ); } } #[test] fn links_inside_code() { let text = "Foo:\n
- First \n item
\n "; let blocks = markup_html(text); assert!(blocks.is_ok()); let blocks = blocks.unwrap(); assert_eq!(blocks.len(), 1); if let HtmlBlock::Code(t) = &blocks[0] { assert_eq!(t, "https://gitlab.gnome.org/World/Fractal/"); } } #[test] fn ci_links() { let text = " [World/Fractal] "; let blocks = markup_html(text); assert!(blocks.is_ok()); let blocks = blocks.unwrap(); assert_eq!(blocks.len(), 1); if let HtmlBlock::Text(s) = &blocks[0] { assert_eq!(s, "[World/Fractal]"); } } #[test] fn newline_after_quotes() { let text = "https://gitlab.gnome.org/World/Fractal/F"; let target = "In reply to @okias:matrix.org\nText"; let blocks = markup_html(text); assert!(blocks.is_ok()); let blocks = blocks.unwrap(); assert_eq!(blocks.len(), 2); if let HtmlBlock::Quote(blk) = &blocks[0] { assert_eq!(blk.len(), 1); if let HtmlBlock::Text(h) = &blk[0] { assert_eq!(h, target); } } if let HtmlBlock::Text(t) = &blocks[1] { assert_eq!(t, "F"); } } } In reply to @okias:matrix.org
Text