From cb4a3e0711b330c03e43daf316d883e92fc3a31e Mon Sep 17 00:00:00 2001 From: Eric Huss Date: Wed, 8 May 2019 14:50:59 -0700 Subject: [PATCH] Fix more print.html links. (#871) --- src/utils/mod.rs | 49 ++++++++------ tests/dummy_book/src/second/nested.md | 6 +- tests/rendered_output.rs | 6 +- tests/searchindex_fixture.json | 96 +++++++++++++++++++++++++-- 4 files changed, 131 insertions(+), 26 deletions(-) diff --git a/src/utils/mod.rs b/src/utils/mod.rs index 8b113676..30c776fa 100644 --- a/src/utils/mod.rs +++ b/src/utils/mod.rs @@ -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.*)\.md(?P#.*)?").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, } diff --git a/tests/dummy_book/src/second/nested.md b/tests/dummy_book/src/second/nested.md index c3eb3c8f..2fa98beb 100644 --- a/tests/dummy_book/src/second/nested.md +++ b/tests/dummy_book/src/second/nested.md @@ -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. \ No newline at end of file +both the print page and the non-print page. + +Link [outside](../../std/foo/bar.html). + +![Some image](../images/picture.png) diff --git a/tests/rendered_output.rs b/tests/rendered_output.rs index 2d679e4d..2aa7f93a 100644 --- a/tests/rendered_output.rs +++ b/tests/rendered_output.rs @@ -123,7 +123,11 @@ fn check_correct_relative_links_in_print_page() { assert_contains_strings( first.join("print.html"), - &[r##"the first section,"##], + &[ + r##"the first section,"##, + r##"outside"##, + r##"Some image"##, + ], ); } diff --git a/tests/searchindex_fixture.json b/tests/searchindex_fixture.json index 78a39153..d1e20970 100644 --- a/tests/searchindex_fixture.json +++ b/tests/searchindex_fixture.json @@ -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,