From e4629d8edb5187bd3b018244c490815d3d4f7ccb Mon Sep 17 00:00:00 2001 From: Magnus Hoff Date: Sun, 17 Jun 2018 10:41:18 +0200 Subject: [PATCH] Refactor rendering of pages to centralize header layout. Convert all pages to new layout --- src/resources/about_resource.rs | 17 +++--- src/resources/article_revision_resource.rs | 58 +++++++++---------- src/resources/changes_resource.rs | 25 ++++---- src/resources/html_resource.rs | 23 ++------ src/resources/search_resource.rs | 27 +++++---- src/resources/sitemap_resource.rs | 17 +++--- src/site.rs | 42 ++++++++++---- templates/about.html | 12 ---- templates/article_revision.html | 11 +--- templates/changes.html | 10 ---- templates/diff.html | 10 ++-- templates/error/404.html | 12 +--- templates/error/500.html | 12 +--- templates/search.html | 11 ---- templates/sitemap.html | 10 ---- .../{simple.html => system_page_layout.html} | 2 + 16 files changed, 117 insertions(+), 182 deletions(-) rename templates/{simple.html => system_page_layout.html} (83%) diff --git a/src/resources/about_resource.rs b/src/resources/about_resource.rs index b6e8975..711cfcd 100644 --- a/src/resources/about_resource.rs +++ b/src/resources/about_resource.rs @@ -5,7 +5,7 @@ use hyper::server::*; use build_config; use mimes::*; -use site::Layout; +use site::system_page; use web::{Resource, ResponseFuture}; #[derive(Licenses)] @@ -81,14 +81,13 @@ impl Resource for AboutResource { Box::new(head .and_then(move |head| { - Ok(head - .with_body(Layout { - base: None, // Hmm, should perhaps accept `base` as argument - title: "About Sausagewiki", - body: &Template { - deps: &*LICENSE_INFOS - }, - }.to_string())) + Ok(head.with_body(system_page( + None, // Hmm, should perhaps accept `base` as argument + "About Sausagewiki", + Template { + deps: &*LICENSE_INFOS + }, + ).to_string())) })) } } diff --git a/src/resources/article_revision_resource.rs b/src/resources/article_revision_resource.rs index 862cbac..1e5da96 100644 --- a/src/resources/article_revision_resource.rs +++ b/src/resources/article_revision_resource.rs @@ -7,7 +7,7 @@ use hyper::server::*; use mimes::*; use models; use rendering::render_markdown; -use site::Layout; +use site::system_page; use web::{Resource, ResponseFuture}; use super::changes_resource::QueryParameters; @@ -81,8 +81,6 @@ impl Resource for ArticleRevisionResource { link_current: &'a str, timestamp_and_author: &'a str, diff_link: Option, - - title: &'a str, rendered: String, } @@ -91,34 +89,32 @@ impl Resource for ArticleRevisionResource { Box::new(head .and_then(move |head| - Ok(head - .with_body(Layout { - base: Some("../../"), // Hmm, should perhaps accept `base` as argument - title: &data.title, - body: &Template { - link_current: &format!("_by_id/{}", data.article_id), - timestamp_and_author: ×tamp_and_author( - data.sequence_number, - data.article_id, - &Local.from_utc_datetime(&data.created), - data.author.as_ref().map(|x| &**x) - ), - diff_link: - if data.revision > 1 { - Some(format!("_diff/{}?{}", - data.article_id, - diff_resource::QueryParameters::new( - data.revision as u32 - 1, - data.revision as u32, - ) - )) - } else { - None - }, - title: &data.title, - rendered: render_markdown(&data.body), - }, - }.to_string())) + Ok(head.with_body(system_page( + Some("../../"), // Hmm, should perhaps accept `base` as argument + &data.title, + &Template { + link_current: &format!("_by_id/{}", data.article_id), + timestamp_and_author: ×tamp_and_author( + data.sequence_number, + data.article_id, + &Local.from_utc_datetime(&data.created), + data.author.as_ref().map(|x| &**x) + ), + diff_link: + if data.revision > 1 { + Some(format!("_diff/{}?{}", + data.article_id, + diff_resource::QueryParameters::new( + data.revision as u32 - 1, + data.revision as u32, + ) + )) + } else { + None + }, + rendered: render_markdown(&data.body), + }, + ).to_string())) )) } } diff --git a/src/resources/changes_resource.rs b/src/resources/changes_resource.rs index cfd8f94..af677af 100644 --- a/src/resources/changes_resource.rs +++ b/src/resources/changes_resource.rs @@ -8,7 +8,7 @@ use serde_urlencoded; use mimes::*; use schema::article_revisions; -use site::Layout; +use site::system_page; use state::State; use web::{Resource, ResponseFuture}; @@ -352,18 +352,17 @@ impl Resource for ChangesResource { } }).collect::>(); - Ok(head - .with_body(Layout { - base: None, // Hmm, should perhaps accept `base` as argument - title: "Changes", - body: &Template { - resource: &self, - show_authors: self.show_authors, - newer, - older, - changes - }, - }.to_string())) + Ok(head.with_body(system_page( + None, // Hmm, should perhaps accept `base` as argument + "Changes", + Template { + resource: &self, + show_authors: self.show_authors, + newer, + older, + changes + } + ).to_string())) })) } } diff --git a/src/resources/html_resource.rs b/src/resources/html_resource.rs index c07efef..b1fcaa9 100644 --- a/src/resources/html_resource.rs +++ b/src/resources/html_resource.rs @@ -4,7 +4,7 @@ use hyper::header::ContentType; use hyper::server::*; use mimes::*; -use site::Layout; +use site::system_page; use web::{Resource, ResponseFuture}; pub struct HtmlResource { @@ -19,13 +19,6 @@ impl HtmlResource { } } -#[derive(BartDisplay)] -#[template="templates/simple.html"] -struct Template<'a> { - title: &'a str, - html_body: &'a str, -} - impl Resource for HtmlResource { fn allow(&self) -> Vec { use hyper::Method::*; @@ -44,15 +37,11 @@ impl Resource for HtmlResource { Box::new(head .and_then(move |head| { - Ok(head - .with_body(Layout { - base: self.base, - title: self.title, - body: &Template { - title: self.title, - html_body: self.html_body, - }, - }.to_string())) + Ok(head.with_body(system_page( + self.base, + self.title, + self.html_body + ).to_string())) })) } } diff --git a/src/resources/search_resource.rs b/src/resources/search_resource.rs index 32bdcc7..3ce024f 100644 --- a/src/resources/search_resource.rs +++ b/src/resources/search_resource.rs @@ -7,7 +7,7 @@ use serde_urlencoded; use mimes::*; use models::SearchResult; -use site::Layout; +use site::system_page; use state::State; use web::{Resource, ResponseFuture}; @@ -198,19 +198,18 @@ impl Resource for SearchResource { next, }).expect("Should never fail")) ), - &ResponseType::Html => Ok(head - .with_body(Layout { - base: None, // Hmm, should perhaps accept `base` as argument - title: "Search", - body: &Template { - query: self.query.as_ref().map(|x| &**x).unwrap_or(""), - hits: &data.iter() - .enumerate() - .collect::>(), - prev, - next, - }, - }.to_string())), + &ResponseType::Html => Ok(head.with_body(system_page( + None, // Hmm, should perhaps accept `base` as argument + "Search", + &Template { + query: self.query.as_ref().map(|x| &**x).unwrap_or(""), + hits: &data.iter() + .enumerate() + .collect::>(), + prev, + next, + }, + ).to_string())), } })) } diff --git a/src/resources/sitemap_resource.rs b/src/resources/sitemap_resource.rs index 48cdffc..448b633 100644 --- a/src/resources/sitemap_resource.rs +++ b/src/resources/sitemap_resource.rs @@ -5,7 +5,7 @@ use hyper::server::*; use mimes::*; use models::ArticleRevisionStub; -use site::Layout; +use site::system_page; use state::State; use web::{Resource, ResponseFuture}; @@ -44,14 +44,13 @@ impl Resource for SitemapResource { Box::new(data.join(head) .and_then(move |(articles, head)| { - Ok(head - .with_body(Layout { - base: None, // Hmm, should perhaps accept `base` as argument - title: "Sitemap", - body: &Template { - articles: &articles, - }, - }.to_string())) + Ok(head.with_body(system_page( + None, // Hmm, should perhaps accept `base` as argument + "Sitemap", + Template { + articles: &articles, + }, + ).to_string())) })) } } diff --git a/src/site.rs b/src/site.rs index 8ea1203..8285c05 100644 --- a/src/site.rs +++ b/src/site.rs @@ -31,7 +31,7 @@ header! { (XIdentity, "X-Identity") => [String] } pub struct Layout<'a, T: 'a + fmt::Display> { pub base: Option<&'a str>, pub title: &'a str, - pub body: &'a T, + pub body: T, } impl<'a, T: 'a + fmt::Display> Layout<'a, T> { @@ -47,6 +47,28 @@ impl<'a, T: 'a + fmt::Display> Layout<'a, T> { pub fn version(&self) -> &str { build_config::VERSION.as_str() } } +#[derive(BartDisplay)] +#[template="templates/system_page_layout.html"] +pub struct SystemPageLayout<'a, T: 'a + fmt::Display> { + title: &'a str, + html_body: T, +} + +pub fn system_page<'a, T>(base: Option<&'a str>, title: &'a str, body: T) + -> Layout<'a, SystemPageLayout<'a, T>> +where + T: 'a + fmt::Display +{ + Layout { + base, + title, + body: SystemPageLayout { + title, + html_body: body, + }, + } +} + #[derive(BartDisplay)] #[template = "templates/error/404.html"] struct NotFound; @@ -68,11 +90,11 @@ impl Site { fn not_found(base: Option<&str>) -> Response { Response::new() .with_header(ContentType(TEXT_HTML.clone())) - .with_body(Layout { - base: base, - title: "Not found", - body: &NotFound, - }.to_string()) + .with_body(system_page( + base, + "Not found", + NotFound, + ).to_string()) .with_status(hyper::StatusCode::NotFound) } @@ -81,11 +103,11 @@ impl Site { Response::new() .with_header(ContentType(TEXT_HTML.clone())) - .with_body(Layout { + .with_body(system_page( base, - title: "Internal server error", - body: &InternalServerError, - }.to_string()) + "Internal server error", + InternalServerError, + ).to_string()) .with_status(hyper::StatusCode::InternalServerError) } } diff --git a/templates/about.html b/templates/about.html index 64a185e..d65b90c 100644 --- a/templates/about.html +++ b/templates/about.html @@ -1,11 +1,3 @@ -
-
-
-

