just some random stuff

This commit is contained in:
りき萌 2023-08-27 18:47:57 +02:00
parent d794e88bdc
commit e43d612e3d
5 changed files with 63 additions and 2 deletions

View file

@ -219,6 +219,7 @@ pub fn regenerate(paths: &Paths<'_>) -> anyhow::Result<()> {
info!("loading config");
let mut config = Config::load(paths.config_file)?;
config.site = std::env::var("TREEHOUSE_SITE").unwrap_or(config.site);
config.autopopulate_emoji(&paths.static_dir.join("emoji"))?;
info!("cleaning target directory");
let _ = std::fs::remove_dir_all(paths.target_dir);

View file

@ -1,7 +1,8 @@
use std::{collections::HashMap, path::Path};
use std::{collections::HashMap, ffi::OsStr, path::Path};
use anyhow::Context;
use serde::{Deserialize, Serialize};
use walkdir::WalkDir;
#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct Config {
@ -28,4 +29,31 @@ impl Config {
let string = std::fs::read_to_string(path).context("cannot read config file")?;
toml_edit::de::from_str(&string).context("error in config file")
}
fn is_emoji_file(path: &Path) -> bool {
path.extension() == Some(OsStr::new("png")) || path.extension() == Some(OsStr::new("svg"))
}
pub fn autopopulate_emoji(&mut self, dir: &Path) -> anyhow::Result<()> {
for file in WalkDir::new(dir) {
let entry = file?;
if entry.file_type().is_file() && Self::is_emoji_file(entry.path()) {
if let Some(emoji_name) = entry.path().file_stem() {
let emoji_name = emoji_name.to_string_lossy();
if !self.emoji.contains_key(emoji_name.as_ref()) {
self.emoji.insert(
emoji_name.into_owned(),
entry
.path()
.strip_prefix(dir)
.unwrap_or(entry.path())
.to_string_lossy()
.into_owned(),
);
}
}
}
}
Ok(())
}
}