Fix more print.html links. (#871)

This commit is contained in:
Eric Huss 2019-05-08 14:50:59 -07:00 committed by Dylan DPC
parent 9194a40acd
commit cb4a3e0711
4 changed files with 131 additions and 26 deletions

View File

@ -69,31 +69,40 @@ pub fn id_from_content(content: &str) -> String {
fn adjust_links<'a>(event: Event<'a>, with_base: &str) -> Event<'a> {
lazy_static! {
static ref HTTP_LINK: Regex = Regex::new("^https?://").unwrap();
static ref SCHEME_LINK: Regex = Regex::new(r"^[a-z][a-z0-9+.-]*:").unwrap();
static ref MD_LINK: Regex = Regex::new(r"(?P<link>.*)\.md(?P<anchor>#.*)?").unwrap();
}
fn fix<'a>(dest: Cow<'a, str>, base: &str) -> Cow<'a, str> {
// Don't modify links with schemes like `https`.
if !SCHEME_LINK.is_match(&dest) {
// This is a relative link, adjust it as necessary.
let mut fixed_link = String::new();
if !base.is_empty() {
fixed_link.push_str(base);
fixed_link.push_str("/");
}
if let Some(caps) = MD_LINK.captures(&dest) {
fixed_link.push_str(&caps["link"]);
fixed_link.push_str(".html");
if let Some(anchor) = caps.name("anchor") {
fixed_link.push_str(anchor.as_str());
}
} else {
fixed_link.push_str(&dest);
};
return Cow::from(fixed_link);
}
dest
}
match event {
Event::Start(Tag::Link(dest, title)) => {
if !HTTP_LINK.is_match(&dest) {
let dest = if !with_base.is_empty() {
format!("{}/{}", with_base, dest)
} else {
dest.clone().into_owned()
};
if let Some(caps) = MD_LINK.captures(&dest) {
let mut html_link = [&caps["link"], ".html"].concat();
if let Some(anchor) = caps.name("anchor") {
html_link.push_str(anchor.as_str());
}
return Event::Start(Tag::Link(Cow::from(html_link), title));
}
}
Event::Start(Tag::Link(dest, title))
Event::Start(Tag::Link(fix(dest, with_base), title))
}
Event::Start(Tag::Image(dest, title)) => {
Event::Start(Tag::Image(fix(dest, with_base), title))
}
_ => event,
}

View File

@ -2,3 +2,7 @@
When we link to [the first section](../first/nested.md), it should work on
both the print page and the non-print page.
Link [outside](../../std/foo/bar.html).
![Some image](../images/picture.png)

View File

@ -123,7 +123,11 @@ fn check_correct_relative_links_in_print_page() {
assert_contains_strings(
first.join("print.html"),
&[r##"<a href="second/../first/nested.html">the first section</a>,"##],
&[
r##"<a href="second/../first/nested.html">the first section</a>,"##,
r##"<a href="second/../../std/foo/bar.html">outside</a>"##,
r##"<img src="second/../images/picture.png" alt="Some image" />"##,
],
);
}

View File

@ -66,7 +66,7 @@
"title": 2
},
"9": {
"body": 10,
"body": 13,
"breadcrumbs": 7,
"title": 5
}
@ -133,7 +133,7 @@
"title": "Second Chapter"
},
"9": {
"body": "When we link to the first section , it should work on both the print page and the non-print page.",
"body": "When we link to the first section , it should work on both the print page and the non-print page. Link outside . Some image",
"breadcrumbs": "Second Chapter » Testing relative links for the print page",
"id": "9",
"title": "Testing relative links for the print page"
@ -555,6 +555,22 @@
"i": {
"df": 0,
"docs": {},
"m": {
"a": {
"df": 0,
"docs": {},
"g": {
"df": 1,
"docs": {
"9": {
"tf": 1.0
}
}
}
},
"df": 0,
"docs": {}
},
"n": {
"c": {
"df": 0,
@ -715,7 +731,7 @@
"df": 1,
"docs": {
"9": {
"tf": 1.4142135623730951
"tf": 1.7320508075688772
}
}
}
@ -845,6 +861,34 @@
}
}
},
"o": {
"df": 0,
"docs": {},
"u": {
"df": 0,
"docs": {},
"t": {
"df": 0,
"docs": {},
"s": {
"df": 0,
"docs": {},
"i": {
"d": {
"df": 1,
"docs": {
"9": {
"tf": 1.0
}
}
},
"df": 0,
"docs": {}
}
}
}
}
},
"p": {
"a": {
"df": 0,
@ -1732,6 +1776,22 @@
"i": {
"df": 0,
"docs": {},
"m": {
"a": {
"df": 0,
"docs": {},
"g": {
"df": 1,
"docs": {
"9": {
"tf": 1.0
}
}
}
},
"df": 0,
"docs": {}
},
"n": {
"c": {
"df": 0,
@ -1892,7 +1952,7 @@
"df": 1,
"docs": {
"9": {
"tf": 1.7320508075688772
"tf": 2.0
}
}
}
@ -2022,6 +2082,34 @@
}
}
},
"o": {
"df": 0,
"docs": {},
"u": {
"df": 0,
"docs": {},
"t": {
"df": 0,
"docs": {},
"s": {
"df": 0,
"docs": {},
"i": {
"d": {
"df": 1,
"docs": {
"9": {
"tf": 1.0
}
}
},
"df": 0,
"docs": {}
}
}
}
}
},
"p": {
"a": {
"df": 0,