diff --git a/site_test/index.html b/site_test/index.html new file mode 100644 index 0000000..8f47c8a --- /dev/null +++ b/site_test/index.html @@ -0,0 +1,9 @@ +{% extends "default" %} + +{% block content -%} + +hello + +{{ latest_post.metadata.title }} + +{%- endblock %} diff --git a/src/generator/home.rs b/src/generator/home.rs new file mode 100644 index 0000000..0a7f0b6 --- /dev/null +++ b/src/generator/home.rs @@ -0,0 +1,62 @@ +use compute_graph::{ + builder::GraphBuilder, + rule::{Input, InputVisitable, Rule}, + synchronicity::Asynchronous, +}; + +use crate::generator::{ + templates::{AddTemplate, BuildTemplateContext}, + util::content_path, +}; + +use super::{ + FileWatcher, + posts::content::{HtmlContent, Post}, + templates::{RenderTemplate, Templates}, +}; + +pub fn make_graph( + builder: &mut GraphBuilder<(), Asynchronous>, + posts: Input>>, + default_template: Input, + watcher: &mut FileWatcher, +) -> Input<()> { + let latest_post = builder.add_rule(LatestPost(posts)); + + let template_path = content_path("index.html"); + let (home_template, invalidate_template) = builder.add_invalidatable_rule(AddTemplate::new( + "home", + template_path.clone(), + default_template, + )); + watcher.watch(template_path, move || invalidate_template.invalidate()); + + let context = builder.add_rule(BuildTemplateContext::new( + "/".into(), + latest_post, + |latest_post, ctx| { + ctx.insert("latest_post", latest_post); + ctx.insert("latest_post_content", latest_post.content.html()); + }, + )); + + builder.add_rule(RenderTemplate { + name: "home", + output_path: "index.html".into(), + templates: home_template, + context, + }) +} + +#[derive(InputVisitable)] +struct LatestPost(Input>>); +impl Rule for LatestPost { + type Output = Post; + fn evaluate(&mut self) -> Self::Output { + self.input_0() + .iter() + .max_by_key(|p| p.metadata.date) + .unwrap() + .clone() + } +} diff --git a/src/generator/mod.rs b/src/generator/mod.rs index 046b0ed..3a9b7db 100644 --- a/src/generator/mod.rs +++ b/src/generator/mod.rs @@ -1,5 +1,6 @@ mod archive; mod css; +mod home; mod markdown; mod posts; mod tags; @@ -37,7 +38,7 @@ fn make_graph(watcher: Rc>) -> anyhow::Result>) -> anyhow::Result>) -> anyhow::Result u32; } -#[derive(Debug, PartialEq, Clone)] +#[derive(Debug, PartialEq, Clone, Serialize)] pub struct Post { pub path: PathBuf, pub metadata: PostMetadata, pub slug: String, pub word_count: Option, pub excerpt: Option, + #[serde(skip)] pub content: Content, }