From e41763a22932a46f4b779edf9ae168a54754fa76 Mon Sep 17 00:00:00 2001 From: Bauke Date: Sun, 9 Oct 2022 19:29:43 +0200 Subject: [PATCH] Add a way to add extra HTML to the head and body at runtime. --- source/cli/run.rs | 1 + source/templates/base.html | 2 ++ source/templates/mod.rs | 21 +++++++++++++++++++-- 3 files changed, 22 insertions(+), 2 deletions(-) diff --git a/source/cli/run.rs b/source/cli/run.rs index 9793d9d..0e09651 100644 --- a/source/cli/run.rs +++ b/source/cli/run.rs @@ -110,6 +110,7 @@ pub async fn run() -> Result<()> { groups, user_count_group.as_ref().map(|group| group.subscribers), ) + .await .render_to_file(&output) .await?; generate_css(&output).await?; diff --git a/source/templates/base.html b/source/templates/base.html index e2b6571..d507fc4 100644 --- a/source/templates/base.html +++ b/source/templates/base.html @@ -10,10 +10,12 @@ {% block head %}{% endblock %} + {{ extra_head_html|safe }} {% block body %}{% endblock %} + {{ extra_body_html|safe }} diff --git a/source/templates/mod.rs b/source/templates/mod.rs index 235edf1..1a7d63a 100644 --- a/source/templates/mod.rs +++ b/source/templates/mod.rs @@ -2,7 +2,10 @@ use { askama::Template, - async_std::{fs::write, path::PathBuf}, + async_std::{ + fs::{read_to_string, write}, + path::PathBuf, + }, chrono::NaiveDate, color_eyre::Result, }; @@ -13,6 +16,12 @@ use crate::{group_data::GroupDataModel, utilities::today}; #[derive(Template)] #[template(path = "index.html")] pub struct HomeTemplate { + /// Extra HTML to insert in the body. + pub extra_body_html: String, + + /// Extra HTML to insert in the head. + pub extra_head_html: String, + /// The groups to create the table with. pub groups: Vec, @@ -28,8 +37,16 @@ pub struct HomeTemplate { impl HomeTemplate { /// Create a new [`HomeTemplate`]. - pub fn new(groups: Vec, user_count: Option) -> Self { + pub async fn new( + groups: Vec, + user_count: Option, + ) -> Self { + let extra_body_html = read_to_string("extra-body.html").await; + let extra_head_html = read_to_string("extra-head.html").await; + Self { + extra_body_html: extra_body_html.unwrap_or_default(), + extra_head_html: extra_head_html.unwrap_or_default(), groups, page_title: "Tildes Statistics".to_string(), today: today(),