Skip to content

Commit e32ddde

Browse files
Fix new sidebar changes
1 parent 6e354ec commit e32ddde

File tree

11 files changed

+230
-115
lines changed

11 files changed

+230
-115
lines changed

build.rs

+1
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ fn compile_sass() -> Result<(), Box<dyn Error>> {
7777

7878
// Compile rustdoc.scss -> rustdoc.css
7979
compile_sass_file("rustdoc", "rustdoc", &[])?;
80+
compile_sass_file("rustdoc-2", "rustdoc-2", &[])?;
8081

8182
// Compile vendored.scss -> vendored.css
8283
compile_sass_file(

src/utils/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ pub use self::daemon::start_daemon;
66
pub(crate) use self::html::rewrite_lol;
77
pub use self::queue::{get_crate_priority, remove_crate_priority, set_crate_priority};
88
pub use self::queue_builder::queue_builder;
9-
pub(crate) use self::rustc_version::parse_rustc_version;
9+
pub(crate) use self::rustc_version::{parse_rustc_date, parse_rustc_version};
1010

1111
#[cfg(test)]
1212
pub(crate) use self::cargo_metadata::{Dependency, Target};

src/utils/rustc_version.rs

+20
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
use crate::error::Result;
22
use anyhow::{anyhow, Context};
3+
use chrono::prelude::*;
4+
use once_cell::sync::Lazy;
35
use regex::Regex;
46

57
/// Parses rustc commit hash from rustc version string
@@ -19,6 +21,24 @@ pub fn parse_rustc_version<S: AsRef<str>>(version: S) -> Result<String> {
1921
))
2022
}
2123

24+
pub fn parse_rustc_date<S: AsRef<str>>(version: S) -> Result<Date<Utc>> {
25+
static RE: Lazy<Regex> = Lazy::new(|| Regex::new(r"-(\d+)-(\d+)-(\d+)$").unwrap());
26+
27+
let cap = RE
28+
.captures(version.as_ref())
29+
.with_context(|| anyhow!("Failed to parse rustc date"))?;
30+
31+
let year = cap.get(1).unwrap().as_str();
32+
let month = cap.get(2).unwrap().as_str();
33+
let day = cap.get(3).unwrap().as_str();
34+
35+
Ok(Utc.ymd(
36+
year.parse::<i32>().unwrap(),
37+
month.parse::<u32>().unwrap(),
38+
day.parse::<u32>().unwrap(),
39+
))
40+
}
41+
2242
#[test]
2343
fn test_parse_rustc_version() {
2444
assert_eq!(

src/web/crate_details.rs

+2
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ impl CrateDetails {
114114
releases.license,
115115
releases.documentation_url,
116116
releases.default_target,
117+
releases.doc_rustc_version,
117118
doc_coverage.total_items,
118119
doc_coverage.documented_items,
119120
doc_coverage.total_items_needing_examples,
@@ -159,6 +160,7 @@ impl CrateDetails {
159160
default_target: krate.get("default_target"),
160161
doc_targets: MetaData::parse_doc_targets(krate.get("doc_targets")),
161162
yanked: krate.get("yanked"),
163+
rustdoc_version: krate.get("doc_rustc_version"),
162164
};
163165

164166
let documented_items: Option<i32> = krate.get("documented_items");

src/web/mod.rs

+9-1
Original file line numberDiff line numberDiff line change
@@ -534,6 +534,7 @@ pub(crate) struct MetaData {
534534
pub(crate) default_target: String,
535535
pub(crate) doc_targets: Vec<String>,
536536
pub(crate) yanked: bool,
537+
pub(crate) rustdoc_version: String,
537538
}
538539

539540
impl MetaData {
@@ -552,7 +553,8 @@ impl MetaData {
552553
releases.rustdoc_status,
553554
releases.default_target,
554555
releases.doc_targets,
555-
releases.yanked
556+
releases.yanked,
557+
releases.doc_rustc_version
556558
FROM releases
557559
INNER JOIN crates ON crates.id = releases.crate_id
558560
WHERE crates.name = $1 AND releases.version = $2",
@@ -572,6 +574,7 @@ impl MetaData {
572574
default_target: row.get(5),
573575
doc_targets: MetaData::parse_doc_targets(row.get(6)),
574576
yanked: row.get(7),
577+
rustdoc_version: row.get(8),
575578
})
576579
}
577580

@@ -927,6 +930,7 @@ mod test {
927930
"arm64-unknown-linux-gnu".to_string(),
928931
],
929932
yanked: false,
933+
rustdoc_version: "rustdoc 1.59.0-nightly (e100ec5bc 2021-12-21)".to_string(),
930934
};
931935

932936
let correct_json = json!({
@@ -942,6 +946,7 @@ mod test {
942946
"arm64-unknown-linux-gnu",
943947
],
944948
"yanked": false,
949+
"rustdoc_version": "rustdoc 1.59.0-nightly (e100ec5bc 2021-12-21)",
945950
});
946951

947952
assert_eq!(correct_json, serde_json::to_value(&metadata).unwrap());
@@ -960,6 +965,7 @@ mod test {
960965
"arm64-unknown-linux-gnu",
961966
],
962967
"yanked": false,
968+
"rustdoc_version": "rustdoc 1.59.0-nightly (e100ec5bc 2021-12-21)",
963969
});
964970

