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(-)
@@ -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"
@@ -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
@@ -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 {
pub max_repo_desc_length: usize,
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)
}
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;
@@ -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)),
)
@@ -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>