diff --git a/Cargo.toml b/Cargo.toml index 322d065..0ff9514 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -3,6 +3,7 @@ name = "bingus-blog" version = "0.1.0" edition = "2021" default-run = "bingus-blog" +repository = "https://git.slonk.ing/slonk/bingus-blog" [features] default = [] diff --git a/src/app.rs b/src/app.rs index 5d8bcfd..33e246e 100644 --- a/src/app.rs +++ b/src/app.rs @@ -5,7 +5,7 @@ use std::time::Duration; use axum::extract::{Path, Query, State}; use axum::http::header::CONTENT_TYPE; use axum::http::Request; -use axum::response::{IntoResponse, Redirect, Response}; +use axum::response::{Html, IntoResponse, Redirect, Response}; use axum::routing::get; use axum::{Json, Router}; use handlebars::Handlebars; @@ -26,6 +26,19 @@ use crate::serve_dir_included::handle; const STATIC: Dir<'static> = include_dir!("$CARGO_MANIFEST_DIR/static"); +#[derive(Serialize)] +pub struct BingusInfo { + pub name: &'static str, + pub version: &'static str, + pub repository: &'static str, +} + +const BINGUS_INFO: BingusInfo = BingusInfo { + name: env!("CARGO_PKG_NAME"), + version: env!("CARGO_PKG_VERSION"), + repository: env!("CARGO_PKG_REPOSITORY"), +}; + #[derive(Clone)] #[non_exhaustive] pub struct AppState { @@ -36,6 +49,7 @@ pub struct AppState { #[derive(Serialize)] struct IndexTemplate<'a> { + bingus_info: &'a BingusInfo, title: &'a str, description: &'a str, posts: Vec, @@ -48,6 +62,7 @@ struct IndexTemplate<'a> { #[derive(Serialize)] struct PostTemplate<'a> { + bingus_info: &'a BingusInfo, meta: &'a PostMetadata, rendered: String, rendered_in: RenderStats, @@ -124,6 +139,7 @@ async fn index<'a>( &IndexTemplate { title: &config.title, description: &config.description, + bingus_info: &BINGUS_INFO, posts, rss: config.rss.enable, js: config.js_enable, @@ -133,7 +149,7 @@ async fn index<'a>( }, ); drop(reg); - Ok(([(CONTENT_TYPE, "text/html")], rendered?)) + Ok(Html(rendered?)) } async fn all_posts( @@ -220,6 +236,7 @@ async fn post( let rendered = reg.render( "post", &PostTemplate { + bingus_info: &BINGUS_INFO, meta, rendered, rendered_in, @@ -234,7 +251,7 @@ async fn post( }, ); drop(reg); - Ok(([(CONTENT_TYPE, "text/html")], rendered?).into_response()) + Ok(Html(rendered?).into_response()) } ReturnedPost::Raw(body, content_type) => { Ok(([(CONTENT_TYPE, content_type)], body).into_response()) diff --git a/src/config.rs b/src/config.rs index 6ba5bf0..290e156 100644 --- a/src/config.rs +++ b/src/config.rs @@ -143,7 +143,6 @@ impl Default for DisplayDates { } } - impl Default for DirsConfig { fn default() -> Self { Self { @@ -194,7 +193,7 @@ pub async fn load() -> Result { "{}_CONFIG", env!("CARGO_BIN_NAME").to_uppercase().replace('-', "_") )) - .unwrap_or(String::from("config.toml")); + .unwrap_or_else(|_| String::from("config.toml")); match tokio::fs::OpenOptions::new() .read(true) .open(&config_file) diff --git a/src/post/markdown_posts.rs b/src/post/markdown_posts.rs index e4d0287..e43b33a 100644 --- a/src/post/markdown_posts.rs +++ b/src/post/markdown_posts.rs @@ -203,13 +203,7 @@ where if stat.is_file() && path.extension().is_some_and(|ext| ext == "md") { let mtime = as_secs(&stat.modified()?); - // TODO. this? - let name = path - .clone() - .file_stem() - .unwrap() - .to_string_lossy() - .to_string(); + let name = String::from(path.file_stem().unwrap().to_string_lossy()); if let Some(cache) = self.cache.as_ref() && let Some(hit) = cache.lookup_metadata(&name, mtime).await diff --git a/src/templates/mod.rs b/src/templates/mod.rs index 9063940..ccc7d56 100644 --- a/src/templates/mod.rs +++ b/src/templates/mod.rs @@ -8,7 +8,6 @@ use thiserror::Error; use tracing::{debug, error, info_span, trace}; const TEMPLATES: Dir<'static> = include_dir!("$CARGO_MANIFEST_DIR/templates"); -const PARTIALS: Dir<'static> = include_dir!("$CARGO_MANIFEST_DIR/partials"); #[derive(Error, Debug)] #[allow(clippy::enum_variant_names)] @@ -29,7 +28,7 @@ fn is_ext(path: impl AsRef, ext: &str) -> bool { } } - fn get_template_name(path: &Path) -> Option<&str> { +fn get_template_name(path: &Path) -> Option<&str> { if !is_ext(path, "hbs") { return None; } @@ -57,15 +56,6 @@ fn register_path( Ok(()) } -fn register_partial( - file: &include_dir::File<'_>, - name: &str, - registry: &mut Handlebars, -) -> Result<(), TemplateError> { - registry.register_partial(name, file.contents_utf8().ok_or(TemplateError::UTF8Error)?)?; - Ok(()) -} - fn compile_included_file(file: &include_dir::File<'_>) -> Result { let contents = file.contents_utf8().ok_or(TemplateError::UTF8Error)?; @@ -85,7 +75,7 @@ fn compile_path(path: impl AsRef) -> Result, ) -> Result { use tokio::fs::OpenOptions; @@ -125,29 +115,6 @@ pub fn new_registry<'a>(custom_templates_path: impl AsRef) -> io::Result file, - None => continue, - }; - - let span = info_span!("register_partial", path = ?file.path()); - let _handle = span.enter(); - - let name = match get_template_name(file.path()) { - Some(v) => v, - None => { - trace!("skipping file"); - continue; - } - }; - - match register_partial(file, name, &mut reg) { - Ok(()) => debug!("registered partial {name:?}"), - Err(err) => error!("error while registering partial: {err}"), - }; - } - let read_dir = match std::fs::read_dir(custom_templates_path) { Ok(v) => v, Err(err) => match err.kind() { diff --git a/templates/footer.hbs b/templates/footer.hbs new file mode 100644 index 0000000..d50a79c --- /dev/null +++ b/templates/footer.hbs @@ -0,0 +1,21 @@ +running {{bingus_info.name}} v{{bingus_info.version}} +{{#if rendered_in}} + - +{{/if}} +{{#each rendered_in}} + {{#if (eq @key "ParsedAndRendered")}} + parsed + and + rendered + in + {{duration this.0}} + + {{else if (eq @key "Cached")}} + retrieved from cache in + {{duration this}} + {{/if}} +{{/each}} +{{#if markdown_access}} + - + view raw +{{/if}} diff --git a/templates/index.hbs b/templates/index.hbs index 1862c8a..9f4c83f 100644 --- a/templates/index.hbs +++ b/templates/index.hbs @@ -22,8 +22,7 @@
-

