add support for pictures

This commit is contained in:
liquidex 2024-02-07 11:24:54 +01:00
parent fb143b03c8
commit e974dd9839
6 changed files with 61 additions and 0 deletions

View file

@ -251,6 +251,8 @@ pub fn generate(paths: &Paths<'_>) -> anyhow::Result<Treehouse> {
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);

View file

@ -36,6 +36,9 @@ pub enum Command {
#[clap(flatten)]
serve: ServeArgs,
},
/// Generates a new ulid and prints it to stdout.
Ulid,
}
#[derive(Args)]

View file

@ -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<String, String>,
/// 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<String, String>,
}
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(())
}
}

View file

@ -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 {

View file

@ -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(())

View file

@ -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]