make emojis cached

This commit is contained in:
liquidex 2024-07-19 19:52:56 +02:00
parent c337d38891
commit f3aee8f41a
6 changed files with 25 additions and 11 deletions

View file

@ -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<ParsedTree>,
) -> 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);

View file

@ -6,9 +6,12 @@ use log::{debug, error, warn};
use serde::{Deserialize, Serialize};
use walkdir::WalkDir;
use crate::html::highlight::{
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<String, Option<ImageSize>>,
pub static_urls: StaticUrls,
}
/// Image size. This is useful for emitting <img> elements with a specific size to eliminate

View file

@ -555,9 +555,12 @@ where
.write_str("<img data-cast=\"emoji\" title=\":")?;
escape_html(&mut self.writer, name)?;
self.writer.write_str(":\" src=\"")?;
escape_html(&mut self.writer, &self.config.site)?;
self.writer.write_str("/static/emoji/")?;
escape_html(&mut self.writer, filename)?;
let url = self
.config_derived_data
.static_urls
.get(&format!("emoji/{filename}"))
.unwrap_or_default();
escape_html(&mut self.writer, &url)?;
self.writer.write_str("\" alt=\"")?;
escape_html(&mut self.writer, name)?;
if let Some(image_size) = self

View file

@ -142,6 +142,9 @@ pub fn branch_to_html(
"page" => 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(),
)

View file

@ -16,6 +16,7 @@ mod fun;
mod html;
mod paths;
mod state;
mod static_urls;
mod tree;
async fn fallible_main() -> anyhow::Result<()> {