965971
assert_eq!(correct_json, serde_json::to_value(&metadata).unwrap());
@@ -978,6 +984,7 @@ mod test {
978984
"arm64-unknown-linux-gnu",
979985
],
980986
"yanked": false,
987+
"rustdoc_version": "rustdoc 1.59.0-nightly (e100ec5bc 2021-12-21)",
981988
});
982989

983990
assert_eq!(correct_json, serde_json::to_value(&metadata).unwrap());
@@ -1001,6 +1008,7 @@ mod test {
10011008
default_target: "x86_64-unknown-linux-gnu".to_string(),
10021009
doc_targets: vec![],
10031010
yanked: false,
1011+
rustdoc_version: "rustdoc 1.59.0-nightly (e100ec5bc 2021-12-21)".to_string(),
10041012
},
10051013
);
10061014
Ok(())

src/web/source.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,8 @@ impl FileList {
6767
releases.files,
6868
releases.default_target,
6969
releases.doc_targets,
70-
releases.yanked
70+
releases.yanked,
71+
releases.doc_rustc_version
7172
FROM releases
7273
LEFT OUTER JOIN crates ON crates.id = releases.crate_id
7374
WHERE crates.name = $1 AND releases.version = $2",
@@ -147,6 +148,7 @@ impl FileList {
147148
default_target: rows[0].get(6),
148149
doc_targets: MetaData::parse_doc_targets(rows[0].get(7)),
149150
yanked: rows[0].get(8),
151+
rustdoc_version: rows[0].get(9),
150152
},
151153
files: file_list,
152154
})

src/web/statics.rs

+30-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use super::{error::Nope, redirect, redirect_base, STATIC_FILE_CACHE_DURATION};
2-
use crate::utils::report_error;
2+
use crate::utils::{parse_rustc_date, report_error};
33
use anyhow::Context;
4-
use chrono::Utc;
4+
use chrono::prelude::*;
55
use iron::{
66
headers::CacheDirective,
77
headers::{CacheControl, ContentLength, ContentType, LastModified},
@@ -14,8 +14,30 @@ use std::{ffi::OsStr, fs, path::Path};
1414
const VENDORED_CSS: &str = include_str!(concat!(env!("OUT_DIR"), "/vendored.css"));
1515
const STYLE_CSS: &str = include_str!(concat!(env!("OUT_DIR"), "/style.css"));
1616
const RUSTDOC_CSS: &str = include_str!(concat!(env!("OUT_DIR"), "/rustdoc.css"));
17+
const RUSTDOC_CSS_2: &str = include_str!(concat!(env!("OUT_DIR"), "/rustdoc-2.css"));
1718
const STATIC_SEARCH_PATHS: &[&str] = &["static", "vendor"];
1819

20+
fn get_correct_docsrs_style(req: &Request) -> &'static str {
21+
if let Some(query) = req.url.query() {
22+
// First comes the docs.rs version then the rustdoc version. They are separated with a "--".
23+
if let Some(date) = query
24+
.split("--")
25+
.nth(1)
26+
.and_then(|s| parse_rustc_date(s).ok())
27+
{
28+
// This is the date where https://github.com/rust-lang/rust/pull/91356 was merged.
29+
return if Utc.ymd(2021, 12, 6) > date {
30+
RUSTDOC_CSS
31+
} else {
32+
// If this is the new rustdoc layout, we need the newer docs.rs CSS file.
33+
RUSTDOC_CSS_2
34+
};
35+
}
36+
}
37+
// By default, we return the old docs.rs CSS file.
38+
RUSTDOC_CSS
39+
}
40+
1941
pub(crate) fn static_handler(req: &mut Request) -> IronResult<Response> {
2042
let mut file = req.url.path();
2143
file.drain(..2).for_each(std::mem::drop);
@@ -24,7 +46,12 @@ pub(crate) fn static_handler(req: &mut Request) -> IronResult<Response> {
2446
Ok(match file.as_str() {
2547
"vendored.css" => serve_resource(VENDORED_CSS, ContentType("text/css".parse().unwrap())),
2648
"style.css" => serve_resource(STYLE_CSS, ContentType("text/css".parse().unwrap())),
27-
"rustdoc.css" => serve_resource(RUSTDOC_CSS, ContentType("text/css".parse().unwrap())),
49+
"rustdoc.css" => serve_resource(
50+
// We pick the correct "rustdoc.css" static file depending on which rustdoc version
51+
// was used to generate this version of this crate.
52+
get_correct_docsrs_style(req),
53+
ContentType("text/css".parse().unwrap()),
54+
),
2855
file => serve_file(file)?,
2956
})
3057
}

templates/rustdoc/head.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{%- import "macros.html" as macros -%}
2-
<link rel="stylesheet" href="/-/static/rustdoc.css?{{ docsrs_version() | slugify }}" type="text/css" media="all" />
2+
<link rel="stylesheet" href="/-/static/rustdoc.css?{{ docsrs_version() | slugify }}--{{ metadata.rustdoc_version | slugify }}" type="text/css" media="all" />
33

44
<link rel="search" href="/-/static/opensearch.xml" type="application/opensearchdescription+xml" title="Docs.rs" />
55

templates/style/rustdoc-2.scss

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// FIXME: Use modules
2+
@import "rustdoc-common";
3+
4+
// This file is needed to overload the previous docs.rs style. It is added into crates generated
5+
// using rustdoc after https://github.com/rust-lang/rust/pull/91356 has been merged.
6+
7+
#rustdoc_body_wrapper {
8+
padding: 0;
9+
10+
.sidebar {
11+
margin-top: 0;
12+
top: $top-navbar-height;
13+
14+
.sidebar-menu {
15+
top: $top-navbar-height;
16+
}
17+
}
18+
19+
main {
20+
padding-bottom: 50px;
21+
}
22+
}
23+
24+
div.container-rustdoc {
25+
> .docs-rs-footer {
26+
bottom: 0;
27+
right: 0;
28+
}
29+
}
30+
31+
div.container-rustdoc:not(.source) {
32+
> .docs-rs-footer {
33+
left: 200px;
34+
}
35+
}
36+
37+
div.rustdoc {
38+
#sidebar-toggle {
39+
top: 0;
40+
}
41+
}

