From 74f7ba968a008dca0f9d3895bc2a36393e591339 Mon Sep 17 00:00:00 2001 From: slonkazoid Date: Wed, 14 Aug 2024 14:00:52 +0300 Subject: [PATCH] add toggles for dates ("grr its too complicated") --- CONFIG.md | 10 ++++++++-- partials/post_table.hbs | 8 ++++---- partials/span_date.hbs | 2 +- src/app.rs | 19 +++++++++---------- src/config.rs | 35 +++++++++++++++++++++++++++++------ src/templates/mod.rs | 8 ++++---- templates/index.hbs | 8 ++++---- templates/post.hbs | 4 ++-- 8 files changed, 61 insertions(+), 33 deletions(-) diff --git a/CONFIG.md b/CONFIG.md index 071dde9..551a621 100644 --- a/CONFIG.md +++ b/CONFIG.md @@ -8,7 +8,9 @@ title = "bingus-blog" # title of the blog description = "blazingly fast markdown blog software written in rust memory safe" markdown_access = true # allow users to see the raw markdown of a post # endpoint: /posts/.md -js_enable = true # enable javascript (required for below 2 options) +js_enable = true # enable javascript (required for sorting and dates) + +[style] date_format = "RFC3339" # format string used to format dates in the backend # it's highly recommended to leave this as default, # so the date can be formatted by the browser. @@ -16,6 +18,10 @@ date_format = "RFC3339" # format string used to format dates in the backend default_sort = "date" # default sorting method ("date" or "name") #default_color = "#f5c2e7" # default embed color, optional +[style.display_dates] +creation = true # display creation ("written") dates +modification = true # display modified ("last modified") dates + [rss] enable = false # serve an rss field under /feed.xml # this may be a bit resource intensive @@ -54,7 +60,7 @@ a default value you don't have to copy the whole thing from here, it's generated by the program if it doesn't exist -## Specifying Configuration +## Specifying the configuration file the configuration file is loaded from `config.toml` by default, but the path can be overriden by setting the environment variable `BINGUS_BLOG_CONFIG`, diff --git a/partials/post_table.hbs b/partials/post_table.hbs index 8655ca6..419b364 100644 --- a/partials/post_table.hbs +++ b/partials/post_table.hbs @@ -1,11 +1,11 @@
- {{#if (ne this.created_at null)}} + {{#if (and (ne this.created_at null) style.display_dates.creation)}}
written
-
{{>span_date date_time=this.created_at}}
+
{{>span_date dt=this.created_at df=style.date_format}}
{{/if}} - {{#if (ne this.modified_at null)}} + {{#if (and (ne this.modified_at null) style.display_dates.modification)}}
last modified
-
{{>span_date date_time=this.modified_at}}
+
{{>span_date dt=this.modified_at df=style.date_format}}
{{/if}} {{#if (gt (len this.tags) 0)}}
tags
diff --git a/partials/span_date.hbs b/partials/span_date.hbs index a8c92f5..f7079f4 100644 --- a/partials/span_date.hbs +++ b/partials/span_date.hbs @@ -1 +1 @@ -{{date date_time df}} +{{date dt df}} diff --git a/src/app.rs b/src/app.rs index b957fd6..5d8bcfd 100644 --- a/src/app.rs +++ b/src/app.rs @@ -19,7 +19,7 @@ use tower_http::services::ServeDir; use tower_http::trace::TraceLayer; use tracing::{info, info_span, Span}; -use crate::config::{Config, DateFormat, Sort}; +use crate::config::{Config, StyleConfig}; use crate::error::{AppError, AppResult}; use crate::post::{MarkdownPosts, PostManager, PostMetadata, RenderStats, ReturnedPost}; use crate::serve_dir_included::handle; @@ -40,12 +40,10 @@ struct IndexTemplate<'a> { description: &'a str, posts: Vec, rss: bool, - df: &'a DateFormat, js: bool, - color: Option<&'a str>, - sort: Sort, tags: Map, joined_tags: String, + style: &'a StyleConfig, } #[derive(Serialize)] @@ -54,10 +52,10 @@ struct PostTemplate<'a> { rendered: String, rendered_in: RenderStats, markdown_access: bool, - df: &'a DateFormat, js: bool, color: Option<&'a str>, joined_tags: String, + style: &'a StyleConfig, } #[derive(Deserialize)] @@ -128,12 +126,10 @@ async fn index<'a>( description: &config.description, posts, rss: config.rss.enable, - df: &config.date_format, js: config.js_enable, - color: config.default_color.as_deref(), - sort: config.default_sort, tags, joined_tags, + style: &config.style, }, ); drop(reg); @@ -228,10 +224,13 @@ async fn post( rendered, rendered_in, markdown_access: config.markdown_access, - df: &config.date_format, js: config.js_enable, - color: meta.color.as_deref().or(config.default_color.as_deref()), + color: meta + .color + .as_deref() + .or(config.style.default_color.as_deref()), joined_tags, + style: &config.style, }, ); drop(reg); diff --git a/src/config.rs b/src/config.rs index 3fc6eb3..6ba5bf0 100644 --- a/src/config.rs +++ b/src/config.rs @@ -76,6 +76,23 @@ pub enum Sort { Name, } +#[derive(Serialize, Deserialize, Debug, Clone)] +#[serde(default)] +#[derive(Default)] +pub struct StyleConfig { + pub display_dates: DisplayDates, + pub date_format: DateFormat, + pub default_sort: Sort, + pub default_color: Option, +} + +#[derive(Serialize, Deserialize, Debug, Clone)] +#[serde(default)] +pub struct DisplayDates { + pub creation: bool, + pub modification: bool, +} + #[derive(Serialize, Deserialize, Debug, Clone)] #[serde(default)] pub struct Config { @@ -83,9 +100,7 @@ pub struct Config { pub description: String, pub markdown_access: bool, pub js_enable: bool, - pub date_format: DateFormat, - pub default_sort: Sort, - pub default_color: Option, + pub style: StyleConfig, pub rss: RssConfig, pub dirs: DirsConfig, pub http: HttpConfig, @@ -100,9 +115,7 @@ impl Default for Config { description: "blazingly fast markdown blog software written in rust memory safe".into(), markdown_access: true, js_enable: true, - date_format: Default::default(), - default_sort: Default::default(), - default_color: None, + style: Default::default(), // i have a love-hate relationship with serde // it was engimatic at first, but then i started actually using it // writing my own serialize and deserialize implementations.. spending @@ -121,6 +134,16 @@ impl Default for Config { } } +impl Default for DisplayDates { + fn default() -> Self { + Self { + creation: true, + modification: true, + } + } +} + + impl Default for DirsConfig { fn default() -> Self { Self { diff --git a/src/templates/mod.rs b/src/templates/mod.rs index 3fe61f3..9063940 100644 --- a/src/templates/mod.rs +++ b/src/templates/mod.rs @@ -29,7 +29,7 @@ fn is_ext(path: impl AsRef, ext: &str) -> bool { } } -pub(self) fn get_template_name<'a>(path: &'a Path) -> Option<&'a str> { + fn get_template_name(path: &Path) -> Option<&str> { if !is_ext(path, "hbs") { return None; } @@ -47,10 +47,10 @@ fn register_included_file( Ok(()) } -fn register_path<'a>( +fn register_path( path: impl AsRef, name: &str, - registry: &mut Handlebars<'a>, + registry: &mut Handlebars<'_>, ) -> Result<(), TemplateError> { let template = compile_path(path)?; registry.register_template(name, template); @@ -85,7 +85,7 @@ fn compile_path(path: impl AsRef) -> Result, ) -> Result { use tokio::fs::OpenOptions; diff --git a/templates/index.hbs b/templates/index.hbs index 549c547..1862c8a 100644 --- a/templates/index.hbs +++ b/templates/index.hbs @@ -7,7 +7,7 @@ {{#if (ne color null)}} - + {{/if}} {{title}} @@ -30,9 +30,9 @@ {{/if}} @@ -43,7 +43,7 @@
{{description}}
- {{>post_table post df=@root.df}} + {{>post_table post style=@root.style}}
{{else}} there are no posts right now. check back later! {{/each}} diff --git a/templates/post.hbs b/templates/post.hbs index 81788de..c1bd109 100644 --- a/templates/post.hbs +++ b/templates/post.hbs @@ -21,7 +21,7 @@ {{/if}}{{/if}} {{#if (ne color null)}} - + {{/if}} {{meta.title}} @@ -41,7 +41,7 @@

{{meta.description}}

- {{>post_table meta df=@root.df}} + {{>post_table meta style=@root.style}} link
back to home