Browse Source

Clean up new callback design a bit

pull/1/head
Marcus Klaas de Vries 11 months ago
parent
commit
fe43163640
  1. 6
      examples/broken-link-callbacks.rs
  2. 8
      src/parse.rs

6
examples/broken-link-callbacks.rs

@ -9,12 +9,12 @@ fn main() {
// Setup callback that sets the URL and title when it encounters
// a reference to our home page.
let callback = &mut |broken_link: BrokenLink| {
if broken_link.reference.as_ref() == "my website" {
if broken_link.reference == "my website" {
println!(
"Replacing the markdown `{}` of type {:?} with a working link",
&input[broken_link.span], broken_link.link_type,
);
Some(("http;//example.com".into(), "my example website".into()))
Some(("http://example.com".into(), "my example website".into()))
} else {
None
}
@ -29,7 +29,7 @@ fn main() {
// Check that the output is what we expected.
let expected_html: &str =
"<p>Hello world, check out <a href=\"http;//example.com\" title=\"my example website\">my website</a>.</p>\n";
"<p>Hello world, check out <a href=\"http://example.com\" title=\"my example website\">my website</a>.</p>\n";
assert_eq!(expected_html, &html_output);
// Write result to stdout.

8
src/parse.rs

@ -279,9 +279,9 @@ enum TableParseMode {
}
pub struct BrokenLink<'a> {
pub span: ::std::ops::Range<usize>,
pub span: std::ops::Range<usize>,
pub link_type: LinkType,
pub reference: &'a CowStr<'a>,
pub reference: &'a str,
}
/// State for the first parsing pass.
@ -2255,7 +2255,7 @@ impl<'a> Parser<'a> {
let broken_link = BrokenLink {
span: (self.tree[tos.node].item.start)..end,
link_type: link_type,
reference: &link_label,
reference: link_label.as_ref(),
};
callback(broken_link).map(|(url, title)| {
@ -3161,7 +3161,7 @@ mod test {
fn simple_broken_link_callback() {
let test_str = "This is a link w/o def: [hello][world]";
let mut callback = |broken_link: BrokenLink| {
assert_eq!("world", broken_link.reference.as_ref());
assert_eq!("world", broken_link.reference);
assert_eq!(&test_str[broken_link.span], "[hello][world]");
let url = "YOLO".into();
let title = "SWAG".to_owned().into();

Loading…
Cancel
Save