From f3aee8f41aed608c242cb1667927deb18083c380 Mon Sep 17 00:00:00 2001 From: liquidev Date: Fri, 19 Jul 2024 19:52:56 +0200 Subject: [PATCH] make emojis cached --- crates/treehouse/src/cli/generate.rs | 12 ++++++++---- crates/treehouse/src/config.rs | 11 +++++++---- crates/treehouse/src/html/markdown.rs | 9 ++++++--- crates/treehouse/src/html/tree.rs | 3 +++ crates/treehouse/src/main.rs | 1 + .../treehouse/src/{cli/generate => }/static_urls.rs | 0 6 files changed, 25 insertions(+), 11 deletions(-) rename crates/treehouse/src/{cli/generate => }/static_urls.rs (100%) diff --git a/crates/treehouse/src/cli/generate.rs b/crates/treehouse/src/cli/generate.rs index a37948f..3b61f95 100644 --- a/crates/treehouse/src/cli/generate.rs +++ b/crates/treehouse/src/cli/generate.rs @@ -1,5 +1,3 @@ -mod static_urls; - use std::{ collections::HashMap, ffi::OsStr, @@ -16,7 +14,6 @@ use copy_dir::copy_dir; use handlebars::{handlebars_helper, Handlebars}; use log::{debug, error, info}; use serde::Serialize; -use static_urls::StaticUrls; use walkdir::WalkDir; use crate::{ @@ -29,6 +26,7 @@ use crate::{ tree::branches_to_html, }, state::Source, + static_urls::StaticUrls, tree::SemaRoots, }; @@ -215,7 +213,13 @@ impl Generator { parsed_trees: Vec, ) -> anyhow::Result<()> { let mut handlebars = Handlebars::new(); - let mut config_derived_data = ConfigDerivedData::default(); + let mut config_derived_data = ConfigDerivedData { + image_sizes: Default::default(), + static_urls: StaticUrls::new( + paths.static_dir.to_owned(), + format!("{}/static", config.site), + ), + }; handlebars_helper!(cat: |a: String, b: String| a + &b); diff --git a/crates/treehouse/src/config.rs b/crates/treehouse/src/config.rs index 5b4d567..78b1d9a 100644 --- a/crates/treehouse/src/config.rs +++ b/crates/treehouse/src/config.rs @@ -6,9 +6,12 @@ use log::{debug, error, warn}; use serde::{Deserialize, Serialize}; use walkdir::WalkDir; -use crate::html::highlight::{ - compiled::{compile_syntax, CompiledSyntax}, - Syntax, +use crate::{ + html::highlight::{ + compiled::{compile_syntax, CompiledSyntax}, + Syntax, + }, + static_urls::StaticUrls, }; #[derive(Debug, Clone, Deserialize, Serialize)] @@ -179,9 +182,9 @@ impl Config { } /// Data derived from the config. -#[derive(Debug, Clone, Default)] pub struct ConfigDerivedData { pub image_sizes: HashMap>, + pub static_urls: StaticUrls, } /// Image size. This is useful for emitting elements with a specific size to eliminate diff --git a/crates/treehouse/src/html/markdown.rs b/crates/treehouse/src/html/markdown.rs index 06b06d4..6c82e11 100644 --- a/crates/treehouse/src/html/markdown.rs +++ b/crates/treehouse/src/html/markdown.rs @@ -555,9 +555,12 @@ where .write_str("\"")?; Some((config.page_url(linked).into(), "".into())), "pic" => config.pics.get(linked).map(|filename| { ( + // NOTE: We can't generate a URL with a hash here yet, because we + // cannot access ConfigDerivedData here due to it being borrowed + // by the Markdown parser. format!("{}/static/pic/{}", config.site, &filename).into(), "".into(), ) diff --git a/crates/treehouse/src/main.rs b/crates/treehouse/src/main.rs index 0e380d3..70324c2 100644 --- a/crates/treehouse/src/main.rs +++ b/crates/treehouse/src/main.rs @@ -16,6 +16,7 @@ mod fun; mod html; mod paths; mod state; +mod static_urls; mod tree; async fn fallible_main() -> anyhow::Result<()> { diff --git a/crates/treehouse/src/cli/generate/static_urls.rs b/crates/treehouse/src/static_urls.rs similarity index 100% rename from crates/treehouse/src/cli/generate/static_urls.rs rename to crates/treehouse/src/static_urls.rs