{{title}}

-

{{description}}

+ {{>title}}

posts

{{#if js}} @@ -59,5 +58,8 @@
{{/each}}
+
+ {{>footer}} +
diff --git a/templates/post.hbs b/templates/post.hbs index c1bd109..41b99e2 100644 --- a/templates/post.hbs +++ b/templates/post.hbs @@ -49,23 +49,7 @@ {{{rendered}}}
- {{#each rendered_in}} - {{#if (eq @key "ParsedAndRendered")}} - parsed - and - rendered - in - {{duration this.[0]}} - - {{else if (eq @key "Cached")}} - retrieved from cache in - {{duration this}} - {{/if}} - {{/each}} - {{#if markdown_access}} - - - view raw - {{/if}} + {{>footer}}
diff --git a/partials/post_table.hbs b/templates/post_table.hbs similarity index 100% rename from partials/post_table.hbs rename to templates/post_table.hbs diff --git a/partials/span_date.hbs b/templates/span_date.hbs similarity index 100% rename from partials/span_date.hbs rename to templates/span_date.hbs diff --git a/templates/title.hbs b/templates/title.hbs new file mode 100644 index 0000000..75ad56e --- /dev/null +++ b/templates/title.hbs @@ -0,0 +1,2 @@ +

{{title}}

+

{{description}}