Skip to content

Commit 384ebf5

Browse files
Fix new sidebar changes
1 parent 6e354ec commit 384ebf5

File tree

11 files changed

+222
-113
lines changed

11 files changed

+222
-113
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::{get_correct_docsrs_style_file, parse_rustc_version};
1010

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

src/utils/rustc_version.rs

+34
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,38 @@ pub fn parse_rustc_version<S: AsRef<str>>(version: S) -> Result<String> {
1921
))
2022
}
2123

24+
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+
42+
/// Picks the correct "rustdoc.css" static file depending on which rustdoc version was used to
43+
/// generate this version of this crate.
44+
pub fn get_correct_docsrs_style_file(version: &str) -> String {
45+
if let Ok(date) = parse_rustc_date(version) {
46+
// This is the date where https://github.com/rust-lang/rust/pull/91356 was merged.
47+
if Utc.ymd(2021, 12, 6) < date {
48+
// If this is the new rustdoc layout, we need the newer docs.rs CSS file.
49+
return "rustdoc-2.css".to_owned();
50+
}
51+
}
52+
// By default, we return the old docs.rs CSS file.
53+
"rustdoc.css".to_owned()
54+
}
55+
2256
#[test]
2357
fn test_parse_rustc_version() {
2458
assert_eq!(

src/web/crate_details.rs

+3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use super::{match_version, redirect_base, render_markdown, MatchSemver, MetaData};
2+
use crate::utils::get_correct_docsrs_style_file;
23
use crate::{db::Pool, impl_webpage, repositories::RepositoryStatsUpdater, web::page::WebPage};
34
use chrono::{DateTime, Utc};
45
use iron::prelude::*;
@@ -114,6 +115,7 @@ impl CrateDetails {
114115
releases.license,
115116
releases.documentation_url,
116117
releases.default_target,
118+
releases.doc_rustc_version,
117119
doc_coverage.total_items,
118120
doc_coverage.documented_items,
119121
doc_coverage.total_items_needing_examples,
@@ -159,6 +161,7 @@ impl CrateDetails {
159161
default_target: krate.get("default_target"),
160162
doc_targets: MetaData::parse_doc_targets(krate.get("doc_targets")),
161163
yanked: krate.get("yanked"),
164+
rustdoc_css_file: get_correct_docsrs_style_file(krate.get("doc_rustc_version")),
162165
};
163166

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

src/web/mod.rs

+12-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
33
pub(crate) mod page;
44

5+
use crate::utils::get_correct_docsrs_style_file;
56
use crate::utils::report_error;
67
use anyhow::{anyhow, Context as _};
78
use log::info;
@@ -534,6 +535,9 @@ pub(crate) struct MetaData {
534535
pub(crate) default_target: String,
535536
pub(crate) doc_targets: Vec<String>,
536537
pub(crate) yanked: bool,
538+
/// CSS file to use depending on the rustdoc version used to generate this version of this
539+
/// crate.
540+
pub(crate) rustdoc_css_file: String,
537541
}
538542

539543
impl MetaData {
@@ -552,7 +556,8 @@ impl MetaData {
552556
releases.rustdoc_status,
553557
releases.default_target,
554558
releases.doc_targets,
555-
releases.yanked
559+
releases.yanked,
560+
releases.doc_rustc_version
556561
FROM releases
557562
INNER JOIN crates ON crates.id = releases.crate_id
558563
WHERE crates.name = $1 AND releases.version = $2",
@@ -572,6 +577,7 @@ impl MetaData {
572577
default_target: row.get(5),
573578
doc_targets: MetaData::parse_doc_targets(row.get(6)),
574579
yanked: row.get(7),
580+
rustdoc_css_file: get_correct_docsrs_style_file(row.get(8)),
575581
})
576582
}
577583

@@ -927,6 +933,7 @@ mod test {
927933
"arm64-unknown-linux-gnu".to_string(),
928934
],
929935
yanked: false,
936+
rustdoc_css_file: "rustdoc.css".to_string(),
930937
};
931938

932939
let correct_json = json!({
@@ -942,6 +949,7 @@ mod test {
942949
"arm64-unknown-linux-gnu",
943950
],
944951
"yanked": false,
952+
"rustdoc_css_file": "rustdoc.css",
945953
});
946954

947955
assert_eq!(correct_json, serde_json::to_value(&metadata).unwrap());
@@ -960,6 +968,7 @@ mod test {
960968
"arm64-unknown-linux-gnu",
961969
],
962970
"yanked": false,
971+
"rustdoc_css_file": "rustdoc 1.59.0-nightly (e100ec5bc 2021-12-21)",
963972
});
964973

965974
assert_eq!(correct_json, serde_json::to_value(&metadata).unwrap());
@@ -978,6 +987,7 @@ mod test {
978987
"arm64-unknown-linux-gnu",
979988
],
980989
"yanked": false,
990+
"rustdoc_css_file": "rustdoc 1.59.0-nightly (e100ec5bc 2021-12-21)",
981991
});
982992

983993
assert_eq!(correct_json, serde_json::to_value(&metadata).unwrap());
@@ -1001,6 +1011,7 @@ mod test {
10011011
default_target: "x86_64-unknown-linux-gnu".to_string(),
10021012
doc_targets: vec![],
10031013
yanked: false,
1014+
rustdoc_css_file: "rustdoc.css".to_string(),
10041015
},
10051016
);
10061017
Ok(())

src/web/source.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
use crate::{
44
db::Pool,
55
impl_webpage,
6+
utils::get_correct_docsrs_style_file,
67
web::{
78
error::Nope, file::File as DbFile, match_version, page::WebPage, redirect_base,
89
MatchSemver, MetaData, Url,
@@ -67,7 +68,8 @@ impl FileList {
6768
releases.files,
6869
releases.default_target,
6970
releases.doc_targets,
70-
releases.yanked
71+
releases.yanked,
72+
releases.doc_rustc_version
7173
FROM releases
7274
LEFT OUTER JOIN crates ON crates.id = releases.crate_id
7375
WHERE crates.name = $1 AND releases.version = $2",
@@ -147,6 +149,7 @@ impl FileList {
147149
default_target: rows[0].get(6),
148150
doc_targets: MetaData::parse_doc_targets(rows[0].get(7)),
149151
yanked: rows[0].get(8),
152+
rustdoc_css_file: get_correct_docsrs_style_file(rows[0].get(9)),
150153
},
151154
files: file_list,
152155
})

src/web/statics.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use super::{error::Nope, redirect, redirect_base, STATIC_FILE_CACHE_DURATION};
22
use crate::utils::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,6 +14,7 @@ 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

1920
pub(crate) fn static_handler(req: &mut Request) -> IronResult<Response> {
@@ -25,6 +26,7 @@ pub(crate) fn static_handler(req: &mut Request) -> IronResult<Response> {
2526
"vendored.css" => serve_resource(VENDORED_CSS, ContentType("text/css".parse().unwrap())),
2627
"style.css" => serve_resource(STYLE_CSS, ContentType("text/css".parse().unwrap())),
2728
"rustdoc.css" => serve_resource(RUSTDOC_CSS, ContentType("text/css".parse().unwrap())),
29+
"rustdoc-2.css" => serve_resource(RUSTDOC_CSS_2, ContentType("text/css".parse().unwrap())),
2830
file => serve_file(file)?,
2931
})
3032
}

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/{{metadata.rustdoc_css_file}}?{{ docsrs_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)