Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix more print.html links. #871

Merged
merged 1 commit into from
May 8, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 28 additions & 19 deletions src/utils/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,31 +68,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();
}

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());
}
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("/");
}

return Event::Start(Tag::Link(Cow::from(html_link), title));
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
}

Event::Start(Tag::Link(dest, title))
match event {
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,
}
Expand Down
6 changes: 5 additions & 1 deletion tests/dummy_book/src/second/nested.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
# Testing relative links for the print page

When we link to [the first section](../first/nested.md), it should work on
both the print page and the non-print page.
both the print page and the non-print page.

Link [outside](../../std/foo/bar.html).

![Some image](../images/picture.png)
6 changes: 5 additions & 1 deletion tests/rendered_output.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,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" />"##,
],
);
}

Expand Down
96 changes: 92 additions & 4 deletions tests/searchindex_fixture.json
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@
"title": 2
},
"9": {
"body": 10,
"body": 13,
"breadcrumbs": 7,
"title": 5
}
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -715,7 +731,7 @@
"df": 1,
"docs": {
"9": {
"tf": 1.4142135623730951
"tf": 1.7320508075688772
}
}
}
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -1892,7 +1952,7 @@
"df": 1,
"docs": {
"9": {
"tf": 1.7320508075688772
"tf": 2.0
}
}
}
Expand Down Expand Up @@ -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,
Expand Down