index : sparkle-git.git

ascending towards madness

author holly sparkles <sparkles@holly.sh> 2023-12-27 20:08:40.0 +00:00:00
committer holly sparkles <sparkles@holly.sh> 2024-05-14 6:40:47.0 +00:00:00
commit
0e5543fba92d8089e4705f3733bab8cc1330cb81 [patch]
tree
82d449b08afb758c79ab56563f904bb62944af48
parent
f453ce6bacc273b0244d9c03e3b7769f44510f18
download
0e5543fba92d8089e4705f3733bab8cc1330cb81.tar.gz

feat: add ability to use custom CSS



Diff

 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<String>,
}

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 @@
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title>{% block title %}✨ sparkle-git{% endblock %}</title>
    <link rel="stylesheet" type="text/css" href="/style-{{ crate::GLOBAL_CSS_HASH.as_ref() }}.css" />
	{% if config.css.clone().is_some() %}
	<link rel="stylesheet" type="text/css" href="/style-custom.css" />
	{% else %}
	<link rel="stylesheet" type="text/css" href="/style-{{ crate::GLOBAL_CSS_HASH.as_ref() }}.css" />
	{% endif %}
    {%- block head -%}{%- endblock %}
</head>