Skip to content

Split up the about page #924

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

Merged
merged 19 commits into from
Aug 6, 2020
Merged
Show file tree
Hide file tree
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
8 changes: 8 additions & 0 deletions build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,14 @@ fn main() {
env::var("TARGET").unwrap()
);

// Don't rerun anytime a single change is made
println!("cargo:rerun-if-changed=templates/style.scss");
println!("cargo:rerun-if-changed=templates/menu.js");
println!("cargo:rerun-if-changed=templates/index.js");
// TODO: are these right?
println!("cargo:rerun-if-changed=.git/HEAD");
println!("cargo:rerun-if-changed=.git/index");

write_git_version();
compile_sass();
copy_js();
Expand Down
2 changes: 1 addition & 1 deletion src/web/page/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ mod templates;
mod web_page;

pub(crate) use templates::TemplateData;
pub(crate) use web_page::WebPage;
pub(super) use web_page::WebPage;

use serde::Serialize;

Expand Down
14 changes: 10 additions & 4 deletions src/web/page/web_page.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,21 @@
use super::TemplateData;
use iron::{headers::ContentType, response::Response, status::Status, IronResult, Request};
use serde::Serialize;
use std::borrow::Cow;
use tera::Context;

/// When making using a custom status, use a closure that coerces to a `fn(&Self) -> Status`
#[macro_export]
macro_rules! impl_webpage {
($page:ty = $template:literal $(, status = $status:expr)? $(, content_type = $content_type:expr)? $(,)?) => {
impl_webpage!($page = |_| ::std::borrow::Cow::Borrowed($template) $(, status = $status)? $(, content_type = $content_type)?);
};
($page:ty = $template:expr $(, status = $status:expr)? $(, content_type = $content_type:expr)? $(,)?) => {
impl $crate::web::page::WebPage for $page {
const TEMPLATE: &'static str = $template;
fn template(&self) -> ::std::borrow::Cow<'static, str> {
let template: fn(&Self) -> ::std::borrow::Cow<'static, str> = $template;
template(self)
}

$(
fn get_status(&self) -> ::iron::status::Status {
Expand All @@ -32,14 +39,13 @@ pub trait WebPage: Serialize + Sized {
// TODO: We could cache similar pages using the `&Context`
fn into_response(self, req: &Request) -> IronResult<Response> {
let ctx = Context::from_serialize(&self).unwrap();

let rendered = req
.extensions
.get::<TemplateData>()
.expect("missing TemplateData from the request extensions")
.templates
.load()
.render(Self::TEMPLATE, &ctx)
.render(&self.template(), &ctx)
.unwrap();

let mut response = Response::with((self.get_status(), rendered));
Expand All @@ -49,7 +55,7 @@ pub trait WebPage: Serialize + Sized {
}

/// The name of the template to be rendered
const TEMPLATE: &'static str;
fn template(&self) -> Cow<'static, str>;

/// Gets the status of the request, defaults to `Ok`
fn get_status(&self) -> Status {
Expand Down
2 changes: 2 additions & 0 deletions src/web/routes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ pub(super) fn build_routes() -> Routes {

routes.internal_page("/about", super::sitemap::about_handler);
routes.internal_page("/about/metrics", super::metrics::metrics_handler);
routes.internal_page("/about/builds", super::sitemap::about_builds_handler);
routes.internal_page("/about/:subpage", super::sitemap::about_handler);

routes.internal_page("/releases", super::releases::recent_releases_handler);
routes.static_resource("/releases/feed", super::releases::releases_feed_handler);
Expand Down
59 changes: 55 additions & 4 deletions src/web/sitemap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,16 +56,18 @@ pub fn robots_txt_handler(_: &mut Request) -> IronResult<Response> {
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
struct About {
struct AboutBuilds {
/// The current version of rustc that docs.rs is using to build crates
rustc_version: Option<String>,
/// The default crate build limits
limits: Limits,
/// Just for the template, since this isn't shared with AboutPage
active_tab: &'static str,
}

impl_webpage!(About = "core/about.html");
impl_webpage!(AboutBuilds = "core/about/builds.html");

pub fn about_handler(req: &mut Request) -> IronResult<Response> {
pub fn about_builds_handler(req: &mut Request) -> IronResult<Response> {
let mut conn = extension!(req, Pool).get()?;
let res = ctry!(
req,
Expand All @@ -80,9 +82,45 @@ pub fn about_handler(req: &mut Request) -> IronResult<Response> {
}
});

About {
AboutBuilds {
rustc_version,
limits: Limits::default(),
active_tab: "builds",
}
.into_response(req)
}

#[derive(Serialize)]
struct AboutPage<'a> {
#[serde(skip)]
template: String,
active_tab: &'a str,
}

impl_webpage!(AboutPage<'_> = |this: &AboutPage| this.template.clone().into());

pub fn about_handler(req: &mut Request) -> IronResult<Response> {
use super::ErrorPage;
use iron::status::Status;

let name = match *req.url.path().last().expect("iron is broken") {
"about" | "index" => "index",
x @ "badges" | x @ "metadata" | x @ "redirections" => x,
_ => {
let msg = "This /about page does not exist. \
Perhaps you are interested in <a href=\"https://github.com/rust-lang/docs.rs/tree/master/templates/core/about\">creating</a> it?";
let page = ErrorPage {
title: "The requested page does not exist",
message: Some(msg.into()),
status: Status::NotFound,
};
return page.into_response(req);
}
};
let template = format!("core/about/{}.html", name);
AboutPage {
template,
active_tab: name,
}
.into_response(req)
}
Expand Down Expand Up @@ -110,6 +148,19 @@ mod tests {
fn about_page() {
wrapper(|env| {
let web = env.frontend();
for file in std::fs::read_dir("templates/core/about")? {
use std::ffi::OsStr;

let file_path = file?.path();
if file_path.extension() != Some(OsStr::new("html"))
|| file_path.file_stem() == Some(OsStr::new("index"))
{
continue;
}
let filename = file_path.file_stem().unwrap().to_str().unwrap();
let path = format!("/about/{}", filename);
assert_success(&path, web)?;
}
assert_success("/about", web)
})
}
Expand Down
23 changes: 23 additions & 0 deletions templates/about-base.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{% extends "base.html" %}

{% block header %}
<div class="cratesfyi-package-container">
<div class="container about">
<h3 id="crate-title">Docs.rs documentation</h3>
<div class="pure-menu pure-menu-horizontal">
<ul class="pure-menu-list">
{% set text = '<i class="fa fa-fw fa-info-circle"></i> <span class="title">About</span>' %}
{{ macros::active_link(expected="index", href="/about", text=text) }}
{% set text = '<i class="fa fa-fw fa-fonticons"></i> <span class="title">Badges</span>' %}
{{ macros::active_link(expected="badges", href="/about/badges", text=text) }}
{% set text = '<i class="fa fa-fw fa-cogs"></i> <span class="title">Builds</span>' %}
{{ macros::active_link(expected="builds", href="/about/builds", text=text) }}
{% set text = '<i class="fa fa-fw fa-table"></i> <span class="title">Metadata</span>' %}
{{ macros::active_link(expected="metadata", href="/about/metadata", text=text) }}
{% set text = '<i class="fa fa-fw fa-road"></i> <span class="title">Shorthand URLs</span>' %}
{{ macros::active_link(expected="redirections", href="/about/redirections", text=text) }}
</ul>
</div>
</div>
</div>
{% endblock %}
201 changes: 0 additions & 201 deletions templates/core/about.html

This file was deleted.

Loading