From 52aeff65a82d949dd3ce33d1a5998e00ad4c379e Mon Sep 17 00:00:00 2001 From: alyx Date: Tue, 2 Apr 2024 11:51:14 -0400 Subject: Theming refactor Theming has been broken off into a seperate space, so that it'll be easier to add lua support later. --- src/theming/hbs.rs | 35 +++++++++++++++++++++++++++++++++++ src/theming/hbs/plain.hbs | 39 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 74 insertions(+) create mode 100644 src/theming/hbs.rs create mode 100644 src/theming/hbs/plain.hbs (limited to 'src/theming') diff --git a/src/theming/hbs.rs b/src/theming/hbs.rs new file mode 100644 index 0000000..73c72ae --- /dev/null +++ b/src/theming/hbs.rs @@ -0,0 +1,35 @@ +use std::sync::LazyLock; + +use handlebars::*; +use reqwest::StatusCode; + +use crate::CONFIG; + +static INTERNAL_THEMES: &[(&str, &str)] = &[("plain", include_str!("hbs/plain.hbs"))]; + +static HANDLEBARS: LazyLock = LazyLock::new(|| { + let mut hb = Handlebars::new(); + handlebars_helper!(url_encode: |s: String| urlencoding::encode(&s)); + + hb.register_helper("url-encode", Box::new(url_encode)); + + for (key, fulltext) in INTERNAL_THEMES { + log::info!("Registering internal theme `{key}`"); + hb.register_template_string(key, fulltext).unwrap(); + } + hb.set_dev_mode(CONFIG.theme_debug); + + if let Some(themes_dir) = CONFIG.theme_dir.as_ref() { + log::info!("Registering theme dir `{themes_dir}`"); + hb.register_templates_directory(&CONFIG.theme_ext, themes_dir.as_ref()).unwrap(); + } + + hb +}); + +pub fn render_theme(name: &str, ctx: &crate::ctx::Ctx) -> Option> { + let templ = HANDLEBARS.get_template(name)?; + let ctx = Context::wraps(ctx).unwrap(); + let render = templ.renders(&HANDLEBARS, &ctx, &mut RenderContext::new(Some(&name.into()))).map_err(|_| StatusCode::INTERNAL_SERVER_ERROR); + Some(render) +} diff --git a/src/theming/hbs/plain.hbs b/src/theming/hbs/plain.hbs new file mode 100644 index 0000000..975d6c5 --- /dev/null +++ b/src/theming/hbs/plain.hbs @@ -0,0 +1,39 @@ + + + + + {{#if error}}Error!{{else}}@{{user.name}}'s Last.fm Stats{{/if}} + + + + + {{#if error}} +

{{error}}

+ {{else}} + +

@{{user.name}}{{#if scrobble.now_playing}} + is scrobbling{{else}}'s last scrobble was + {{/if}} +

+ {{scrobble.name}} +

+ {{#if scrobble.album}}from {{scrobble.album}}{{/if}} +

+ by {{scrobble.artist.name}}. +

+ {{/if}} + + -- cgit v1.2.3-70-g09d2