From a9c96314f744922850759c331fbafde4afb9bcfd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rapha=C3=ABl=20Ducha=C3=AEne?= Date: Thu, 17 Sep 2020 16:30:02 -0400 Subject: [PATCH] Start v3 using also pulldown-cmark-to-cmark --- Cargo.lock | 10 ++++++++ Cargo.toml | 1 + src/preprocess/index.rs | 51 +++++++++++++++++++++++------------------ 3 files changed, 40 insertions(+), 22 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 463a69ef..bdd09ff7 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -741,6 +741,7 @@ dependencies = [ "open", "pretty_assertions", "pulldown-cmark", + "pulldown-cmark-to-cmark", "regex", "select", "serde", @@ -1073,6 +1074,15 @@ dependencies = [ "unicase", ] +[[package]] +name = "pulldown-cmark-to-cmark" +version = "5.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "32accf4473121d8c0b508ca5673363703762d6cc59cf25af1df48f653346f736" +dependencies = [ + "pulldown-cmark", +] + [[package]] name = "quick-error" version = "1.2.3" diff --git a/Cargo.toml b/Cargo.toml index a943e736..388b221b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -26,6 +26,7 @@ log = "0.4" memchr = "2.0" open = "1.1" pulldown-cmark = "0.7.0" +pulldown-cmark-to-cmark = "5.0.0" regex = "1.0.0" serde = "1.0" serde_derive = "1.0" diff --git a/src/preprocess/index.rs b/src/preprocess/index.rs index db6b3d40..6eb461da 100644 --- a/src/preprocess/index.rs +++ b/src/preprocess/index.rs @@ -1,5 +1,6 @@ use core::ops::Range; use pulldown_cmark::{Event, LinkType, Parser, Tag}; +use pulldown_cmark_to_cmark::cmark; use regex::Regex; use std::iter::{FromIterator, Iterator}; use std::path::Path; @@ -60,20 +61,26 @@ fn replace_readme_in_string(content: &str) -> String { .unwrap(); } let parser = pulldown_cmark::Parser::new(content).into_offset_iter(); - let ranges_to_replace_in: Vec> = parser - .filter_map(|(event, range)| match event { - Event::Start(Tag::Link(link_type, _, _)) => match link_type { - LinkType::Reference | LinkType::Inline => Some(range), - _ => None, - }, - Event::End(Tag::Link(link_type, _, _)) => match link_type { - LinkType::Reference | LinkType::Inline => Some(range), - _ => None, - }, - _ => None, - }) - .collect(); - let ranges_to_replace_in = vec![..]; + let mut ranges_to_replace_in = vec![]; + let mut start_range: Option> = None; + let mut end_range: Option> = None; + for (event, range) in parser { + match event { + Event::Start(Tag::Link(link_type, dest, title)) => start_range = Some(range), + Event::End(Tag::Link(link_type, dest, title)) => end_range = Some(range), + _ => (), + } + match end_range { + Some(_) => { + let range = Range:: { + start: start_range.take().unwrap().start, + end: end_range.take().unwrap().end, + }; + ranges_to_replace_in.push(range); + } + None => {} + } + } let content: String = ranges_to_replace_in .iter() .map(|range| { @@ -190,14 +197,14 @@ mod tests { replace_readme_tests! { "[content]: ./bla/index.md ", - replace_readme_in_footnote_link_test_1:"[content]: ./bla/readme.md ", - replace_readme_in_footnote_link_test_2:"[content]: ./bla/ReAdme.md ", - replace_readme_in_footnote_link_test_3:"[content]: ./bla/ReaDme.md ", - replace_readme_in_footnote_link_test_4:"[content]: ./bla/README.MD ", - replace_readme_in_footnote_link_test_5:"[content]: ./bla/REadmE.md ", - replace_readme_in_footnote_link_test_6:"[content]: ./bla/ReAdme.md ", - replace_readme_in_footnote_link_test_7:"[content]: ./bla/Readme.MD ", - replace_readme_in_footnote_link_test_8:"[content]: ./bla/readme.MD ", + replace_readme_in_reference_link_test_1:"[content]: ./bla/readme.md ", + replace_readme_in_reference_link_test_2:"[content]: ./bla/ReAdme.md ", + replace_readme_in_reference_link_test_3:"[content]: ./bla/ReaDme.md ", + replace_readme_in_reference_link_test_4:"[content]: ./bla/README.MD ", + replace_readme_in_reference_link_test_5:"[content]: ./bla/REadmE.md ", + replace_readme_in_reference_link_test_6:"[content]: ./bla/ReAdme.md ", + replace_readme_in_reference_link_test_7:"[content]: ./bla/Readme.MD ", + replace_readme_in_reference_link_test_8:"[content]: ./bla/readme.MD ", } replace_readme_tests! { "[content]( ./bla/index.md)",