Theming (#624)
* Theming - Custom CSS for blogs - Custom themes for instance - New dark theme - UI to choose your instance theme - Option to disable blog themes if you prefer to only have the instance theme - UI to choose a blog theme
This commit is contained in:
@@ -2,7 +2,7 @@ extern crate rsass;
|
||||
extern crate ructe;
|
||||
use ructe::*;
|
||||
use std::process::{Command, Stdio};
|
||||
use std::{env, fs::*, io::Write, path::PathBuf};
|
||||
use std::{env, ffi::OsStr, fs::*, io::Write, path::*};
|
||||
|
||||
fn compute_static_hash() -> String {
|
||||
//"find static/ -type f ! -path 'static/media/*' | sort | xargs stat -c'%n %Y' | openssl dgst -r"
|
||||
@@ -41,21 +41,12 @@ fn main() {
|
||||
let in_dir = PathBuf::from(env::var("CARGO_MANIFEST_DIR").unwrap()).join("templates");
|
||||
compile_templates(&in_dir, &out_dir).expect("compile templates");
|
||||
|
||||
println!("cargo:rerun-if-changed=static/css/_article.scss");
|
||||
println!("cargo:rerun-if-changed=static/css/_forms.scss");
|
||||
println!("cargo:rerun-if-changed=static/css/_global.scss");
|
||||
println!("cargo:rerun-if-changed=static/css/_header.scss");
|
||||
println!("cargo:rerun-if-changed=static/css/_variables.scss");
|
||||
println!("cargo:rerun-if-changed=static/css/main.scss");
|
||||
let mut out = File::create("static/css/main.css").expect("Couldn't create main.css");
|
||||
out.write_all(
|
||||
&rsass::compile_scss_file(
|
||||
"static/css/main.scss".as_ref(),
|
||||
rsass::OutputStyle::Compressed,
|
||||
)
|
||||
.expect("Error during SCSS compilation"),
|
||||
)
|
||||
.expect("Couldn't write CSS output");
|
||||
compile_themes().expect("Theme compilation error");
|
||||
recursive_copy(&Path::new("assets").join("icons"), &Path::new("static"))
|
||||
.expect("Couldn't copy icons");
|
||||
recursive_copy(&Path::new("assets").join("images"), &Path::new("static"))
|
||||
.expect("Couldn't copy images");
|
||||
create_dir_all(&Path::new("static").join("media")).expect("Couldn't init media directory");
|
||||
|
||||
let cache_id = &compute_static_hash()[..8];
|
||||
println!("cargo:rerun-if-changed=target/deploy/plume-front.wasm");
|
||||
@@ -74,3 +65,91 @@ fn main() {
|
||||
|
||||
println!("cargo:rustc-env=CACHE_ID={}", cache_id)
|
||||
}
|
||||
|
||||
fn compile_themes() -> std::io::Result<()> {
|
||||
let input_dir = Path::new("assets").join("themes");
|
||||
let output_dir = Path::new("static").join("css");
|
||||
|
||||
let themes = find_themes(input_dir)?;
|
||||
|
||||
for theme in themes {
|
||||
compile_theme(&theme, &output_dir)?;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn find_themes(path: PathBuf) -> std::io::Result<Vec<PathBuf>> {
|
||||
let ext = path.extension().and_then(OsStr::to_str);
|
||||
if metadata(&path)?.is_dir() {
|
||||
Ok(read_dir(&path)?.fold(vec![], |mut themes, ch| {
|
||||
if let Ok(ch) = ch {
|
||||
if let Ok(mut new) = find_themes(ch.path()) {
|
||||
themes.append(&mut new);
|
||||
}
|
||||
}
|
||||
themes
|
||||
}))
|
||||
} else if (ext == Some("scss") || ext == Some("sass"))
|
||||
&& !path.file_name().unwrap().to_str().unwrap().starts_with('_')
|
||||
{
|
||||
Ok(vec![path.clone()])
|
||||
} else {
|
||||
Ok(vec![])
|
||||
}
|
||||
}
|
||||
|
||||
fn compile_theme(path: &Path, out_dir: &Path) -> std::io::Result<()> {
|
||||
let name = path
|
||||
.components()
|
||||
.skip_while(|c| *c != Component::Normal(OsStr::new("themes")))
|
||||
.skip(1)
|
||||
.filter_map(|c| {
|
||||
c.as_os_str()
|
||||
.to_str()
|
||||
.unwrap_or_default()
|
||||
.splitn(2, '.')
|
||||
.next()
|
||||
})
|
||||
.collect::<Vec<_>>()
|
||||
.join("-");
|
||||
|
||||
let dir = path.parent().unwrap();
|
||||
|
||||
let out = out_dir.join(name);
|
||||
create_dir_all(&out)?;
|
||||
|
||||
// copy files of the theme that are not scss
|
||||
for ch in read_dir(&dir)? {
|
||||
recursive_copy(&ch?.path(), &out)?;
|
||||
}
|
||||
|
||||
// compile the .scss/.sass file
|
||||
let mut out = File::create(out.join("theme.css"))?;
|
||||
out.write_all(
|
||||
&rsass::compile_scss_file(path, rsass::OutputStyle::Compressed)
|
||||
.expect("SCSS compilation error"),
|
||||
)?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn recursive_copy(path: &Path, out_dir: &Path) -> std::io::Result<()> {
|
||||
if metadata(path)?.is_dir() {
|
||||
let out = out_dir.join(path.file_name().unwrap());
|
||||
create_dir_all(out.clone())?;
|
||||
|
||||
for ch in read_dir(path)? {
|
||||
recursive_copy(&ch?.path(), &out)?;
|
||||
}
|
||||
} else {
|
||||
println!("cargo:rerun-if-changed={}", path.display());
|
||||
|
||||
let ext = path.extension().and_then(OsStr::to_str);
|
||||
if ext != Some("scss") && ext != Some("sass") {
|
||||
copy(path, out_dir.join(path.file_name().unwrap()))?;
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user