diff --git a/source/cli/run.rs b/source/cli/run.rs index 71ba333..97cd021 100644 --- a/source/cli/run.rs +++ b/source/cli/run.rs @@ -19,7 +19,7 @@ use crate::{ migrations::Migrator, scss::generate_css, snapshots::SnapshotModel, - templates::HomeTemplate, + templates::{GroupTemplate, HomeTemplate}, utilities::{create_db, today}, }; @@ -117,6 +117,11 @@ pub async fn run() -> Result<()> { } .render(&output, &group.name, true) .await?; + + GroupTemplate::new(&group.name) + .await + .render_to_file(&output) + .await?; } HomeTemplate::new( diff --git a/source/templates/group.html b/source/templates/group.html new file mode 100644 index 0000000..b85e1f1 --- /dev/null +++ b/source/templates/group.html @@ -0,0 +1,19 @@ +{% extends "base.html" %} + +{% block head %} + +{% endblock %} + +{% block body %} + + +
+

General

+ + {{ group_name }} User Count Chart +
+{% endblock %} diff --git a/source/templates/index.html b/source/templates/index.html index 7cc8eb2..801786a 100644 --- a/source/templates/index.html +++ b/source/templates/index.html @@ -37,7 +37,7 @@ {% for group in groups %} - {{ group.name }} + {{ group.name }} {{ group.subscribers }} diff --git a/source/templates/mod.rs b/source/templates/mod.rs index f797fd9..6dd750d 100644 --- a/source/templates/mod.rs +++ b/source/templates/mod.rs @@ -10,6 +10,8 @@ use { color_eyre::Result, }; +use async_std::fs::create_dir_all; + use crate::{ group_data::GroupDataModel, utilities::{get_base_url, today}, @@ -70,3 +72,52 @@ impl HomeTemplate { Ok(()) } } + +/// The template for group-specific pages. +#[derive(Template)] +#[template(path = "group.html")] +pub struct GroupTemplate { + /// The base URL for links to the Tildes instance. + pub base_url: String, + + /// 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 group name for this group. + pub group_name: String, + + /// The string for the `` element. + pub page_title: String, + + /// The date of today's snapshot. + pub today: NaiveDate, +} + +impl GroupTemplate { + /// Create a new [`GroupTemplate`]. + pub async fn new(group_name: &str) -> Self { + let extra_body_html = read_to_string("extra-body.html").await; + let extra_head_html = read_to_string("extra-head.html").await; + + Self { + base_url: get_base_url(), + extra_body_html: extra_body_html.unwrap_or_default(), + extra_head_html: extra_head_html.unwrap_or_default(), + group_name: group_name.to_string(), + page_title: "Tildes Statistics".to_string(), + today: today(), + } + } + + /// Render the template and write it to file. + pub async fn render_to_file(&self, parent: &PathBuf) -> Result<()> { + let output_dir = parent.join(&self.group_name); + create_dir_all(&output_dir).await?; + write(output_dir.join("index.html"), self.render()?).await?; + + Ok(()) + } +}