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 29, 2021
1 parent 6e354ec commit 88e2435
Show file tree
Hide file tree
Showing 12 changed files with 255 additions and 129 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-2021-12-06", "rustdoc-2021-12-06", &[])?;

// 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::{get_correct_docsrs_style_file, parse_rustc_version};

#[cfg(test)]
pub(crate) use self::cargo_metadata::{Dependency, Target};
Expand Down
47 changes: 47 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,38 @@ pub fn parse_rustc_version<S: AsRef<str>>(version: S) -> Result<String> {
))
}

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(),
))
}

/// Picks the correct "rustdoc.css" static file depending on which rustdoc version was used to
/// generate this version of this crate.
pub fn get_correct_docsrs_style_file(version: &str) -> Result<String> {
let date = parse_rustc_date(version)?;
// This is the date where https://github.com/rust-lang/rust/pull/91356 was merged.
if Utc.ymd(2021, 12, 6) < date {
// If this is the new rustdoc layout, we need the newer docs.rs CSS file.
Ok("rustdoc-2021-12-06.css".to_owned())
} else {
// By default, we return the old docs.rs CSS file.
Ok("rustdoc.css".to_owned())
}
}

#[test]
fn test_parse_rustc_version() {
assert_eq!(
Expand All @@ -30,3 +64,16 @@ fn test_parse_rustc_version() {
"20160526-0.2.0-ba9ae23"
);
}

#[test]
fn test_get_correct_docsrs_style_file() {
assert_eq!(
get_correct_docsrs_style_file("rustc 1.10.0-nightly (57ef01513 2016-05-23)").unwrap(),
"rustdoc.css"
);
assert_eq!(
get_correct_docsrs_style_file("docsrs 0.2.0 (ba9ae23 2022-05-26)").unwrap(),
"rustdoc-2021-12-06.css"
);
assert!(get_correct_docsrs_style_file("docsrs 0.2.0").is_err(),);
}
32 changes: 16 additions & 16 deletions src/web/builds.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,14 +144,14 @@ mod tests {
.version("0.1.0")
.builds(vec![
FakeBuild::default()
.rustc_version("rustc 1.0.0")
.rustc_version("rustc (blabla 2019-01-01)")
.docsrs_version("docs.rs 1.0.0"),
FakeBuild::default()
.successful(false)
.rustc_version("rustc 2.0.0")
.rustc_version("rustc (blabla 2020-01-01)")
.docsrs_version("docs.rs 2.0.0"),
FakeBuild::default()
.rustc_version("rustc 3.0.0")
.rustc_version("rustc (blabla 2021-01-01)")
.docsrs_version("docs.rs 3.0.0"),
])
.create()?;
Expand All @@ -169,11 +169,11 @@ mod tests {
.map(|row| row.text_contents())
.collect();

assert!(rows[0].contains("rustc 3.0.0"));
assert!(rows[0].contains("rustc (blabla 2021-01-01)"));
assert!(rows[0].contains("docs.rs 3.0.0"));
assert!(rows[1].contains("rustc 2.0.0"));
assert!(rows[1].contains("rustc (blabla 2020-01-01)"));
assert!(rows[1].contains("docs.rs 2.0.0"));
assert!(rows[2].contains("rustc 1.0.0"));
assert!(rows[2].contains("rustc (blabla 2019-01-01)"));
assert!(rows[2].contains("docs.rs 1.0.0"));

Ok(())
Expand All @@ -188,14 +188,14 @@ mod tests {
.version("0.1.0")
.builds(vec![
FakeBuild::default()
.rustc_version("rustc 1.0.0")
.rustc_version("rustc (blabla 2019-01-01)")
.docsrs_version("docs.rs 1.0.0"),
FakeBuild::default()
.successful(false)
.rustc_version("rustc 2.0.0")
.rustc_version("rustc (blabla 2020-01-01)")
.docsrs_version("docs.rs 2.0.0"),
FakeBuild::default()
.rustc_version("rustc 3.0.0")
.rustc_version("rustc (blabla 2021-01-01)")
.docsrs_version("docs.rs 3.0.0"),
])
.create()?;
Expand All @@ -214,7 +214,7 @@ mod tests {
);
assert_eq!(
value.pointer("/0/rustc_version"),
Some(&"rustc 3.0.0".into())
Some(&"rustc (blabla 2021-01-01)".into())
);
assert!(value.pointer("/0/id").unwrap().is_i64());
assert!(serde_json::from_value::<DateTime<Utc>>(
Expand All @@ -229,7 +229,7 @@ mod tests {
);
assert_eq!(
value.pointer("/1/rustc_version"),
Some(&"rustc 2.0.0".into())
Some(&"rustc (blabla 2020-01-01)".into())
);
assert!(value.pointer("/1/id").unwrap().is_i64());
assert!(serde_json::from_value::<DateTime<Utc>>(
Expand All @@ -244,7 +244,7 @@ mod tests {
);
assert_eq!(
value.pointer("/2/rustc_version"),
Some(&"rustc 1.0.0".into())
Some(&"rustc (blabla 2019-01-01)".into())
);
assert!(value.pointer("/2/id").unwrap().is_i64());
assert!(serde_json::from_value::<DateTime<Utc>>(
Expand Down Expand Up @@ -317,15 +317,15 @@ mod tests {
.name("aquarelle")
.version("0.1.0")
.builds(vec![FakeBuild::default()
.rustc_version("rustc 1.0.0")
.rustc_version("rustc (blabla 2019-01-01)")
.docsrs_version("docs.rs 1.0.0")])
.create()?;

env.fake_release()
.name("aquarelle")
.version("0.2.0")
.builds(vec![FakeBuild::default()
.rustc_version("rustc 1.0.0")
.rustc_version("rustc (blabla 2019-01-01)")
.docsrs_version("docs.rs 1.0.0")])
.create()?;

Expand Down Expand Up @@ -363,7 +363,7 @@ mod tests {
.name("foo")
.version("0.1.0")
.builds(vec![FakeBuild::default()
.rustc_version("rustc 1.0.0")
.rustc_version("rustc (blabla 2019-01-01)")
.docsrs_version("docs.rs 1.0.0")])
.create()?;

Expand All @@ -382,7 +382,7 @@ mod tests {
.name("foo")
.version("0.1.0")
.builds(vec![FakeBuild::default()
.rustc_version("rustc 1.0.0")
.rustc_version("rustc (blabla 2019-01-01)")
.docsrs_version("docs.rs 1.0.0")])
.create()?;

Expand Down
3 changes: 3 additions & 0 deletions src/web/crate_details.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use super::{match_version, redirect_base, render_markdown, MatchSemver, MetaData};
use crate::utils::get_correct_docsrs_style_file;
use crate::{db::Pool, impl_webpage, repositories::RepositoryStatsUpdater, web::page::WebPage};
use chrono::{DateTime, Utc};
use iron::prelude::*;
Expand Down Expand Up @@ -114,6 +115,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 +161,7 @@ impl CrateDetails {
default_target: krate.get("default_target"),
doc_targets: MetaData::parse_doc_targets(krate.get("doc_targets")),
yanked: krate.get("yanked"),
rustdoc_css_file: get_correct_docsrs_style_file(krate.get("doc_rustc_version"))?,
};

let documented_items: Option<i32> = krate.get("documented_items");
Expand Down
13 changes: 12 additions & 1 deletion src/web/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
pub(crate) mod page;

use crate::utils::get_correct_docsrs_style_file;
use crate::utils::report_error;
use anyhow::{anyhow, Context as _};
use log::info;
Expand Down Expand Up @@ -534,6 +535,9 @@ pub(crate) struct MetaData {
pub(crate) default_target: String,
pub(crate) doc_targets: Vec<String>,
pub(crate) yanked: bool,
/// CSS file to use depending on the rustdoc version used to generate this version of this
/// crate.
pub(crate) rustdoc_css_file: String,
}

impl MetaData {
Expand All @@ -552,7 +556,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 +577,7 @@ impl MetaData {
default_target: row.get(5),
doc_targets: MetaData::parse_doc_targets(row.get(6)),
yanked: row.get(7),
rustdoc_css_file: get_correct_docsrs_style_file(row.get(8)).unwrap(),
})
}

Expand Down Expand Up @@ -927,6 +933,7 @@ mod test {
"arm64-unknown-linux-gnu".to_string(),
],
yanked: false,
rustdoc_css_file: "rustdoc.css".to_string(),
};

let correct_json = json!({
Expand All @@ -942,6 +949,7 @@ mod test {
"arm64-unknown-linux-gnu",
],
"yanked": false,
"rustdoc_css_file": "rustdoc.css",
});

assert_eq!(correct_json, serde_json::to_value(&metadata).unwrap());
Expand All @@ -960,6 +968,7 @@ mod test {
"arm64-unknown-linux-gnu",
],
"yanked": false,
"rustdoc_css_file": "rustdoc.css",
});

assert_eq!(correct_json, serde_json::to_value(&metadata).unwrap());
Expand All @@ -978,6 +987,7 @@ mod test {
"arm64-unknown-linux-gnu",
],
"yanked": false,
"rustdoc_css_file": "rustdoc.css",
});

assert_eq!(correct_json, serde_json::to_value(&metadata).unwrap());
Expand All @@ -1001,6 +1011,7 @@ mod test {
default_target: "x86_64-unknown-linux-gnu".to_string(),
doc_targets: vec![],
yanked: false,
rustdoc_css_file: "rustdoc.css".to_string(),
},
);
Ok(())
Expand Down
5 changes: 4 additions & 1 deletion src/web/source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
use crate::{
db::Pool,
impl_webpage,
utils::get_correct_docsrs_style_file,
web::{
error::Nope, file::File as DbFile, match_version, page::WebPage, redirect_base,
MatchSemver, MetaData, Url,
Expand Down Expand Up @@ -67,7 +68,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 +149,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_css_file: get_correct_docsrs_style_file(rows[0].get(9)).unwrap(),
},
files: file_list,
})
Expand Down
8 changes: 7 additions & 1 deletion 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 anyhow::Context;
use chrono::Utc;
use chrono::prelude::*;
use iron::{
headers::CacheDirective,
headers::{CacheControl, ContentLength, ContentType, LastModified},
Expand All @@ -14,6 +14,8 @@ 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_2021_12_06_CSS: &str =
include_str!(concat!(env!("OUT_DIR"), "/rustdoc-2021-12-06.css"));
const STATIC_SEARCH_PATHS: &[&str] = &["static", "vendor"];

pub(crate) fn static_handler(req: &mut Request) -> IronResult<Response> {
Expand All @@ -25,6 +27,10 @@ pub(crate) fn static_handler(req: &mut Request) -> IronResult<Response> {
"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-2021-12-06.css" => serve_resource(
RUSTDOC_2021_12_06_CSS,
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/{{metadata.rustdoc_css_file}}?{{ docsrs_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-2021-12-06.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;
}
}
Loading

0 comments on commit 88e2435

Please sign in to comment.