templates/style/rustdoc-common.scss

+121
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
// FIXME: Use modules
2+
@import "vars", "navbar", "themes", "fa", "footer";
3+
4+
// This rule is needed to be sure that the footer will always be at the bottom of the page.
5+
#rustdoc_body_wrapper {
6+
min-height: calc(100vh - #{$top-navbar-height + $footer-height + 2});
7+
}
8+
9+
#clipboard {
10+
cursor: pointer;
11+
}
12+
13+
// Force the navbar to be left-aligned on rustdoc pages
14+
body.rustdoc-page > .nav-container > .container {
15+
margin-left: 0;
16+
}
17+
18+
div.container-rustdoc {
19+
text-align: left;
20+
21+
> .docs-rs-footer {
22+
width: unset;
23+
}
24+
}
25+
26+
div.container-rustdoc {
27+
width: unset;
28+
}
29+
30+
div.container-rustdoc:not(.source) {
31+
// This is when the rustdoc sidebar "disappears" (for mobile mode).
32+
@media (max-width: 700px) {
33+
> .docs-rs-footer:not(.source) {
34+
left: -15px;
35+
}
36+
}
37+
}
38+
39+
div.container-rustdoc.source {
40+
> .docs-rs-footer {
41+
left: -15px;
42+
// This is needed because even though the sidebar only contains the header, it still takes
43+
// all the height, going over the footer.
44+
z-index: 1;
45+
}
46+
}
47+
48+
// this is a super nasty override for help dialog in rustdocs
49+
// see #52 for details
50+
body.blur {
51+
> :not(#help) {
52+
filter: none;
53+
-webkit-filter: none;
54+
}
55+
56+
> div.nav-container > *,
57+
> div.docsrs-package-container > *,
58+
> div.rustdoc > :not(#help) {
59+
filter: blur(8px);
60+
-webkit-filter: blur(8px);
61+
opacity: 0.7;
62+
}
63+
}
64+
65+
// rustdoc overrides
66+
div.rustdoc {
67+
$padding-x: 15px;
68+
padding: 10px $padding-x 20px;
69+
position: relative;
70+
71+
@media (max-width: 700px) {
72+
padding-top: 0;
73+
}
74+
75+
.sidebar {
76+
@media (min-width: 701px) {
77+
margin-top: $top-navbar-height;
78+
}
79+
80+
.block > ul > li {
81+
margin-right: -10px;
82+
}
83+
84+
@media (max-width: 700px) {
85+
margin-left: -2 * $padding-x; // offset the additional padding added by the parent containers
86+
width: calc(100% + #{4 * $padding-x});
87+
88+
&.mobile {
89+
top: $top-navbar-height;
90+
margin-left: 0; // since the sidebar is now fixed position, remove the padding workaround
91+
width: 100%;
92+
93+
.sidebar-elems.show-it {
94+
top: 45px + $top-navbar-height;
95+
}
96+
97+
#sidebar-filler {
98+
top: $top-navbar-height;
99+
}
100+
}
101+
}
102+
}
103+
104+
#source-sidebar {
105+
top: $top-navbar-height;
106+
}
107+
108+
&:focus {
109+
outline: unset;
110+
}
111+
112+
// Overriding some outdated rustdoc CSS rules
113+
#results {
114+
position: initial !important;
115+
overflow: initial !important;
116+
117+
> table {
118+
margin-bottom: 0 !important;
119+
}
120+
}
121+
}

0 commit comments

Comments
 (0)