About Sausagewiki

-
-
- -

This site is running Sausagewiki, a simple, self-contained wiki engine, version {{version()}}.

Copyright © 2017 Magnus Hovland Hoff.

@@ -41,7 +33,3 @@ copyright holders and distributed under various licenses: {{/deps}} -
-
- -{{>footer/default.html}} diff --git a/templates/article_revision.html b/templates/article_revision.html index 498a0d9..ee11deb 100644 --- a/templates/article_revision.html +++ b/templates/article_revision.html @@ -1,5 +1,3 @@ -
-

You are viewing an historical version of this article, @@ -11,11 +9,4 @@

-
-{{>article_contents.html}} -
-
- -
-{{>footer/items.html}} -
+{{{rendered}}} diff --git a/templates/changes.html b/templates/changes.html index e4d3f4c..2e27fb6 100644 --- a/templates/changes.html +++ b/templates/changes.html @@ -1,9 +1,3 @@ -
-
-

Changes

-
- -

These are the {{^newer}}most recent{{/newer}} changes made to{{{subject_clause()}}}{{#author()}} by {{.}}{{/author()}}. @@ -44,7 +38,3 @@ >

  • First changes
  • {{/older}} {{#changes?}}{{^older}}

    There are no older changes.

    {{/older}}{{/changes}} -
    -
    - -{{>footer/default.html}} diff --git a/templates/diff.html b/templates/diff.html index d450bdb..b62958d 100644 --- a/templates/diff.html +++ b/templates/diff.html @@ -1,5 +1,11 @@
    +
    +
    +

    {{#title}}{{#.removed}}{{.}}{{/.removed}}{{#.same}}{{.}}{{/.same}}{{#.added}}{{.}}{{/.added}}{{/title}}

    +
    +
    +

    You are viewing the difference between two {{#consecutive?}}consecutive{{/consecutive}} @@ -12,10 +18,6 @@

    -
    -

    {{#title}}{{#.removed}}{{.}}{{/.removed}}{{#.same}}{{.}}{{/.same}}{{#.added}}{{.}}{{/.added}}{{/title}}

    -
    -
    {{#lines}}{{#.removed}}{{.}}
     {{/.removed}}{{#.same}}{{.}}
    diff --git a/templates/error/404.html b/templates/error/404.html
    index 89ae856..fd2795d 100644
    --- a/templates/error/404.html
    +++ b/templates/error/404.html
    @@ -1,11 +1 @@
    -
    -
    -

    Not found

    -
    - -
    -

    This page was not found.

    -
    -
    - -{{>../footer/default.html}} +

    This page was not found.

    diff --git a/templates/error/500.html b/templates/error/500.html index 7626729..454e294 100644 --- a/templates/error/500.html +++ b/templates/error/500.html @@ -1,11 +1 @@ -
    -
    -

    Internal server error

    -
    - -
    -

    An error has occurred.

    -
    -
    - -{{>../footer/default.html}} +

    An error has occurred.

    diff --git a/templates/search.html b/templates/search.html index 4fbd9fc..aa5edee 100644 --- a/templates/search.html +++ b/templates/search.html @@ -1,9 +1,3 @@ -
    -
    -

    Search

    -
    - -
    {{#hits?}}

    Search results for the query {{query}}:

    @@ -25,8 +19,3 @@ {{^hits?}}

    Your search for {{query}} gave no results.

    {{/hits}} - -
    -
    - -{{>footer/default.html}} diff --git a/templates/sitemap.html b/templates/sitemap.html index c127416..9149e57 100644 --- a/templates/sitemap.html +++ b/templates/sitemap.html @@ -1,15 +1,5 @@ -
    -
    -

    Sitemap

    -
    - - -
    - -{{>footer/default.html}} diff --git a/templates/simple.html b/templates/system_page_layout.html similarity index 83% rename from templates/simple.html rename to templates/system_page_layout.html index f508bbb..78936e1 100644 --- a/templates/simple.html +++ b/templates/system_page_layout.html @@ -1,7 +1,9 @@
    +

    {{title}}

    +
    {{{html_body}}}