Skip to content

Commit ec2839b

Browse files
committed
Keep the subpage when going to latest version
Addresses rust-lang#200 If the page does not exist on the latest version, searches for a page with that title.
1 parent 5d22713 commit ec2839b

File tree

2 files changed

+25
-4
lines changed

2 files changed

+25
-4
lines changed

src/web/rustdoc.rs

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,8 @@ pub fn rustdoc_html_server_handler(req: &mut Request) -> IronResult<Response> {
224224

225225
let path = {
226226
let mut path = req_path.join("/");
227-
if path.ends_with("/") {
227+
if path.ends_with('/') {
228+
req_path.pop(); // get rid of empty string
228229
path.push_str("index.html");
229230
req_path.push("index.html");
230231
}
@@ -263,12 +264,32 @@ pub fn rustdoc_html_server_handler(req: &mut Request) -> IronResult<Response> {
263264

264265
content.crate_details = Some(crate_details);
265266

267+
let (path, version) = if let Some(version) = latest_version {
268+
req_path[2] = &version;
269+
let path = if File::from_path(&conn, &req_path.join("/")).is_some() {
270+
req_path[3..].join("/") // NOTE: this adds 'index.html' if it wasn't there before
271+
} else { // this page doesn't exist in the latest version
272+
let search_item = if *req_path.last().unwrap() == "index.html" { // this is a module
273+
req_path[&req_path.len() - 2]
274+
} else { // this is an item
275+
req_path.last().unwrap().split('.').nth(1)
276+
.expect("paths should be of the form <class>.<name>.html")
277+
};
278+
// TODO: check if req_path[3] is the platform choice or the name of the crate
279+
format!("{}/?search={}", req_path[3], search_item)
280+
};
281+
(path, version)
282+
} else {
283+
(String::new(), String::new())
284+
};
285+
266286
Page::new(content)
267287
.set_true("show_package_navigation")
268288
.set_true("package_navigation_documentation_tab")
269289
.set_true("package_navigation_show_platforms_tab")
270-
.set_bool("is_latest_version", latest_version.is_none())
271-
.set("latest_version", &latest_version.unwrap_or(String::new()))
290+
.set_bool("is_latest_version", path.is_empty())
291+
.set("path_in_latest", &path)
292+
.set("latest_version", &version)
272293
.to_resp("rustdoc")
273294
}
274295

templates/navigation_rustdoc.hbs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@
8686
</li>
8787
{{#unless ../../varsb.is_latest_version}}
8888
<li class="pure-menu-item">
89-
<a href="/{{name}}/{{../../varss.latest_version}}" class="pure-menu-link warn" title="You are seeing an outdated version of {{name}} crate. Click here to go to latest version."><i class="fa fa-fw fa-warning"></i><span class="title"> Go to latest version</span></a>
89+
<a href="/{{name}}/{{../../varss.latest_version}}/{{../../varss.path_in_latest}}" class="pure-menu-link warn" title="You are seeing an outdated version of {{name}} crate. Click here to go to latest version."><i class="fa fa-fw fa-warning"></i><span class="title"> Go to latest version</span></a>
9090
</li>
9191
{{/unless}}
9292
<li class="pure-menu-item">

0 commit comments

Comments
 (0)