From 0e5543fba92d8089e4705f3733bab8cc1330cb81 Mon Sep 17 00:00:00 2001 From: holly sparkles Date: Wed, 27 Dec 2023 21:08:40 +0100 Subject: [PATCH] feat: add ability to use custom CSS --- Cargo.toml | 2 ++ rgit.conf.example | 4 +++- src/configuration/mod.rs | 39 ++++++++++++++++++++++++++++++++++++++- src/main.rs | 4 ++++ templates/base.html | 6 +++++- 5 files changed, 52 insertions(+), 3 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index c449f0d..d77931f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -54,6 +54,8 @@ httparse = "1.7" yoke = { version = "0.7.1", features = ["derive"] } rand = "0.8.5" confy = "0.5.1" +rsass = "0.28.0" + [build-dependencies] anyhow = "1.0" diff --git a/rgit.conf.example b/rgit.conf.example index a11bccd..3479f0b 100644 --- a/rgit.conf.example +++ b/rgit.conf.example @@ -31,4 +31,6 @@ ssh_clone_prefix = 'ssh://git.mydomain.tld:/srv/git' # Limit length of commit messages in the log to this; use 0 for no limit max_commit_message_length = 80 # Limit length of repository descriptions; use 0 for no limit -max_repo_desc_length = 80 \ No newline at end of file +max_repo_desc_length = 80 +# Use custom css on all pages +css = "/custom.scss" \ No newline at end of file diff --git a/src/configuration/mod.rs b/src/configuration/mod.rs index 2b2a60b..68be7c1 100644 --- a/src/configuration/mod.rs +++ b/src/configuration/mod.rs @@ -1,7 +1,11 @@ -use std::{path::Path, vec}; +use std::{ + path::{Path, PathBuf}, + vec, +}; use comrak::Options; use serde::{Deserialize, Serialize}; +use tracing::warn; #[derive(Debug, Clone, Serialize, Deserialize)] #[serde(default)] @@ -86,6 +90,13 @@ pub struct AppConfig { /// /// Default value: "0" pub max_repo_desc_length: usize, + + /// Specifies an absolute path on the filesystem to the css document to include in all pages. + /// + /// If set, the default stylesheet will be replaced with this one. + /// + /// Default value: `None` + pub css: Option, } impl ::std::default::Default for AppConfig { @@ -110,6 +121,7 @@ impl ::std::default::Default for AppConfig { ssh_clone_prefix: String::new(), max_commit_message_length: 0, max_repo_desc_length: 0, + css: None, } } } @@ -127,6 +139,31 @@ impl AppConfig { pub fn root_readme_is_valid(&self) -> bool { Path::new(&self.root_readme).try_exists().unwrap_or(false) } + + /// Loads a (s)css file from disk and compiles it using `rsass` + /// + /// if `Ok()`, returns the file contents. + /// If `Err()` or `None`, returns an empty slice. + pub fn get_css_data(&self) -> Box<[u8]> { + self.css + .as_ref() + .map(|file| { + let format = rsass::output::Format { + style: rsass::output::Style::Compressed, + ..rsass::output::Format::default() + }; + + rsass::compile_scss_path(&PathBuf::from(file), format).unwrap_or_else(|_| { + warn!( + "Unable to load or build css from path {:?}. Using defaults.", + file + ); + Vec::new() + }) + }) + .unwrap_or_else(|| Vec::new()) + .into_boxed_slice() + } } pub struct ReadmeConfig; diff --git a/src/main.rs b/src/main.rs index dd7cfe0..71eb480 100644 --- a/src/main.rs +++ b/src/main.rs @@ -210,6 +210,10 @@ async fn main() -> Result<(), anyhow::Error> { get(static_css(GLOBAL_CSS)), ) .route( + "/style-custom.css", + get(static_css(Box::leak(config.get_css_data()))), + ) + .route( &format!("/highlight-{}.css", HIGHLIGHT_CSS_HASH.get().unwrap()), get(static_css(css)), ) diff --git a/templates/base.html b/templates/base.html index 332e2b5..6387c8d 100644 --- a/templates/base.html +++ b/templates/base.html @@ -4,7 +4,11 @@ {% block title %}✨ sparkle-git{% endblock %} - + {% if config.css.clone().is_some() %} + + {% else %} + + {% endif %} {%- block head -%}{%- endblock %} -- libgit2 1.7.2