Skip to content

Commit 152822c

Browse files
ehussDylan-DPC
authored andcommitted
Fix more print.html links. (rust-lang#871)
1 parent 5c98b55 commit 152822c

File tree

4 files changed

+130
-25
lines changed

4 files changed

+130
-25
lines changed

src/utils/mod.rs

+28-19
Original file line numberDiff line numberDiff line change
@@ -69,31 +69,40 @@ pub fn id_from_content(content: &str) -> String {
6969

7070
fn adjust_links<'a>(event: Event<'a>, with_base: &str) -> Event<'a> {
7171
lazy_static! {
72-
static ref HTTP_LINK: Regex = Regex::new("^https?://").unwrap();
72+
static ref SCHEME_LINK: Regex = Regex::new(r"^[a-z][a-z0-9+.-]*:").unwrap();
7373
static ref MD_LINK: Regex = Regex::new(r"(?P<link>.*)\.md(?P<anchor>#.*)?").unwrap();
7474
}
7575

76-
match event {
77-
Event::Start(Tag::Link(dest, title)) => {
78-
if !HTTP_LINK.is_match(&dest) {
79-
let dest = if !with_base.is_empty() {
80-
format!("{}/{}", with_base, dest)
81-
} else {
82-
dest.clone().into_owned()
83-
};
84-
85-
if let Some(caps) = MD_LINK.captures(&dest) {
86-
let mut html_link = [&caps["link"], ".html"].concat();
87-
88-
if let Some(anchor) = caps.name("anchor") {
89-
html_link.push_str(anchor.as_str());
90-
}
76+
fn fix<'a>(dest: Cow<'a, str>, base: &str) -> Cow<'a, str> {
77+
// Don't modify links with schemes like `https`.
78+
if !SCHEME_LINK.is_match(&dest) {
79+
// This is a relative link, adjust it as necessary.
80+
let mut fixed_link = String::new();
81+
if !base.is_empty() {
82+
fixed_link.push_str(base);
83+
fixed_link.push_str("/");
84+
}
9185

92-
return Event::Start(Tag::Link(Cow::from(html_link), title));
86+
if let Some(caps) = MD_LINK.captures(&dest) {
87+
fixed_link.push_str(&caps["link"]);
88+
fixed_link.push_str(".html");
89+
if let Some(anchor) = caps.name("anchor") {
90+
fixed_link.push_str(anchor.as_str());
9391
}
94-
}
92+
} else {
93+
fixed_link.push_str(&dest);
94+
};
95+
return Cow::from(fixed_link);
96+
}
97+
dest
98+
}
9599

96-
Event::Start(Tag::Link(dest, title))
100+
match event {
101+
Event::Start(Tag::Link(dest, title)) => {
102+
Event::Start(Tag::Link(fix(dest, with_base), title))
103+
}
104+
Event::Start(Tag::Image(dest, title)) => {
105+
Event::Start(Tag::Image(fix(dest, with_base), title))
97106
}
98107
_ => event,
99108
}

tests/dummy_book/src/second/nested.md

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
# Testing relative links for the print page
22

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

tests/rendered_output.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,11 @@ fn check_correct_relative_links_in_print_page() {
123123

124124
assert_contains_strings(
125125
first.join("print.html"),
126-
&[r##"<a href="second/../first/nested.html">the first section</a>,"##],
126+
&[
127+
r##"<a href="second/../first/nested.html">the first section</a>,"##,
128+
r##"<a href="second/../../std/foo/bar.html">outside</a>"##,
129+
r##"<img src="second/../images/picture.png" alt="Some image" />"##,
130+
],
127131
);
128132
}
129133

tests/searchindex_fixture.json

+92-4
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@
6666
"title": 2
6767
},
6868
"9": {
69-
"body": 10,
69+
"body": 13,
7070
"breadcrumbs": 7,
7171
"title": 5
7272
}
@@ -133,7 +133,7 @@
133133
"title": "Second Chapter"
134134
},
135135
"9": {
136-
"body": "When we link to the first section , it should work on both the print page and the non-print page.",
136+
"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",
137137
"breadcrumbs": "Second Chapter » Testing relative links for the print page",
138138
"id": "9",
139139
"title": "Testing relative links for the print page"
@@ -555,6 +555,22 @@
555555
"i": {
556556
"df": 0,
557557
"docs": {},
558+
"m": {
559+
"a": {
560+
"df": 0,
561+
"docs": {},
562+
"g": {
563+
"df": 1,
564+
"docs": {
565+
"9": {
566+
"tf": 1.0
567+
}
568+
}
569+
}
570+
},
571+
"df": 0,
572+
"docs": {}
573+
},
558574
"n": {
559575
"c": {
560576
"df": 0,
@@ -715,7 +731,7 @@
715731
"df": 1,
716732
"docs": {
717733
"9": {
718-
"tf": 1.4142135623730951
734+
"tf": 1.7320508075688772
719735
}
720736
}
721737
}
@@ -845,6 +861,34 @@
845861
}
846862
}
847863
},
864+
"o": {
865+
"df": 0,
866+
"docs": {},
867+
"u": {
868+
"df": 0,
869+
"docs": {},
870+
"t": {
871+
"df": 0,
872+
"docs": {},
873+
"s": {
874+
"df": 0,
875+
"docs": {},
876+
"i": {
877+
"d": {
878+
"df": 1,
879+
"docs": {
880+
"9": {
881+
"tf": 1.0
882+
}
883+
}
884+
},
885+
"df": 0,
886+
"docs": {}
887+
}
888+
}
889+
}
890+
}
891+
},
848892
"p": {
849893
"a": {
850894
"df": 0,
@@ -1732,6 +1776,22 @@
17321776
"i": {
17331777
"df": 0,
17341778
"docs": {},
1779+
"m": {
1780+
"a": {
1781+
"df": 0,
1782+
"docs": {},
1783+
"g": {
1784+
"df": 1,
1785+
"docs": {
1786+
"9": {
1787+
"tf": 1.0
1788+
}
1789+
}
1790+
}
1791+
},
1792+
"df": 0,
1793+
"docs": {}
1794+
},
17351795
"n": {
17361796
"c": {
17371797
"df": 0,
@@ -1892,7 +1952,7 @@
18921952
"df": 1,
18931953
"docs": {
18941954
"9": {
1895-
"tf": 1.7320508075688772
1955+
"tf": 2.0
18961956
}
18971957
}
18981958
}
@@ -2022,6 +2082,34 @@
20222082
}
20232083
}
20242084
},
2085+
"o": {
2086+
"df": 0,
2087+
"docs": {},
2088+
"u": {
2089+
"df": 0,
2090+
"docs": {},
2091+
"t": {
2092+
"df": 0,
2093+
"docs": {},
2094+
"s": {
2095+
"df": 0,
2096+
"docs": {},
2097+
"i": {
2098+
"d": {
2099+
"df": 1,
2100+
"docs": {
2101+
"9": {
2102+
"tf": 1.0
2103+
}
2104+
}
2105+
},
2106+
"df": 0,
2107+
"docs": {}
2108+
}
2109+
}
2110+
}
2111+
}
2112+
},
20252113
"p": {
20262114
"a": {
20272115
"df": 0,

0 commit comments

Comments
 (0)