Add homepage
This commit is contained in:
parent
5d795c3084
commit
ec0746011e
9
site_test/index.html
Normal file
9
site_test/index.html
Normal file
@ -0,0 +1,9 @@
|
||||
{% extends "default" %}
|
||||
|
||||
{% block content -%}
|
||||
|
||||
hello
|
||||
|
||||
{{ latest_post.metadata.title }}
|
||||
|
||||
{%- endblock %}
|
62
src/generator/home.rs
Normal file
62
src/generator/home.rs
Normal file
@ -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<Vec<Post<HtmlContent>>>,
|
||||
default_template: Input<Templates>,
|
||||
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<Vec<Post<HtmlContent>>>);
|
||||
impl Rule for LatestPost {
|
||||
type Output = Post<HtmlContent>;
|
||||
fn evaluate(&mut self) -> Self::Output {
|
||||
self.input_0()
|
||||
.iter()
|
||||
.max_by_key(|p| p.metadata.date)
|
||||
.unwrap()
|
||||
.clone()
|
||||
}
|
||||
}
|
@ -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<RefCell<FileWatcher>>) -> anyhow::Result<AsyncGraph<()
|
||||
|
||||
let archive = archive::make_graph(
|
||||
&mut builder,
|
||||
all_posts,
|
||||
all_posts.clone(),
|
||||
default_template.clone(),
|
||||
&mut *watcher.borrow_mut(),
|
||||
);
|
||||
@ -45,6 +46,13 @@ fn make_graph(watcher: Rc<RefCell<FileWatcher>>) -> anyhow::Result<AsyncGraph<()
|
||||
let tags = tags::make_graph(
|
||||
&mut builder,
|
||||
posts,
|
||||
default_template.clone(),
|
||||
&mut *watcher.borrow_mut(),
|
||||
);
|
||||
|
||||
let home = home::make_graph(
|
||||
&mut builder,
|
||||
all_posts,
|
||||
default_template,
|
||||
&mut *watcher.borrow_mut(),
|
||||
);
|
||||
@ -56,6 +64,7 @@ fn make_graph(watcher: Rc<RefCell<FileWatcher>>) -> anyhow::Result<AsyncGraph<()
|
||||
void_outputs,
|
||||
archive,
|
||||
tags,
|
||||
home,
|
||||
css,
|
||||
post_metadatas_voided,
|
||||
]);
|
||||
|
@ -2,6 +2,7 @@ use std::path::PathBuf;
|
||||
|
||||
use anyhow::anyhow;
|
||||
use chrono::Datelike;
|
||||
use serde::Serialize;
|
||||
|
||||
use crate::generator::markdown;
|
||||
use crate::generator::posts::metadata::PostMetadata;
|
||||
@ -12,13 +13,14 @@ pub trait PostContent: std::fmt::Debug + PartialEq {
|
||||
fn word_count(&self) -> u32;
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq, Clone)]
|
||||
#[derive(Debug, PartialEq, Clone, Serialize)]
|
||||
pub struct Post<Content: PostContent> {
|
||||
pub path: PathBuf,
|
||||
pub metadata: PostMetadata,
|
||||
pub slug: String,
|
||||
pub word_count: Option<u32>,
|
||||
pub excerpt: Option<String>,
|
||||
#[serde(skip)]
|
||||
pub content: Content,
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user