Module html2pango::block
source · Expand description
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:
<h1>
,<h2>
,<h3>
,<h4>
,<h5>
,<h6>
=>HtmlBlock::Heading
<pre>
=>HtmlBlock::Code
<ul>
=>HtmlBlock::UList
<ol>
=>HtmlBlock::OList
<blockquote>
=>HtmlBlock::Quote
It handles the following tags as text, yielding an HtmlBlock::Text
with the contents:
<a>
=><a>
with only thehref
attribute set<b>
and<strong>
=><b>
<body>
and<mx-reply>
=> removed<br>
=>\n
<code>
=><tt>
<del>
and<s>
=><s>
<em>
and<i>
=><i>
<span>
and<font>
=><span>
<p>
=> contents followed by\n
<u>
=><u>
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:
let blks =
markup_html("<h1>Heading</h1><p>This is paragraph 1.</p><p>Paragraph 2</p>").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:
let blks =
markup_html("<ul><li>This is <b>bold</b></li><li><code>Some code</code></ul>").unwrap();
assert_eq!(
blks[0],
HtmlBlock::UList(vec![
String::from("This is <b>bold</b>"),
String::from("<tt>Some code</tt>"),
])
);
Converting HTML markup links and URIs that are converted into links:
let blks = markup_html("a <a href=\"https://gnome.org\" rel=\"nofollow\">link</a> and \
another: https://gnome.org").unwrap();
assert_eq!(
blks[0],
HtmlBlock::Text(
String::from(
"a <a href=\"https://gnome.org\" title=\"https://gnome.org\">link</a> and \
another: <a href=\"https://gnome.org\">https://gnome.org</a>"
)
)
);
For more examples, see markup_html
.
Enums
- Supported HTML block elements.
Functions
- Converts HTML body markup into a list of HTML blocks.
- Converts HTML body markup into a list of HTML blocks while ignoring some tags.