Function parse_hyperlinks::renderer::text_links2html
source · pub fn text_links2html(input: &str) -> String
Expand description
Source code viewer with link renderer
Text to HTML renderer that prints the input text “as it is”, but
renders links with markup. Links are clickable and only their
link text is shown (the part enclosed with <a>
and </a>
).
Markdown
use parse_hyperlinks::renderer::text_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
"#;
let expected = "\
<pre>abc<a href=\"dest0\" title=\"title0\">text0</a>abc
abc<a href=\"dest1\" title=\"title1\">text1</a>abc
abc<a href=\"dest2\" title=\"title2\">text2</a>abc
<a href=\"dest3\" title=\"title3\">[text3]: dest3 "title3"</a>
<a href=\"dest1\" title=\"title1\">[label1]: dest1 "title1"</a>
abc<a href=\"dest3\" title=\"title3\">text3</a>abc
</pre>";
let res = text_links2html(i);
assert_eq!(res, expected);
Rendered text
This is how the rendered text looks like in the browser:
abctext0abc abctext1abc abctext2abc [text3]: dest3 "title3" [label1]: dest1 "title1" abctext3abc
reStructuredText
use parse_hyperlinks::renderer::text_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 = "\
<pre>abc <a href=\"dest1\" title=\"\">text1</a>abc
abc <a href=\"dest2\" title=\"\">text2</a> abc
abc <a href=\"dest3\" title=\"\">text3</a> abc
abc text_label4_ abc
abc <a href=\"dest5\" title=\"\">text5</a> abc
<a href=\"dest1\" title=\"\">.. _label1: dest1</a>
<a href=\"dest2\" title=\"\">.. _text2: dest2</a>
<a href=\"dest3\" title=\"\">.. __: dest3</a>
<a href=\"dest5\" title=\"\">__ dest5</a>
</pre>\
";
let res = text_links2html(i);
assert_eq!(res, expected);
Rendered text
This is how the rendered text looks like in the browser:
abc text1abc abc text2 abc abc text3 abc abc text_label4_ abc abc text5 abc .. _label1: dest1 .. _text2: dest2 .. __: dest3 __ dest5
Asciidoc
use parse_hyperlinks::renderer::text_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 = "\
<pre>abc <a href=\"https://dest0\" title=\"\">text0</a>abc
abc <a href=\"https://dest1\" title=\"\">text1</a>abc
abc<a href=\"https://dest2\" title=\"\">text2</a>abc
abc<a href=\"https://dest3\" title=\"\">https://dest3</a>abc
<a href=\"https://dest2\" title=\"\">:label2: https://dest2</a>
<a href=\"https://dest3\" title=\"\">:label3: https://dest3</a>
</pre>";
let res = text_links2html(i);
assert_eq!(res, expected);
Rendered text
This is how the rendered text looks like in the browser:
abc text0abc abc text1abc abctext2abc abchttps://dest3abc :label2: https://dest2 :label3: https://dest3
Wikitext
use parse_hyperlinks::renderer::text_links2html;
use std::borrow::Cow;
let i = r#"abc[https://dest0 text0]abc
"#;
let expected = "\
<pre>abc<a href=\"https://dest0\" title=\"\">text0</a>abc
</pre>";
let res = text_links2html(i);
assert_eq!(res, expected);
Rendered text
This is how the rendered text looks like in the browser:
abctext0abc
HTML
HTML inline links are sanitized and passed through.
use parse_hyperlinks::renderer::text_links2html;
use std::borrow::Cow;
let i = r#"abc<a href="dest1" title="title1">text1</a>abc"#;
let expected = "<pre>\
abc<a href=\"dest1\" title=\"title1\">text1</a>abc\
</pre>";
let res = text_links2html(i);
assert_eq!(res, expected);
Rendered text
This is how the rendered text looks like in the browser:
abctext1abc