Skip to content

Commit

Permalink
Fix new sidebar changes
Browse files Browse the repository at this point in the history
  • Loading branch information
GuillaumeGomez committed Dec 24, 2021
1 parent 6e354ec commit 60c8bd9
Show file tree
Hide file tree
Showing 11 changed files with 230 additions and 115 deletions.
1 change: 1 addition & 0 deletions build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ fn compile_sass() -> Result<(), Box<dyn Error>> {

// Compile rustdoc.scss -> rustdoc.css
compile_sass_file("rustdoc", "rustdoc", &[])?;
compile_sass_file("rustdoc-2", "rustdoc-2", &[])?;

// Compile vendored.scss -> vendored.css
compile_sass_file(
Expand Down
2 changes: 1 addition & 1 deletion src/utils/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ pub use self::daemon::start_daemon;
pub(crate) use self::html::rewrite_lol;
pub use self::queue::{get_crate_priority, remove_crate_priority, set_crate_priority};
pub use self::queue_builder::queue_builder;
pub(crate) use self::rustc_version::parse_rustc_version;
pub(crate) use self::rustc_version::{parse_rustc_date, parse_rustc_version};

#[cfg(test)]
pub(crate) use self::cargo_metadata::{Dependency, Target};
Expand Down
20 changes: 20 additions & 0 deletions src/utils/rustc_version.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use crate::error::Result;
use anyhow::{anyhow, Context};
use chrono::prelude::*;
use once_cell::sync::Lazy;
use regex::Regex;

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

pub fn parse_rustc_date<S: AsRef<str>>(version: S) -> Result<Date<Utc>> {
static RE: Lazy<Regex> = Lazy::new(|| Regex::new(r"-(\d+)-(\d+)-(\d+)$").unwrap());

let cap = RE
.captures(version.as_ref())
.with_context(|| anyhow!("Failed to parse rustc date"))?;

let year = cap.get(1).unwrap().as_str();
let month = cap.get(2).unwrap().as_str();
let day = cap.get(3).unwrap().as_str();

Ok(Utc.ymd(
year.parse::<i32>().unwrap(),
month.parse::<u32>().unwrap(),
day.parse::<u32>().unwrap(),
))
}

#[test]
fn test_parse_rustc_version() {
assert_eq!(
Expand Down
2 changes: 2 additions & 0 deletions src/web/crate_details.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ impl CrateDetails {
releases.license,
releases.documentation_url,
releases.default_target,
releases.doc_rustc_version,
doc_coverage.total_items,
doc_coverage.documented_items,
doc_coverage.total_items_needing_examples,
Expand Down Expand Up @@ -159,6 +160,7 @@ impl CrateDetails {
default_target: krate.get("default_target"),
doc_targets: MetaData::parse_doc_targets(krate.get("doc_targets")),
yanked: krate.get("yanked"),
rustdoc_version: krate.get("doc_rustc_version"),
};

let documented_items: Option<i32> = krate.get("documented_items");
Expand Down
10 changes: 9 additions & 1 deletion src/web/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -534,6 +534,7 @@ pub(crate) struct MetaData {
pub(crate) default_target: String,
pub(crate) doc_targets: Vec<String>,
pub(crate) yanked: bool,
pub(crate) rustdoc_version: String,
}

impl MetaData {
Expand All @@ -552,7 +553,8 @@ impl MetaData {
releases.rustdoc_status,
releases.default_target,
releases.doc_targets,
releases.yanked
releases.yanked,
releases.doc_rustc_version
FROM releases
INNER JOIN crates ON crates.id = releases.crate_id
WHERE crates.name = $1 AND releases.version = $2",
Expand All @@ -572,6 +574,7 @@ impl MetaData {
default_target: row.get(5),
doc_targets: MetaData::parse_doc_targets(row.get(6)),
yanked: row.get(7),
rustdoc_version: row.get(8),
})
}

Expand Down Expand Up @@ -927,6 +930,7 @@ mod test {
"arm64-unknown-linux-gnu".to_string(),
],
yanked: false,
rustdoc_version: "rustdoc 1.59.0-nightly (e100ec5bc 2021-12-21)".to_string(),
};

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

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

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

assert_eq!(correct_json, serde_json::to_value(&metadata).unwrap());
Expand All @@ -1001,6 +1008,7 @@ mod test {
default_target: "x86_64-unknown-linux-gnu".to_string(),
doc_targets: vec![],
yanked: false,
rustdoc_version: "rustc 2.0.0-nightly (000000000 1970-01-01)".to_string(),
},
);
Ok(())
Expand Down
4 changes: 3 additions & 1 deletion src/web/source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,8 @@ impl FileList {
releases.files,
releases.default_target,
releases.doc_targets,
releases.yanked
releases.yanked,
releases.doc_rustc_version
FROM releases
LEFT OUTER JOIN crates ON crates.id = releases.crate_id
WHERE crates.name = $1 AND releases.version = $2",
Expand Down Expand Up @@ -147,6 +148,7 @@ impl FileList {
default_target: rows[0].get(6),
doc_targets: MetaData::parse_doc_targets(rows[0].get(7)),
yanked: rows[0].get(8),
rustdoc_version: rows[0].get(9),
},
files: file_list,
})
Expand Down
33 changes: 30 additions & 3 deletions src/web/statics.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use super::{error::Nope, redirect, redirect_base, STATIC_FILE_CACHE_DURATION};
use crate::utils::report_error;
use crate::utils::{parse_rustc_date, report_error};
use anyhow::Context;
use chrono::Utc;
use chrono::prelude::*;
use iron::{
headers::CacheDirective,
headers::{CacheControl, ContentLength, ContentType, LastModified},
Expand All @@ -14,8 +14,30 @@ use std::{ffi::OsStr, fs, path::Path};
const VENDORED_CSS: &str = include_str!(concat!(env!("OUT_DIR"), "/vendored.css"));
const STYLE_CSS: &str = include_str!(concat!(env!("OUT_DIR"), "/style.css"));
const RUSTDOC_CSS: &str = include_str!(concat!(env!("OUT_DIR"), "/rustdoc.css"));
const RUSTDOC_CSS_2: &str = include_str!(concat!(env!("OUT_DIR"), "/rustdoc-2.css"));
const STATIC_SEARCH_PATHS: &[&str] = &["static", "vendor"];

fn get_correct_docsrs_style(req: &Request) -> &'static str {
if let Some(query) = req.url.query() {
// First comes the docs.rs version then the rustdoc version. They are separated with a "--".
if let Some(date) = query
.split("--")
.nth(1)
.and_then(|s| parse_rustc_date(s).ok())
{
// This is the date where https://github.com/rust-lang/rust/pull/91356 was merged.
return if Utc.ymd(2021, 12, 6) > date {
RUSTDOC_CSS
} else {
// If this is the new rustdoc layout, we need the newer docs.rs CSS file.
RUSTDOC_CSS_2
};
}
}
// By default, we return the old docs.rs CSS file.
RUSTDOC_CSS
}

pub(crate) fn static_handler(req: &mut Request) -> IronResult<Response> {
let mut file = req.url.path();
file.drain(..2).for_each(std::mem::drop);
Expand All @@ -24,7 +46,12 @@ pub(crate) fn static_handler(req: &mut Request) -> IronResult<Response> {
Ok(match file.as_str() {
"vendored.css" => serve_resource(VENDORED_CSS, ContentType("text/css".parse().unwrap())),
"style.css" => serve_resource(STYLE_CSS, ContentType("text/css".parse().unwrap())),
"rustdoc.css" => serve_resource(RUSTDOC_CSS, ContentType("text/css".parse().unwrap())),
"rustdoc.css" => serve_resource(
// We pick the correct "rustdoc.css" static file depending on which rustdoc version
// was used to generate this version of this crate.
get_correct_docsrs_style(req),
ContentType("text/css".parse().unwrap()),
),
file => serve_file(file)?,
})
}
Expand Down
2 changes: 1 addition & 1 deletion templates/rustdoc/head.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{%- import "macros.html" as macros -%}
<link rel="stylesheet" href="/-/static/rustdoc.css?{{ docsrs_version() | slugify }}" type="text/css" media="all" />
<link rel="stylesheet" href="/-/static/rustdoc.css?{{ docsrs_version() | slugify }}--{{ metadata.rustdoc_version | slugify }}" type="text/css" media="all" />

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

Expand Down
41 changes: 41 additions & 0 deletions templates/style/rustdoc-2.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// FIXME: Use modules
@import "rustdoc-common";

// This file is needed to overload the previous docs.rs style. It is added into crates generated
// using rustdoc after https://github.com/rust-lang/rust/pull/91356 has been merged.

#rustdoc_body_wrapper {
padding: 0;

.sidebar {
margin-top: 0;
top: $top-navbar-height;

.sidebar-menu {
top: $top-navbar-height;
}
}

main {
padding-bottom: 50px;
}
}

div.container-rustdoc {
> .docs-rs-footer {
bottom: 0;
right: 0;
}
}

div.container-rustdoc:not(.source) {
> .docs-rs-footer {
left: 200px;
}
}

div.rustdoc {
#sidebar-toggle {
top: 0;
}
}
121 changes: 121 additions & 0 deletions templates/style/rustdoc-common.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
// FIXME: Use modules
@import "vars", "navbar", "themes", "fa", "footer";

// This rule is needed to be sure that the footer will always be at the bottom of the page.
#rustdoc_body_wrapper {
min-height: calc(100vh - #{$top-navbar-height + $footer-height + 2});
}

#clipboard {
cursor: pointer;
}

