treehouse/src/config.rs

233 lines
7.8 KiB
Rust
Raw Normal View History

2024-11-27 19:02:30 +01:00
use std::{
collections::{HashMap, HashSet},
ops::ControlFlow,
};
2023-08-27 15:59:52 +02:00
2025-08-26 12:46:50 +02:00
use anyhow::{Context, anyhow};
2024-11-26 22:09:39 +01:00
use rayon::iter::{IntoParallelRefIterator, ParallelIterator};
2023-08-27 15:59:52 +02:00
use serde::{Deserialize, Serialize};
2024-11-26 22:00:02 +01:00
use tracing::{error, info_span, instrument};
2023-08-27 15:59:52 +02:00
2024-07-19 19:52:56 +02:00
use crate::{
html::highlight::{
Syntax,
2025-08-26 12:46:50 +02:00
compiled::{CompiledSyntax, compile_syntax},
2024-07-19 19:52:56 +02:00
},
import_map::ImportRoot,
vfs::{self, Content, Dir, DynDir, ImageSize, VPath, VPathBuf},
2024-03-10 23:23:50 +01:00
};
2025-01-10 14:10:23 +01:00
#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct VfsConfig {
/// Cache salt string. Passed to `Blake3ContentVersionCache` as a salt for content version hashes.
/// Can be changed to bust cached assets for all clients.
pub cache_salt: String,
}
2023-08-27 15:59:52 +02:00
#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct Config {
/// Website root; used when generating links.
/// Can also be specified using the environment variable `$TREEHOUSE_SITE`. (this is the
/// preferred way of setting this in production, so as not to clobber treehouse.toml.)
pub site: String,
/// This is used to generate a link in the footer that links to the page's source commit.
/// The final URL is `{commit_base_url}/{commit}/content/{tree_path}.tree`.
pub commit_base_url: String,
2023-08-27 15:59:52 +02:00
/// User-defined keys.
pub user: HashMap<String, String>,
2023-08-27 16:04:07 +02:00
/// Links exported to Markdown for use with reference syntax `[text][def:key]`.
pub defs: HashMap<String, String>,
2023-08-27 18:25:21 +02:00
2024-11-27 19:02:30 +01:00
/// Config for syndication feeds.
pub feed: Feed,
2024-03-08 17:06:47 +01:00
/// Redirects for moving pages around. These are used solely by the treehouse server.
///
/// Note that redirects are only resolved _non-recursively_ by the server. For a configuration
/// like:
///
/// ```toml
/// page.redirects.foo = "bar"
/// page.redirects.bar = "baz"
/// ```
///
/// the user will be redirected from `foo` to `bar`, then from `bar` to `baz`. This isn't
/// optimal for UX and causes unnecessary latency. Therefore you should always make redirects
/// point to the newest version of the page.
///
/// ```toml
/// page.redirects.foo = "baz"
/// page.redirects.bar = "baz"
/// ```
pub redirects: Redirects,
/// How the treehouse should be built.
pub build: Build,
/// Overrides for emoji names. Useful for setting up aliases.
2023-08-27 18:25:21 +02:00
///
/// Paths are anchored within `static/emoji` and must not contain parent directories.
2023-08-27 18:25:21 +02:00
#[serde(default)]
pub emoji: HashMap<String, VPathBuf>,
2024-02-07 11:24:54 +01:00
/// 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, VPathBuf>,
2024-03-10 23:23:50 +01:00
/// Syntax definitions.
///
/// These are not part of the config file, but are loaded as part of site configuration from
/// `static/syntax`.
#[serde(skip)]
pub syntaxes: HashMap<String, CompiledSyntax>,
2023-08-27 15:59:52 +02:00
}
2024-11-27 19:02:30 +01:00
#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct Feed {
/// Allowed tags in feed entries.
pub tags: HashSet<String>,
}
2024-03-08 17:06:47 +01:00
#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct Redirects {
2025-01-14 23:36:09 +01:00
/// Path redirects. When a user requests a path, if they request `p`, they will be redirected
/// to `path[p]` with a `301 Moved Permanently` status code.
pub path: HashMap<VPathBuf, VPathBuf>,
2024-03-08 17:06:47 +01:00
}
#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct Build {
/// Configuration for how JavaScript is compiled.
pub javascript: JavaScript,
}
#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct JavaScript {
/// Import roots to generate in the project's import map.
pub import_roots: Vec<ImportRoot>,
}
2023-08-27 15:59:52 +02:00
impl Config {
#[instrument(name = "Config::autopopulate_emoji", skip(self))]
pub fn autopopulate_emoji(&mut self, dir: &dyn Dir) -> anyhow::Result<()> {
vfs::walk_dir_rec(dir, VPath::ROOT, &mut |path| {
2025-08-26 12:46:50 +02:00
if path.extension().is_some_and(is_image_file)
&& let Some(emoji_name) = path.file_stem()
&& !self.emoji.contains_key(emoji_name)
{
self.emoji.insert(emoji_name.to_owned(), path.to_owned());
2023-08-27 18:47:57 +02:00
}
2024-02-07 11:24:54 +01:00
ControlFlow::Continue(())
});
2024-02-07 11:24:54 +01:00
Ok(())
}
2024-02-07 11:24:54 +01:00
#[instrument(name = "Config::autopopulate_pics", skip(self))]
pub fn autopopulate_pics(&mut self, dir: &dyn Dir) -> anyhow::Result<()> {
vfs::walk_dir_rec(dir, VPath::ROOT, &mut |path| {
2025-08-26 12:46:50 +02:00
if path.extension().is_some_and(is_image_file)
&& let Some(pic_name) = path.file_stem()
{
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(), path.to_owned());
2024-02-07 11:24:54 +01:00
}
}
ControlFlow::Continue(())
});
2024-02-07 11:24:54 +01:00
Ok(())
}
2024-02-14 23:31:39 +01:00
2024-03-02 20:53:44 +01:00
pub fn page_url(&self, page: &str) -> String {
let (page, hash) = page.split_once('#').unwrap_or((page, ""));
// We don't want .dj appearing in URLs, though it exists as a disambiguator in [page:] links.
let page = page.strip_suffix(".dj").unwrap_or(page);
if !hash.is_empty() {
format!("{}/{page}#{hash}", self.site)
} else {
format!("{}/{page}", self.site)
}
2024-03-02 20:53:44 +01:00
}
pub fn pic_url(&self, pics_dir: &dyn Dir, id: &str) -> String {
vfs::url(
&self.site,
pics_dir,
self.pics
.get(id)
.map(|x| &**x)
.unwrap_or(VPath::new("404.png")),
2024-02-14 23:31:39 +01:00
)
.expect("pics_dir is not anchored anywhere")
2024-02-14 23:31:39 +01:00
}
2024-03-10 23:23:50 +01:00
pub fn pic_size(&self, pics_dir: &dyn Dir, id: &str) -> Option<ImageSize> {
self.pics
.get(id)
.and_then(|path| vfs::query::<ImageSize>(pics_dir, path))
}
2024-03-10 23:23:50 +01:00
/// Loads all syntax definition files.
#[instrument(name = "Config::load_syntaxes", skip(self))]
2024-11-26 22:09:39 +01:00
pub fn load_syntaxes(&mut self, dir: DynDir) -> anyhow::Result<()> {
let mut paths = vec![];
vfs::walk_dir_rec(&dir, VPath::ROOT, &mut |path| {
if path.extension() == Some("json") {
2024-11-26 22:09:39 +01:00
paths.push(path.to_owned());
}
ControlFlow::Continue(())
});
let syntaxes: Vec<_> = paths
.par_iter()
.flat_map(|path| {
let name = path
2024-03-10 23:23:50 +01:00
.file_stem()
.expect("syntax file name should have a stem due to the .json extension");
2024-03-10 23:23:50 +01:00
let result: Result<Syntax, _> = vfs::query::<Content>(&dir, path)
.ok_or_else(|| anyhow!("syntax .json is not a file"))
.and_then(|b| b.string().context("syntax .json contains invalid UTF-8"))
.and_then(|s| {
let _span = info_span!("Config::load_syntaxes::parse").entered();
serde_json::from_str(&s).context("could not deserialize syntax file")
});
match result {
Ok(syntax) => {
let _span = info_span!("Config::load_syntaxes::compile", ?name).entered();
let compiled = compile_syntax(&syntax);
2024-11-26 22:09:39 +01:00
Some((name.to_owned(), compiled))
}
Err(err) => {
error!("error while loading syntax file `{path}`: {err}");
None
}
}
2024-11-26 22:09:39 +01:00
})
.collect();
2024-11-26 22:09:39 +01:00
for (name, compiled) in syntaxes {
self.syntaxes.insert(name, compiled);
}
2024-03-10 23:23:50 +01:00
Ok(())
}
2023-08-27 15:59:52 +02:00
}
2024-02-20 23:30:36 +01:00
pub fn is_image_file(extension: &str) -> bool {
matches!(extension, "png" | "svg" | "jpg" | "jpeg" | "webp")
2024-02-20 23:30:36 +01:00
}