Factor choice of theme into Layout struct

This commit is contained in:
Magnus Hoff 2018-06-16 14:30:18 +02:00
parent 7e6fe36ea0
commit 0a48ff2a54
11 changed files with 10 additions and 19 deletions

View file

@ -85,7 +85,6 @@ impl Resource for AboutResource {
.with_body(Layout {
base: None, // Hmm, should perhaps accept `base` as argument
title: "About Sausagewiki",
theme: "blue-gray",
body: &Template {
deps: &*LICENSE_INFOS
},

View file

@ -96,18 +96,12 @@ impl Resource for ArticleResource {
.map(|x| x.expect("Data model guarantees that this exists"));
let head = self.head();
use rand::{thread_rng, Rng};
const themes: [&str; 19] = ["red", "pink", "purple", "deep-purple", "indigo", "blue", "light-blue", "cyan", "teal", "green", "light-green", "lime", "yellow", "amber", "orange", "deep-orange", "brown", "gray", "blue-gray"];
let theme = thread_rng().choose(&themes).unwrap();
Box::new(data.join(head)
.and_then(move |(data, head)| {
Ok(head
.with_body(Layout {
base: None, // Hmm, should perhaps accept `base` as argument
title: &data.title,
theme,
body: &Template {
revision: data.revision,
last_updated: Some(&last_updated(
@ -244,7 +238,6 @@ impl Resource for ArticleResource {
.with_body(Layout {
base: None,
title: &title,
theme: "blue-gray",
body: &Template {
revision: base_article.revision,
last_updated: Some(&last_updated(

View file

@ -95,7 +95,6 @@ impl Resource for ArticleRevisionResource {
.with_body(Layout {
base: Some("../../"), // Hmm, should perhaps accept `base` as argument
title: &data.title,
theme: "blue-gray",
body: &Template {
link_current: &format!("_by_id/{}", data.article_id),
timestamp_and_author: &timestamp_and_author(

View file

@ -356,7 +356,6 @@ impl Resource for ChangesResource {
.with_body(Layout {
base: None, // Hmm, should perhaps accept `base` as argument
title: "Changes",
theme: "blue-gray",
body: &Template {
resource: &self,
show_authors: self.show_authors,

View file

@ -122,7 +122,6 @@ impl Resource for DiffResource {
.with_body(Layout {
base: Some("../"), // Hmm, should perhaps accept `base` as argument
title: "Difference",
theme: "blue-gray",
body: &Template {
consecutive: self.to.revision - self.from.revision == 1,
article_id: self.from.article_id as u32,

View file

@ -48,7 +48,6 @@ impl Resource for HtmlResource {
.with_body(Layout {
base: self.base,
title: self.title,
theme: "blue-gray",
body: &Template {
title: self.title,
html_body: self.html_body,

View file

@ -83,7 +83,6 @@ impl Resource for NewArticleResource {
.with_body(Layout {
base: None, // Hmm, should perhaps accept `base` as argument
title: &title,
theme: "blue-gray",
body: &Template {
revision: NEW,
last_updated: None,

View file

@ -202,7 +202,6 @@ impl Resource for SearchResource {
.with_body(Layout {
base: None, // Hmm, should perhaps accept `base` as argument
title: "Search",
theme: "blue-gray",
body: &Template {
query: self.query.as_ref().map(|x| &**x).unwrap_or(""),
hits: &data.iter()

View file

@ -48,7 +48,6 @@ impl Resource for SitemapResource {
.with_body(Layout {
base: None, // Hmm, should perhaps accept `base` as argument
title: "Sitemap",
theme: "blue-gray",
body: &Template {
articles: &articles,
},

View file

@ -14,6 +14,10 @@ use build_config;
use web::Lookup;
use wiki_lookup::WikiLookup;
const THEMES: [&str; 19] = ["red", "pink", "purple", "deep-purple", "indigo",
"blue", "light-blue", "cyan", "teal", "green", "light-green", "lime",
"yellow", "amber", "orange", "deep-orange", "brown", "gray", "blue-gray"];
lazy_static! {
static ref TEXT_HTML: mime::Mime = "text/html;charset=utf-8".parse().unwrap();
static ref SERVER: Server =
@ -27,11 +31,15 @@ header! { (XIdentity, "X-Identity") => [String] }
pub struct Layout<'a, T: 'a + fmt::Display> {
pub base: Option<&'a str>,
pub title: &'a str,
pub theme: &'a str,
pub body: &'a T,
}
impl<'a, T: 'a + fmt::Display> Layout<'a, T> {
pub fn theme(&self) -> &str {
use rand::{thread_rng, Rng};
thread_rng().choose(&THEMES).unwrap()
}
pub fn style_css_checksum(&self) -> &str { StyleCss::checksum() }
pub fn search_js_checksum(&self) -> &str { SearchJs::checksum() }
@ -63,7 +71,6 @@ impl Site {
.with_body(Layout {
base: base,
title: "Not found",
theme: "blue-gray",
body: &NotFound,
}.to_string())
.with_status(hyper::StatusCode::NotFound)
@ -77,7 +84,6 @@ impl Site {
.with_body(Layout {
base,
title: "Internal server error",
theme: "blue-gray",
body: &InternalServerError,
}.to_string())
.with_status(hyper::StatusCode::InternalServerError)

View file

@ -8,7 +8,7 @@
<link href="_assets/style-{{style_css_checksum()}}.css" rel="stylesheet">
<meta name="generator" content="{{project_name()}} {{version()}}" />
</head>
<body class="theme-{{theme}}">
<body class="theme-{{theme()}}">
{{>search_input.html}}
{{{body}}}
</body>