Function parse_hyperlinks::parser::parse::take_link
source · pub fn take_link(i: &str) -> IResult<&str, (&str, Link<'_>)>
Expand description
Consumes the input until it finds a Markdown, RestructuredText, Asciidoc or
HTML formatted inline link (Text2Dest
), reference link (Text2Label
),
link reference definition (Label2Dest
) or reference alias (Label2Label
).
The parser consumes the finding and returns
Ok((remaining_input, (skipped_input, Link)))
or some error.
Markdown
use parse_hyperlinks::parser::Link;
use parse_hyperlinks::parser::parse::take_link;
use std::borrow::Cow;
let i = r#"abc[text1][label1]abc
abc[text2](destination2 "title2")
[label1]: destination1 'title1'
"#;
let (i, r) = take_link(i).unwrap();
assert_eq!(r.0, "abc");
assert_eq!(r.1, Link::Text2Label(Cow::from("text1"), Cow::from("label1")));
let (i, r) = take_link(i).unwrap();
assert_eq!(r.0, "abc\nabc");
assert_eq!(r.1, Link::Text2Dest(Cow::from("text2"), Cow::from("destination2"), Cow::from("title2")));
let (i, r) = take_link(i).unwrap();
assert_eq!(r.0, "\n");
assert_eq!(r.1, Link::Label2Dest(Cow::from("label1"), Cow::from("destination1"), Cow::from("title1")));
reStructuredText
use parse_hyperlinks::parser::Link;
use parse_hyperlinks::parser::parse::take_link;
use std::borrow::Cow;
let i = r#"abc
abc `text0 <destination0>`_abc
abc `text1 <destination1>`__abc
abc `text2 <label2_>`_abc
abc text3__ abc
.. _label1: destination1
.. __: destination3
__ destination4
"#;
let (i, r) = take_link(i).unwrap();
assert_eq!(r.0, "abc\nabc ");
assert_eq!(r.1, Link::TextLabel2Dest(Cow::from("text0"), Cow::from("destination0"), Cow::from("")));
let (i, r) = take_link(i).unwrap();
assert_eq!(r.1, Link::Text2Dest(Cow::from("text1"), Cow::from("destination1"), Cow::from("")));
let (i, r) = take_link(i).unwrap();
assert_eq!(r.1, Link::Text2Label(Cow::from("text2"), Cow::from("label2")));
let (i, r) = take_link(i).unwrap();
assert_eq!(r.1, Link::Text2Label(Cow::from("text3"), Cow::from("_")));
let (i, r) = take_link(i).unwrap();
assert_eq!(r.1, Link::Label2Dest(Cow::from("label1"), Cow::from("destination1"), Cow::from("")));
let (i, r) = take_link(i).unwrap();
assert_eq!(r.1, Link::Label2Dest(Cow::from("_"), Cow::from("destination3"), Cow::from("")));
let (i, r) = take_link(i).unwrap();
assert_eq!(r.1, Link::Label2Dest(Cow::from("_"), Cow::from("destination4"), Cow::from("")));
Asciidoc
use parse_hyperlinks::parser::Link;
use parse_hyperlinks::parser::parse::take_link;
use std::borrow::Cow;
let i = r#"abc
abc https://destination0[text0]abc
abc link:https://destination1[text1]abc
abc{label2}[text2]abc
abc{label3}abc
:label4: https://destination4
"#;
let (i, r) = take_link(i).unwrap();
assert_eq!(r.0, "abc\nabc ");
assert_eq!(r.1, Link::Text2Dest(Cow::from("text0"), Cow::from("https://destination0"), Cow::from("")));
let (i, r) = take_link(i).unwrap();
assert_eq!(r.1, Link::Text2Dest(Cow::from("text1"), Cow::from("https://destination1"), Cow::from("")));
let (i, r) = take_link(i).unwrap();
assert_eq!(r.1, Link::Text2Label(Cow::from("text2"), Cow::from("label2")));
let (i, r) = take_link(i).unwrap();
assert_eq!(r.1, Link::Text2Label(Cow::from(""), Cow::from("label3")));
let (i, r) = take_link(i).unwrap();
assert_eq!(r.1, Link::Label2Dest(Cow::from("label4"), Cow::from("https://destination4"), Cow::from("")));
HTML
use parse_hyperlinks::parser::Link;
use parse_hyperlinks::parser::parse::take_link;
use std::borrow::Cow;
let i = r#"abc<a href="destination1" title="title1">text1</a>abc
abc<a href="destination2" title="title2">text2</a>abc
"#;
let (i, r) = take_link(i).unwrap();
assert_eq!(r.0, "abc");
assert_eq!(r.1, Link::Text2Dest(Cow::from("text1"), Cow::from("destination1"), Cow::from("title1")));
let (i, r) = take_link(i).unwrap();
assert_eq!(r.0, "abc\nabc");
assert_eq!(r.1, Link::Text2Dest(Cow::from("text2"), Cow::from("destination2"), Cow::from("title2")));