diff --git a/src/errors.rs b/src/errors.rs index c4c2be198..05642d0a8 100644 --- a/src/errors.rs +++ b/src/errors.rs @@ -28,6 +28,7 @@ use actix_web::http::StatusCode; // Modules with foreign errors: use actix; use actix_web; +use askama; use diesel; use r2d2; use reqwest; @@ -50,6 +51,9 @@ pub enum ConverseError { #[fail(display = "a template rendering error occured: {}", reason)] Template { reason: String }, + #[fail(display = "a template rendering error occured: {}", reason)] + Askama { reason: String }, + #[fail(display = "error occured during request handling: {}", error)] ActixWeb { error: actix_web::Error }, @@ -88,6 +92,14 @@ impl From for ConverseError { } } +impl From for ConverseError { + fn from(error: askama::Error) -> ConverseError { + ConverseError::Askama { + reason: format!("{}", error), + } + } +} + impl From for ConverseError { fn from(error: actix::MailboxError) -> ConverseError { ConverseError::Actix { error: Box::new(error) } diff --git a/src/main.rs b/src/main.rs index 4db4ab5cc..c68b75229 100644 --- a/src/main.rs +++ b/src/main.rs @@ -16,18 +16,21 @@ // License along with this program. If not, see // . +#[macro_use] +extern crate askama; + #[macro_use] extern crate diesel; +#[macro_use] +extern crate failure; + #[macro_use] extern crate log; #[macro_use] extern crate serde_derive; -#[macro_use] -extern crate failure; - extern crate actix; extern crate actix_web; extern crate chrono; @@ -137,7 +140,6 @@ fn start_renderer() -> Addr { // location-dependent. // Drawback is that template changes require recompilation ... tera.add_raw_templates(vec![ - ("index.html", include_str!("../templates/index.html")), ("post.html", include_str!("../templates/post.html")), ("search.html", include_str!("../templates/search.html")), ("thread.html", include_str!("../templates/thread.html")), diff --git a/src/render.rs b/src/render.rs index 63a89d99e..ff8bd3ab8 100644 --- a/src/render.rs +++ b/src/render.rs @@ -21,10 +21,12 @@ //! them. use actix::prelude::*; +use askama::Template; use errors::*; +use std::fmt; use md5; use models::*; -use tera::{escape_html, Context, Tera}; +use tera::{escape_html, Tera}; use chrono::prelude::{DateTime, Utc}; use comrak::{ComrakOptions, markdown_to_html}; @@ -39,11 +41,11 @@ impl Actor for Renderer { /// Represents a data formatted for human consumption #[derive(Debug, Serialize)] -struct FormattedDate(String); +struct FormattedDate(DateTime); -impl From> for FormattedDate { - fn from(date: DateTime) -> Self { - FormattedDate(format!("{}", date.format("%a %d %B %Y, %R"))) +impl fmt::Display for FormattedDate { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + write!(f, "{}", self.0.format("%a %d %B %Y, %R")) } } @@ -63,6 +65,12 @@ struct IndexThread { post_author: String, } +#[derive(Template)] +#[template(path = "index.html")] +struct IndexPageTemplate { + threads: Vec, +} + impl Handler for Renderer { type Result = Result; @@ -71,17 +79,19 @@ impl Handler for Renderer { .into_iter() .map(|thread| IndexThread { id: thread.thread_id, - title: escape_html(&thread.title), + title: thread.title, // escape_html(&thread.title), sticky: thread.sticky, - posted: thread.posted.into(), + posted: FormattedDate(thread.posted), author_name: thread.thread_author, post_author: thread.post_author, }) .collect(); - let mut ctx = Context::new(); - ctx.add("threads", &threads); - Ok(self.tera.render("index.html", &ctx)?) + let tpl = IndexPageTemplate { + threads + }; + + tpl.render().map_err(|e| e.into()) } } @@ -98,7 +108,7 @@ message!(ThreadPage, Result); struct RenderablePost { id: i32, body: String, - posted: FormattedDate, + posted: String, // FormattedDate, author_name: String, author_gravatar: String, editable: bool, @@ -127,7 +137,7 @@ fn prepare_thread(comrak: &ComrakOptions, page: ThreadPage) -> RenderableThreadP RenderablePost { id: post.id, body: markdown_to_html(&post.body, comrak), - posted: post.posted.into(), + posted: format!("{}", FormattedDate(post.posted)), // post.posted.into(), author_name: post.author_name.clone(), author_gravatar: md5_hex(post.author_email.as_bytes()), editable, diff --git a/templates/index.html b/templates/index.html index c10493899..bff734bda 100644 --- a/templates/index.html +++ b/templates/index.html @@ -41,21 +41,21 @@