Skip to content

Redirect to "main crate" when clicking Rust logo (or custom logo) #81203

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

Closed
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
12 changes: 5 additions & 7 deletions src/librustdoc/html/layout.rs
Original file line number Diff line number Diff line change
@@ -4,7 +4,7 @@ use std::path::PathBuf;
use crate::externalfiles::ExternalHtml;
use crate::html::escape::Escape;
use crate::html::format::{Buffer, Print};
use crate::html::render::{ensure_trailing_slash, StylePath};
use crate::html::render::StylePath;

#[derive(Clone)]
crate struct Layout {
@@ -134,22 +134,20 @@ crate fn render<T: Print, S: Print>(
root_path = page.root_path,
css_class = page.css_class,
logo = {
let p = format!("{}{}", page.root_path, layout.krate);
let p = ensure_trailing_slash(&p);
if layout.logo.is_empty() {
format!(
"<a href='{path}index.html'>\
"<a href='{path}logo-redirect.html'>\
<div class='logo-container rust-logo'>\
<img src='{static_root_path}rust-logo{suffix}.png' alt='logo'></div></a>",
path = p,
path = page.root_path,
static_root_path = static_root_path,
suffix = page.resource_suffix
)
} else {
format!(
"<a href='{}index.html'>\
"<a href='{}logo-redirect.html'>\
<div class='logo-container'><img src='{}' alt='logo'></div></a>",
p, layout.logo
page.root_path, layout.logo
)
}
},
15 changes: 15 additions & 0 deletions src/librustdoc/html/render/mod.rs
Original file line number Diff line number Diff line change
@@ -596,6 +596,21 @@ impl<'tcx> FormatRenderer<'tcx> for Context<'tcx> {
&style_files,
);
self.shared.fs.write(&settings_file, v.as_bytes())?;

// HACK(ThePuzzlemaker, #81031): As suggested by jynelson, we create a
// redirect page in the parent directory (below the specific crate dir,
// i.e. target/doc or similar) redirecting to that crate's index. This
// way, the last crate compiled will be the one redirected to. Assuming
// you never document examples, and that if you document a binary, you
// want it to take precedence over a library present there, this works
// to have the logo redirect to the "main crate" (the one that would be
// opened by cargo doc --open), without having to add a new rustdoc
// flag and cargo integration.

let v =
layout::redirect(Path::new(&*krate.name.as_str()).join("index.html").to_str().unwrap());
self.shared.fs.write(&self.dst.join("logo-redirect.html"), v.as_bytes())?;

Ok(())
}

2 changes: 1 addition & 1 deletion src/test/rustdoc/keyword.rs
Original file line number Diff line number Diff line change
@@ -8,7 +8,7 @@
// @has foo/keyword.match.html '//a[@class="keyword"]' 'match'
// @has foo/keyword.match.html '//span[@class="in-band"]' 'Keyword match'
// @has foo/keyword.match.html '//section[@id="main"]//div[@class="docblock"]//p' 'this is a test!'
// @has foo/index.html '//a/@href' '../foo/index.html'
// @has foo/index.html '//a/@href' '../logo-redirect.html'
// @!has foo/foo/index.html
// @!has-dir foo/foo
#[doc(keyword = "match")]