pub fn links2html(input: &str) -> String
Expand description

Text to HTML renderer that prints only links with markup as a list, one per line. Links are clickable and only their link text is shown (the part enclosed with <a> and </a>).

Markdown

use parse_hyperlinks::renderer::links2html;
use std::borrow::Cow;

let i = r#"abc[text0](dest0 "title0")abc
abc[text1][label1]abc
abc[text2](dest2 "title2")abc
[text3]: dest3 "title3"
[label1]: dest1 "title1"
abc[text3]abc
abc<foo@dest4>abc
abc![alt5](dest5)abc
abc[![alt6](src6)](dest6)abc
"#;

let expected = "\
<a href=\"dest0\" title=\"title0\">text0</a><br>
<a href=\"dest1\" title=\"title1\">text1</a><br>
<a href=\"dest2\" title=\"title2\">text2</a><br>
<a href=\"dest3\" title=\"title3\">text3</a><br>
<a href=\"mailto:foo@dest4\" title=\"\">foo@dest4</a><br>
<a href=\"dest5\">[alt5]</a><br>
<a href=\"dest6\" title=\"\">[alt6]</a><br>
";
let res = links2html(i);
assert_eq!(res, expected);

Rendered text

This is how the rendered text looks like in the browser:

text0
text1
text2
text3
foo@dest4
[alt5]
[alt6]

reStructuredText

use parse_hyperlinks::renderer::links2html;
use std::borrow::Cow;

let i = r#"
abc `text1 <label1_>`_abc
abc text2_ abc
abc text3__ abc
abc text_label4_ abc
abc text5__ abc
.. _label1: dest1
.. _text2: dest2
.. __: dest3
__ dest5
"#;

let expected = "\
<a href=\"dest1\" title=\"\">text1</a><br>
<a href=\"dest2\" title=\"\">text2</a><br>
<a href=\"dest3\" title=\"\">text3</a><br>
<a href=\"dest5\" title=\"\">text5</a><br>
";

let res = links2html(i);
assert_eq!(res, expected);

Rendered text

This is how the rendered text looks like in the browser:

text1
text2
text3
text5

Asciidoc

use parse_hyperlinks::renderer::links2html;
use std::borrow::Cow;

let i = r#"abc https://dest0[text0]abc
abc link:https://dest1[text1]abc
abc{label2}[text2]abc
abc{label3}abc
:label2: https://dest2
:label3: https://dest3
"#;

let expected = "\
<a href=\"https://dest0\" title=\"\">text0</a><br>
<a href=\"https://dest1\" title=\"\">text1</a><br>
<a href=\"https://dest2\" title=\"\">text2</a><br>
<a href=\"https://dest3\" title=\"\">https://dest3</a><br>
";

let res = links2html(i);
assert_eq!(res, expected);

Rendered text

This is how the rendered text looks like in the browser:

text0
text1
text2
https://dest3

Wikitext

use parse_hyperlinks::renderer::links2html;
use std::borrow::Cow;

let i = r#"abc[https://dest0 text0]abc
"#;

let expected = "\
<a href=\"https://dest0\" title=\"\">text0</a><br>
";

let res = links2html(i);
assert_eq!(res, expected);

Rendered text

This is how the rendered text looks like in the browser:

text0

HTML

HTML inline links are sanitized and listed, one per line.

use parse_hyperlinks::renderer::links2html;
use std::borrow::Cow;

let i = "abc<a href=\"dest1\" title=\"title1\">text1</a>abc
abc<img src=\"dest5\" alt=\"alt5\">abc
abc<a href=\"dest6\" title=\"\"><img alt=\"alt6\" src=\"src6\"></a>abc
";

let expected = "\
<a href=\"dest1\" title=\"title1\">text1</a><br>
<a href=\"dest5\">[alt5]</a><br>
<a href=\"dest6\" title=\"\">[alt6]</a><br>
";

let res = links2html(i);
assert_eq!(res, expected);

Rendered text

This is how the rendered text looks like in the browser:

text1
text2