2024-11-27 19:02:30 +01:00
|
|
|
mod atom;
|
2024-11-17 22:34:43 +01:00
|
|
|
mod dir_helper;
|
|
|
|
mod include_static_helper;
|
2024-11-27 19:02:30 +01:00
|
|
|
mod simple_template;
|
|
|
|
mod tree;
|
2023-08-19 17:52:13 +02:00
|
|
|
|
2024-11-17 22:34:43 +01:00
|
|
|
use std::{collections::HashMap, fmt, ops::ControlFlow, sync::Arc};
|
|
|
|
|
2024-11-27 19:02:30 +01:00
|
|
|
use atom::FeedDir;
|
2024-11-17 22:34:43 +01:00
|
|
|
use dir_helper::DirHelper;
|
2024-07-19 18:04:11 +02:00
|
|
|
use handlebars::{handlebars_helper, Handlebars};
|
2024-11-17 22:34:43 +01:00
|
|
|
use include_static_helper::IncludeStaticHelper;
|
2023-08-18 21:19:31 +02:00
|
|
|
use serde::Serialize;
|
2024-11-26 22:00:02 +01:00
|
|
|
use tracing::{error, info_span, instrument};
|
2023-08-18 21:19:31 +02:00
|
|
|
|
2023-08-22 22:19:43 +02:00
|
|
|
use crate::{
|
2024-11-16 18:33:41 +01:00
|
|
|
config::Config,
|
2024-11-17 22:34:43 +01:00
|
|
|
dirs::Dirs,
|
2024-03-08 22:40:19 +01:00
|
|
|
fun::seasons::Season,
|
2024-11-27 18:46:10 +01:00
|
|
|
sources::Sources,
|
2024-11-23 21:21:28 +01:00
|
|
|
vfs::{
|
2025-01-11 00:15:29 +01:00
|
|
|
self, AnchoredAtExt, Cd, Content, ContentCache, Dir, DynDir, Entries, HtmlCanonicalize,
|
|
|
|
MemDir, Overlay, ToDynDir, VPath, VPathBuf,
|
2024-11-23 21:21:28 +01:00
|
|
|
},
|
2023-08-22 22:19:43 +02:00
|
|
|
};
|
2023-08-19 22:39:02 +02:00
|
|
|
|
2024-02-21 23:17:19 +01:00
|
|
|
#[derive(Serialize)]
|
2024-11-16 18:33:41 +01:00
|
|
|
struct BaseTemplateData<'a> {
|
2024-02-21 23:17:19 +01:00
|
|
|
config: &'a Config,
|
2024-11-16 18:33:41 +01:00
|
|
|
import_map: String,
|
2024-03-08 22:40:19 +01:00
|
|
|
season: Option<Season>,
|
2024-11-23 19:12:00 +01:00
|
|
|
dev: bool,
|
2024-11-27 19:02:30 +01:00
|
|
|
feeds: Vec<String>,
|
2024-02-21 23:17:19 +01:00
|
|
|
}
|
|
|
|
|
2024-11-27 19:02:30 +01:00
|
|
|
impl<'a> BaseTemplateData<'a> {
|
|
|
|
fn new(sources: &'a Sources) -> Self {
|
|
|
|
Self {
|
|
|
|
config: &sources.config,
|
|
|
|
import_map: serde_json::to_string_pretty(&sources.import_map)
|
|
|
|
.expect("import map should be serializable to JSON"),
|
|
|
|
season: Season::current(),
|
|
|
|
dev: cfg!(debug_assertions),
|
2024-11-27 19:02:30 +01:00
|
|
|
feeds: sources.treehouse.feeds_by_name.keys().cloned().collect(),
|
2024-11-27 19:02:30 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-11-17 22:34:43 +01:00
|
|
|
fn create_handlebars(site: &str, static_: DynDir) -> Handlebars<'static> {
|
|
|
|
let mut handlebars = Handlebars::new();
|
2024-09-28 23:43:05 +02:00
|
|
|
|
2024-11-17 22:34:43 +01:00
|
|
|
handlebars_helper!(cat: |a: String, b: String| a + &b);
|
2023-08-18 21:19:31 +02:00
|
|
|
|
2024-11-17 22:34:43 +01:00
|
|
|
handlebars.register_helper("cat", Box::new(cat));
|
|
|
|
handlebars.register_helper("asset", Box::new(DirHelper::new(site, static_.clone())));
|
|
|
|
handlebars.register_helper(
|
|
|
|
"include_static",
|
|
|
|
Box::new(IncludeStaticHelper::new(static_)),
|
|
|
|
);
|
2024-09-28 23:43:05 +02:00
|
|
|
|
2024-11-17 22:34:43 +01:00
|
|
|
handlebars
|
|
|
|
}
|
2024-09-28 23:43:05 +02:00
|
|
|
|
2024-11-26 20:55:49 +01:00
|
|
|
#[instrument(skip(handlebars))]
|
2024-11-17 22:34:43 +01:00
|
|
|
fn load_templates(handlebars: &mut Handlebars, dir: &dyn Dir) {
|
|
|
|
vfs::walk_dir_rec(dir, VPath::ROOT, &mut |path| {
|
|
|
|
if path.extension() == Some("hbs") {
|
2024-11-29 20:03:32 +01:00
|
|
|
if let Some(content) = vfs::query::<Content>(dir, path).and_then(|c| c.string().ok()) {
|
2024-11-26 20:55:49 +01:00
|
|
|
let _span = info_span!("register_template", ?path).entered();
|
2024-11-17 22:34:43 +01:00
|
|
|
if let Err(err) = handlebars.register_template_string(path.as_str(), content) {
|
|
|
|
error!("in template: {err}");
|
|
|
|
}
|
|
|
|
}
|
2023-08-18 21:19:31 +02:00
|
|
|
}
|
2024-11-17 22:34:43 +01:00
|
|
|
ControlFlow::Continue(())
|
|
|
|
});
|
|
|
|
}
|
2023-08-19 17:52:13 +02:00
|
|
|
|
2024-11-29 20:03:32 +01:00
|
|
|
struct TreehouseDir {
|
|
|
|
dirs: Arc<Dirs>,
|
|
|
|
sources: Arc<Sources>,
|
|
|
|
handlebars: Arc<Handlebars<'static>>,
|
|
|
|
dir_index: DirIndex,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl TreehouseDir {
|
|
|
|
fn new(
|
|
|
|
dirs: Arc<Dirs>,
|
|
|
|
sources: Arc<Sources>,
|
|
|
|
handlebars: Arc<Handlebars<'static>>,
|
|
|
|
dir_index: DirIndex,
|
|
|
|
) -> Self {
|
|
|
|
Self {
|
|
|
|
dirs,
|
|
|
|
sources,
|
|
|
|
handlebars,
|
|
|
|
dir_index,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-11-26 20:55:49 +01:00
|
|
|
#[instrument("TreehouseDir::dir", skip(self))]
|
2024-11-29 20:03:32 +01:00
|
|
|
fn dir(&self, path: &VPath) -> Vec<VPathBuf> {
|
2024-11-22 23:12:57 +01:00
|
|
|
// NOTE: This does not include simple templates, because that's not really needed right now.
|
|
|
|
|
2024-11-17 22:34:43 +01:00
|
|
|
let mut index = &self.dir_index;
|
|
|
|
for component in path.segments() {
|
|
|
|
if let Some(child) = index.children.get(component) {
|
|
|
|
index = child;
|
|
|
|
} else {
|
|
|
|
// There cannot possibly be any entries under an invalid path.
|
|
|
|
// Bail early.
|
|
|
|
return vec![];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
index
|
|
|
|
.children
|
|
|
|
.values()
|
2024-11-29 20:03:32 +01:00
|
|
|
.map(|child| child.full_path.clone())
|
2024-11-17 22:34:43 +01:00
|
|
|
.collect()
|
2024-09-28 23:43:05 +02:00
|
|
|
}
|
2023-08-19 17:52:13 +02:00
|
|
|
|
2024-11-26 20:55:49 +01:00
|
|
|
#[instrument("TreehouseDir::content", skip(self))]
|
2024-11-29 20:03:32 +01:00
|
|
|
fn content(&self, path: &VPath) -> Option<Content> {
|
2024-11-17 22:34:43 +01:00
|
|
|
let path = if path.is_root() {
|
|
|
|
VPath::new_const("index")
|
|
|
|
} else {
|
|
|
|
path
|
|
|
|
};
|
2023-08-18 21:19:31 +02:00
|
|
|
|
2024-11-22 23:12:57 +01:00
|
|
|
self.sources
|
2024-11-26 22:58:02 +01:00
|
|
|
.treehouse
|
|
|
|
.files_by_tree_path
|
2024-11-23 21:21:28 +01:00
|
|
|
.get(path)
|
2024-11-26 22:58:02 +01:00
|
|
|
.map(|&file_id| {
|
2024-11-29 20:03:32 +01:00
|
|
|
Content::new(
|
2025-01-14 23:09:01 +01:00
|
|
|
"text/html",
|
2024-11-29 20:03:32 +01:00
|
|
|
tree::generate_or_error(&self.sources, &self.dirs, &self.handlebars, file_id)
|
|
|
|
.into(),
|
|
|
|
)
|
2024-11-22 23:12:57 +01:00
|
|
|
})
|
|
|
|
.or_else(|| {
|
|
|
|
if path.file_name().is_some_and(|s| !s.starts_with('_')) {
|
|
|
|
let template_name = path.with_extension("hbs");
|
|
|
|
if self.handlebars.has_template(template_name.as_str()) {
|
2024-11-29 20:03:32 +01:00
|
|
|
return Some(Content::new(
|
2025-01-14 23:09:01 +01:00
|
|
|
"text/html",
|
2024-11-27 19:02:30 +01:00
|
|
|
simple_template::generate_or_error(
|
2024-11-22 23:12:57 +01:00
|
|
|
&self.sources,
|
|
|
|
&self.handlebars,
|
|
|
|
template_name.as_str(),
|
|
|
|
)
|
|
|
|
.into(),
|
2024-11-29 20:03:32 +01:00
|
|
|
));
|
2024-11-22 23:12:57 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
None
|
|
|
|
})
|
2024-11-17 22:34:43 +01:00
|
|
|
}
|
2024-11-29 20:03:32 +01:00
|
|
|
}
|
2023-08-27 14:50:46 +02:00
|
|
|
|
2024-11-29 20:03:32 +01:00
|
|
|
impl Dir for TreehouseDir {
|
|
|
|
fn query(&self, path: &VPath, query: &mut vfs::Query) {
|
|
|
|
query.provide(|| Entries(self.dir(path)));
|
|
|
|
query.try_provide(|| self.content(path));
|
2024-01-18 22:46:57 +01:00
|
|
|
}
|
2023-08-18 21:19:31 +02:00
|
|
|
}
|
|
|
|
|
2024-11-17 22:34:43 +01:00
|
|
|
impl fmt::Debug for TreehouseDir {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
|
|
f.write_str("TreehouseDir")
|
2023-08-18 21:19:31 +02:00
|
|
|
}
|
2024-11-17 22:34:43 +01:00
|
|
|
}
|
|
|
|
|
2024-11-27 19:02:30 +01:00
|
|
|
/// Acceleration structure for `dir` operations on [`TreehouseDir`]s.
|
|
|
|
#[derive(Debug, Default)]
|
|
|
|
struct DirIndex {
|
|
|
|
full_path: VPathBuf,
|
|
|
|
children: HashMap<VPathBuf, DirIndex>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl DirIndex {
|
|
|
|
#[instrument(name = "DirIndex::new", skip(paths))]
|
|
|
|
pub fn new<'a>(paths: impl Iterator<Item = &'a VPath>) -> Self {
|
|
|
|
let mut root = DirIndex::default();
|
|
|
|
|
|
|
|
for path in paths {
|
|
|
|
let mut parent = &mut root;
|
|
|
|
let mut full_path = VPath::ROOT.to_owned();
|
|
|
|
for segment in path.segments() {
|
|
|
|
full_path.push(segment);
|
|
|
|
let child = parent
|
|
|
|
.children
|
|
|
|
.entry(segment.to_owned())
|
|
|
|
.or_insert_with(|| DirIndex {
|
|
|
|
full_path: full_path.clone(),
|
|
|
|
children: HashMap::new(),
|
|
|
|
});
|
|
|
|
parent = child;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
root
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-11-17 22:34:43 +01:00
|
|
|
pub fn target(dirs: Arc<Dirs>, sources: Arc<Sources>) -> DynDir {
|
2024-11-27 19:02:30 +01:00
|
|
|
let mut handlebars = create_handlebars(&sources.config.site, dirs.static_.clone());
|
|
|
|
load_templates(&mut handlebars, &dirs.template);
|
|
|
|
let handlebars = Arc::new(handlebars);
|
|
|
|
|
2024-11-17 22:34:43 +01:00
|
|
|
let mut root = MemDir::new();
|
2024-11-27 19:02:30 +01:00
|
|
|
root.add(
|
|
|
|
VPath::new("feed"),
|
|
|
|
ContentCache::new(FeedDir::new(
|
|
|
|
dirs.clone(),
|
|
|
|
sources.clone(),
|
|
|
|
handlebars.clone(),
|
|
|
|
))
|
|
|
|
.to_dyn(),
|
|
|
|
);
|
2024-11-17 22:34:43 +01:00
|
|
|
root.add(VPath::new("static"), dirs.static_.clone());
|
2024-11-24 14:28:34 +01:00
|
|
|
root.add(
|
|
|
|
VPath::new("robots.txt"),
|
|
|
|
Cd::new(dirs.static_.clone(), VPathBuf::new("robots.txt")).to_dyn(),
|
|
|
|
);
|
2024-11-17 22:34:43 +01:00
|
|
|
|
2024-11-26 22:58:02 +01:00
|
|
|
let dir_index = DirIndex::new(sources.treehouse.files_by_tree_path.keys().map(|x| &**x));
|
2024-11-27 19:02:30 +01:00
|
|
|
let tree_view = TreehouseDir::new(dirs, sources, handlebars, dir_index);
|
2024-11-23 21:42:25 +01:00
|
|
|
|
2024-11-23 21:21:28 +01:00
|
|
|
let tree_view = ContentCache::new(tree_view);
|
2024-11-23 21:42:25 +01:00
|
|
|
tree_view.warm_up();
|
2024-11-23 21:21:28 +01:00
|
|
|
let tree_view = HtmlCanonicalize::new(tree_view);
|
2024-11-17 22:34:43 +01:00
|
|
|
|
2025-01-11 00:15:29 +01:00
|
|
|
Overlay::new(tree_view.to_dyn(), root.to_dyn())
|
|
|
|
.anchored_at(VPath::ROOT.to_owned())
|
|
|
|
.to_dyn()
|
2023-08-18 21:19:31 +02:00
|
|
|
}
|