From 904bfd2a0bd05b3150cacb64ca5b9d9b78c062e7 Mon Sep 17 00:00:00 2001 From: Magnus Hoff Date: Thu, 19 Oct 2017 13:26:54 +0200 Subject: [PATCH] Restructure footer. Less irrelevant information. Refactor rendering of "last updated" --- assets/script.js | 4 +-- src/resources/article_resource.rs | 36 +++++++++++++++++++-------- src/resources/new_article_resource.rs | 16 ++++++------ templates/article_revision.html | 17 +++---------- templates/footer/items.html | 12 ++++----- 5 files changed, 44 insertions(+), 41 deletions(-) diff --git a/assets/script.js b/assets/script.js index 706cced..b1710f9 100644 --- a/assets/script.js +++ b/assets/script.js @@ -67,9 +67,7 @@ function openEditor() { // Update url-bar, page title and footer window.history.replaceState(null, result.title, result.slug == "" ? "." : result.slug); document.querySelector("title").textContent = result.title; - if (result.article_id != null) articleId.textContent = result.article_id; - revision.textContent = result.revision; - lastUpdated.textContent = result.created; + lastUpdated.textContent = result.last_updated; // Update body: rendered.innerHTML = result.rendered; diff --git a/src/resources/article_resource.rs b/src/resources/article_resource.rs index 7663e71..1d99135 100644 --- a/src/resources/article_resource.rs +++ b/src/resources/article_resource.rs @@ -1,3 +1,4 @@ +use chrono::{TimeZone, DateTime, Local}; use futures::{self, Future}; use hyper; use hyper::header::ContentType; @@ -25,6 +26,20 @@ impl ArticleResource { } } +pub fn last_updated(created: &DateTime, author: Option<&str>) -> String { + #[derive(BartDisplay)] + #[template_string = "Last updated {{created}}{{#author}} by {{.}}{{/author}}"] + struct Template<'a> { + created: &'a str, + author: Option<&'a str>, + } + + Template { + created: &created.to_rfc2822(), + author + }.to_string() +} + impl Resource for ArticleResource { fn allow(&self) -> Vec { use hyper::Method::*; @@ -39,15 +54,11 @@ impl Resource for ArticleResource { } fn get(self: Box) -> ResponseFuture { - use chrono::{TimeZone, Local}; - #[derive(BartDisplay)] #[template="templates/article_revision.html"] struct Template<'a> { - article_id: i32, revision: i32, - created: &'a str, - author: Option<&'a str>, + last_updated: &'a str, edit: bool, cancel_url: Option<&'a str>, @@ -69,10 +80,11 @@ impl Resource for ArticleResource { base: None, // Hmm, should perhaps accept `base` as argument title: &data.title, body: &Template { - article_id: data.article_id, revision: data.revision, - created: &Local.from_utc_datetime(&data.created).to_rfc2822(), - author: data.author.as_ref().map(|x| &**x), + last_updated: &last_updated( + &Local.from_utc_datetime(&data.created), + data.author.as_ref().map(|x| &**x) + ), edit: self.edit, cancel_url: Some(&data.slug), title: &data.title, @@ -88,7 +100,6 @@ impl Resource for ArticleResource { fn put(self: Box, body: hyper::Body, identity: Option) -> ResponseFuture { // TODO Check incoming Content-Type - use chrono::{TimeZone, Local}; use futures::Stream; #[derive(Deserialize)] @@ -111,7 +122,7 @@ impl Resource for ArticleResource { revision: i32, title: &'a str, rendered: &'a str, - created: &'a str, + last_updated: &'a str, } Box::new(body @@ -136,7 +147,10 @@ impl Resource for ArticleResource { title: &updated.title, rendered: render_markdown(&updated.body), }.to_string(), - created: &Local.from_utc_datetime(&updated.created).to_rfc2822(), + last_updated: &last_updated( + &Local.from_utc_datetime(&updated.created), + updated.author.as_ref().map(|x| &**x) + ), }).expect("Should never fail")) ) }) diff --git a/src/resources/new_article_resource.rs b/src/resources/new_article_resource.rs index ba209e9..f007506 100644 --- a/src/resources/new_article_resource.rs +++ b/src/resources/new_article_resource.rs @@ -52,10 +52,8 @@ impl Resource for NewArticleResource { #[derive(BartDisplay)] #[template="templates/article_revision.html"] struct Template<'a> { - article_id: &'a str, revision: &'a str, - created: &'a str, - author: Option<&'a str>, + last_updated: &'a str, edit: bool, cancel_url: Option<&'a str>, @@ -76,10 +74,8 @@ impl Resource for NewArticleResource { base: None, // Hmm, should perhaps accept `base` as argument title: &title, body: &Template { - article_id: NDASH, revision: NDASH, - created: NDASH, - author: None, + last_updated: NDASH, // Implicitly start in edit-mode when no slug is given. This // currently directly corresponds to the /_new endpoint @@ -98,6 +94,7 @@ impl Resource for NewArticleResource { fn put(self: Box, body: hyper::Body, identity: Option) -> ResponseFuture { // TODO Check incoming Content-Type + // TODO Refactor. Reduce duplication with ArticleResource::put use chrono::{TimeZone, Local}; use futures::Stream; @@ -123,7 +120,7 @@ impl Resource for NewArticleResource { revision: i32, title: &'a str, rendered: &'a str, - created: &'a str, + last_updated: &'a str, } Box::new(body @@ -152,7 +149,10 @@ impl Resource for NewArticleResource { title: &updated.title, rendered: render_markdown(&updated.body), }.to_string(), - created: &Local.from_utc_datetime(&updated.created).to_string(), + last_updated: &super::article_resource::last_updated( + &Local.from_utc_datetime(&updated.created), + updated.author.as_ref().map(|x| &**x) + ), }).expect("Should never fail")) ) }) diff --git a/templates/article_revision.html b/templates/article_revision.html index fe8fa6a..a67726a 100644 --- a/templates/article_revision.html +++ b/templates/article_revision.html @@ -32,18 +32,9 @@
- -
-
Article ID
-
{{article_id}}
- -
Revision
-
{{revision}}
- -
Last updated
-
{{created}}{{#author}} by {{.}}{{/author}}
-
+
  • {{last_updated}}
  • Edit
{{>footer/items.html}}
diff --git a/templates/footer/items.html b/templates/footer/items.html index 491d4b1..2f779aa 100644 --- a/templates/footer/items.html +++ b/templates/footer/items.html @@ -1,7 +1,7 @@ - +

Powered by Sausagewiki