diff --git a/crates/treehouse/src/cli/generate.rs b/crates/treehouse/src/cli/generate.rs index bdd437a..fa368d4 100644 --- a/crates/treehouse/src/cli/generate.rs +++ b/crates/treehouse/src/cli/generate.rs @@ -251,6 +251,8 @@ pub fn generate(paths: &Paths<'_>) -> anyhow::Result { 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"))?; + config.autopopulate_pics(&paths.static_dir.join("pic"))?; + dbg!(&config.pics); info!("cleaning target directory"); let _ = std::fs::remove_dir_all(paths.target_dir); diff --git a/crates/treehouse/src/cli/mod.rs b/crates/treehouse/src/cli/mod.rs index a723752..5855beb 100644 --- a/crates/treehouse/src/cli/mod.rs +++ b/crates/treehouse/src/cli/mod.rs @@ -36,6 +36,9 @@ pub enum Command { #[clap(flatten)] serve: ServeArgs, }, + + /// Generates a new ulid and prints it to stdout. + Ulid, } #[derive(Args)] diff --git a/crates/treehouse/src/config.rs b/crates/treehouse/src/config.rs index f559e2c..f37a52c 100644 --- a/crates/treehouse/src/config.rs +++ b/crates/treehouse/src/config.rs @@ -22,6 +22,12 @@ pub struct Config { /// On top of this, emojis are autodiscovered by walking the `static/emoji` directory. #[serde(default)] pub emoji: HashMap, + + /// Overrides for pic filenames. Useful for setting up aliases. + /// + /// On top of this, pics are autodiscovered by walking the `static/pic` directory. + /// Only the part before the first dash is treated as the pic's id. + pub pics: HashMap, } impl Config { @@ -56,4 +62,41 @@ impl Config { } Ok(()) } + + fn is_pic_file(path: &Path) -> bool { + path.extension() == Some(OsStr::new("png")) + || path.extension() == Some(OsStr::new("svg")) + || path.extension() == Some(OsStr::new("jpg")) + || path.extension() == Some(OsStr::new("jpeg")) + || path.extension() == Some(OsStr::new("webp")) + } + + pub fn autopopulate_pics(&mut self, dir: &Path) -> anyhow::Result<()> { + for file in WalkDir::new(dir) { + let entry = file?; + if entry.file_type().is_file() && Self::is_pic_file(entry.path()) { + if let Some(pic_name) = entry.path().file_stem() { + let pic_name = pic_name.to_string_lossy(); + + let pic_id = pic_name + .split_once('-') + .map(|(before_dash, _after_dash)| before_dash) + .unwrap_or(&pic_name); + + if !self.pics.contains_key(pic_id) { + self.pics.insert( + pic_id.to_owned(), + entry + .path() + .strip_prefix(dir) + .unwrap_or(entry.path()) + .to_string_lossy() + .into_owned(), + ); + } + } + } + } + Ok(()) + } } diff --git a/crates/treehouse/src/html/tree.rs b/crates/treehouse/src/html/tree.rs index 82154bc..83f8a62 100644 --- a/crates/treehouse/src/html/tree.rs +++ b/crates/treehouse/src/html/tree.rs @@ -103,6 +103,9 @@ pub fn branch_to_html( "".into(), ) }), + "pic" => config.pics.get(linked).map(|filename| { + (format!("/static/pic/{}", &filename).into(), "".into()) + }), _ => None, }) } else { diff --git a/crates/treehouse/src/main.rs b/crates/treehouse/src/main.rs index 4d58165..4c8b678 100644 --- a/crates/treehouse/src/main.rs +++ b/crates/treehouse/src/main.rs @@ -46,6 +46,14 @@ async fn fallible_main() -> anyhow::Result<()> { Command::Fix(fix_args) => fix_file_cli(fix_args)?, Command::FixAll(fix_args) => fix_all_cli(fix_args, &paths)?, + + Command::Ulid => { + let mut rng = rand::thread_rng(); + let ulid = ulid::Generator::new() + .generate_with_source(&mut rng) + .expect("failed to generate ulid"); + println!("{ulid}"); + } } Ok(()) diff --git a/treehouse.toml b/treehouse.toml index 88dee30..759475e 100644 --- a/treehouse.toml +++ b/treehouse.toml @@ -22,3 +22,5 @@ description = "a place on the Internet I like to call home" "article/function_coloring" = "https://journal.stuffwithstuff.com/2015/02/01/what-color-is-your-function/" [emoji] + +[pics]