// Force the navbar to be left-aligned on rustdoc pages
body.rustdoc-page > .nav-container > .container {
margin-left: 0;
}

div.container-rustdoc {
text-align: left;

> .docs-rs-footer {
width: unset;
}
}

div.container-rustdoc {
width: unset;
}

div.container-rustdoc:not(.source) {
// This is when the rustdoc sidebar "disappears" (for mobile mode).
@media (max-width: 700px) {
> .docs-rs-footer:not(.source) {
left: -15px;
}
}
}

div.container-rustdoc.source {
> .docs-rs-footer {
left: -15px;
// This is needed because even though the sidebar only contains the header, it still takes
// all the height, going over the footer.
z-index: 1;
}
}

// this is a super nasty override for help dialog in rustdocs
// see #52 for details
body.blur {
> :not(#help) {
filter: none;
-webkit-filter: none;
}

> div.nav-container > *,
> div.docsrs-package-container > *,
> div.rustdoc > :not(#help) {
filter: blur(8px);
-webkit-filter: blur(8px);
opacity: 0.7;
}
}

// rustdoc overrides
div.rustdoc {
$padding-x: 15px;
padding: 10px $padding-x 20px;
position: relative;

@media (max-width: 700px) {
padding-top: 0;
}

.sidebar {
@media (min-width: 701px) {
margin-top: $top-navbar-height;
}

.block > ul > li {
margin-right: -10px;
}

@media (max-width: 700px) {
margin-left: -2 * $padding-x; // offset the additional padding added by the parent containers
width: calc(100% + #{4 * $padding-x});

&.mobile {
top: $top-navbar-height;
margin-left: 0; // since the sidebar is now fixed position, remove the padding workaround
width: 100%;

.sidebar-elems.show-it {
top: 45px + $top-navbar-height;
}

#sidebar-filler {
top: $top-navbar-height;
}
}
}
}

#source-sidebar {
top: $top-navbar-height;
}

&:focus {
outline: unset;
}

// Overriding some outdated rustdoc CSS rules
#results {
position: initial !important;
overflow: initial !important;

> table {
margin-bottom: 0 !important;
}
}
}
Loading

0 comments on commit 60c8bd9

Please sign in to comment.