Start v3 using also pulldown-cmark-to-cmark

This commit is contained in:
Raphaël Duchaîne 2020-09-17 16:30:02 -04:00
parent c592e66d8e
commit a9c96314f7
3 changed files with 40 additions and 22 deletions

10
Cargo.lock generated
View File

@ -741,6 +741,7 @@ dependencies = [
"open", "open",
"pretty_assertions", "pretty_assertions",
"pulldown-cmark", "pulldown-cmark",
"pulldown-cmark-to-cmark",
"regex", "regex",
"select", "select",
"serde", "serde",
@ -1073,6 +1074,15 @@ dependencies = [
"unicase", "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]] [[package]]
name = "quick-error" name = "quick-error"
version = "1.2.3" version = "1.2.3"

View File

@ -26,6 +26,7 @@ log = "0.4"
memchr = "2.0" memchr = "2.0"
open = "1.1" open = "1.1"
pulldown-cmark = "0.7.0" pulldown-cmark = "0.7.0"
pulldown-cmark-to-cmark = "5.0.0"
regex = "1.0.0" regex = "1.0.0"
serde = "1.0" serde = "1.0"
serde_derive = "1.0" serde_derive = "1.0"

View File

@ -1,5 +1,6 @@
use core::ops::Range; use core::ops::Range;
use pulldown_cmark::{Event, LinkType, Parser, Tag}; use pulldown_cmark::{Event, LinkType, Parser, Tag};
use pulldown_cmark_to_cmark::cmark;
use regex::Regex; use regex::Regex;
use std::iter::{FromIterator, Iterator}; use std::iter::{FromIterator, Iterator};
use std::path::Path; use std::path::Path;
@ -60,20 +61,26 @@ fn replace_readme_in_string(content: &str) -> String {
.unwrap(); .unwrap();
} }
let parser = pulldown_cmark::Parser::new(content).into_offset_iter(); let parser = pulldown_cmark::Parser::new(content).into_offset_iter();
let ranges_to_replace_in: Vec<Range<usize>> = parser let mut ranges_to_replace_in = vec![];
.filter_map(|(event, range)| match event { let mut start_range: Option<Range<usize>> = None;
Event::Start(Tag::Link(link_type, _, _)) => match link_type { let mut end_range: Option<Range<usize>> = None;
LinkType::Reference | LinkType::Inline => Some(range), for (event, range) in parser {
_ => None, match event {
}, Event::Start(Tag::Link(link_type, dest, title)) => start_range = Some(range),
Event::End(Tag::Link(link_type, _, _)) => match link_type { Event::End(Tag::Link(link_type, dest, title)) => end_range = Some(range),
LinkType::Reference | LinkType::Inline => Some(range), _ => (),
_ => None, }
}, match end_range {
_ => None, Some(_) => {
}) let range = Range::<usize> {
.collect(); start: start_range.take().unwrap().start,
let ranges_to_replace_in = vec![..]; end: end_range.take().unwrap().end,
};
ranges_to_replace_in.push(range);
}
None => {}
}
}
let content: String = ranges_to_replace_in let content: String = ranges_to_replace_in
.iter() .iter()
.map(|range| { .map(|range| {
@ -190,14 +197,14 @@ mod tests {
replace_readme_tests! { replace_readme_tests! {
"[content]: ./bla/index.md ", "[content]: ./bla/index.md ",
replace_readme_in_footnote_link_test_1:"[content]: ./bla/readme.md ", replace_readme_in_reference_link_test_1:"[content]: ./bla/readme.md ",
replace_readme_in_footnote_link_test_2:"[content]: ./bla/ReAdme.md ", replace_readme_in_reference_link_test_2:"[content]: ./bla/ReAdme.md ",
replace_readme_in_footnote_link_test_3:"[content]: ./bla/ReaDme.md ", replace_readme_in_reference_link_test_3:"[content]: ./bla/ReaDme.md ",
replace_readme_in_footnote_link_test_4:"[content]: ./bla/README.MD ", replace_readme_in_reference_link_test_4:"[content]: ./bla/README.MD ",
replace_readme_in_footnote_link_test_5:"[content]: ./bla/REadmE.md ", replace_readme_in_reference_link_test_5:"[content]: ./bla/REadmE.md ",
replace_readme_in_footnote_link_test_6:"[content]: ./bla/ReAdme.md ", replace_readme_in_reference_link_test_6:"[content]: ./bla/ReAdme.md ",
replace_readme_in_footnote_link_test_7:"[content]: ./bla/Readme.MD ", replace_readme_in_reference_link_test_7:"[content]: ./bla/Readme.MD ",
replace_readme_in_footnote_link_test_8:"[content]: ./bla/readme.MD ", replace_readme_in_reference_link_test_8:"[content]: ./bla/readme.MD ",
} }
replace_readme_tests! { replace_readme_tests! {
"[content]( ./bla/index.md)", "[content]( ./bla/index.md)",