From 707b2da0d39371c8b0d274ee4a0495f81006e34d Mon Sep 17 00:00:00 2001 From: lemonsh Date: Fri, 4 Feb 2022 15:24:32 +0100 Subject: [PATCH] Move to Tera templating engine for better customizability --- .gitignore | 12 ++++++------ Cargo.toml | 2 +- src/database.rs | 7 +------ src/web_service.rs | 13 ++++--------- .../quote_tmpl.hbs => templates/quotes.html | 18 +++++++++--------- 5 files changed, 21 insertions(+), 31 deletions(-) rename src/res/quote_tmpl.hbs => templates/quotes.html (81%) diff --git a/.gitignore b/.gitignore index 1bdef75..cdedca6 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,6 @@ -/target -uberbot_*.toml -uberbot.toml -/Cargo.lock -.idea -*.db3 +/target +uberbot_*.toml +uberbot.toml +/Cargo.lock +.idea +*.db3 diff --git a/Cargo.toml b/Cargo.toml index 8b6e4e8..00c37cb 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -27,7 +27,7 @@ rusqlite = { version = "0.26", features = ["bundled"] } warp = "0.3" futures-util = "0.3" irc = { version = "0.15", default-features = false } -handlebars = "4.2" +tera = { version = "1.15", default-features = false } [features] tls = ["irc/tls-rust"] diff --git a/src/database.rs b/src/database.rs index 5fd42ba..bf288ef 100644 --- a/src/database.rs +++ b/src/database.rs @@ -136,11 +136,6 @@ impl ExecutorConnection { Option, author: Option ); - executor_wrapper!( - search, - Task::Search, - Option>, - query: String - ); + executor_wrapper!(search, Task::Search, Option>, query: String); executor_wrapper!(random20, Task::Random20, Option>); } diff --git a/src/web_service.rs b/src/web_service.rs index ab042e2..f18510b 100644 --- a/src/web_service.rs +++ b/src/web_service.rs @@ -1,6 +1,5 @@ use crate::database::Quote; use crate::ExecutorConnection; -use handlebars::Handlebars; use irc::client::Client; use lazy_static::lazy_static; use reqwest::StatusCode; @@ -8,16 +7,12 @@ use serde::{Deserialize, Serialize}; use serde_json::Value::Null; use std::net::SocketAddr; use std::sync::Arc; +use tera::{Context, Tera}; use tokio::sync::broadcast::Receiver; use warp::{reply, Filter, Reply}; lazy_static! { - static ref HANDLEBARS: Handlebars<'static> = { - let mut reg = Handlebars::new(); - reg.register_template_string("quotes", include_str!("res/quote_tmpl.hbs")) - .unwrap(); - reg - }; + static ref TERA: Tera = Tera::new("templates/**/*").unwrap(); } pub async fn run( @@ -58,7 +53,7 @@ struct QuotesTemplate { #[derive(Deserialize)] struct QuotesQuery { - q: Option + q: Option, } async fn handle_get_quote(query: QuotesQuery, db: ExecutorConnection) -> impl Reply { @@ -84,7 +79,7 @@ async fn handle_get_quote(query: QuotesQuery, db: ExecutorConnection) -> impl Re flash: Some("Displaying up to 20 random quotes".into()), } }; - match HANDLEBARS.render("quotes", &template) { + match TERA.render("quotes.html", &Context::from_serialize(&template).unwrap()) { Ok(o) => reply::html(o).into_response(), Err(e) => { tracing::warn!("Error while rendering template: {}", e); diff --git a/src/res/quote_tmpl.hbs b/templates/quotes.html similarity index 81% rename from src/res/quote_tmpl.hbs rename to templates/quotes.html index 6c19baf..1238708 100644 --- a/src/res/quote_tmpl.hbs +++ b/templates/quotes.html @@ -23,10 +23,10 @@ - {{#if flash}} -

{{flash}}

- {{/if}} - {{#if quotes}} + {% if flash %} +

{{ flash }}

+ {% endif %} + {% if quotes %} @@ -35,15 +35,15 @@ - {{#each quotes}} + {% for q in quotes %} - - + + - {{/each}} + {% endfor %}
{{author}}{{quote}}{{ q.author }}{{ q.quote }}
- {{/if}} + {% endif %} \ No newline at end of file