Switch to rendering blog post pages instead of blog posts.

This commit is contained in:
Tom Alexander 2023-10-23 20:30:43 -04:00
parent dc233d26b1
commit 2b6f86d4e9
Signed by: talexander
GPG Key ID: D3A179C9A53C0EDE
6 changed files with 42 additions and 17 deletions

View File

@ -1,6 +1,10 @@
use super::render_context::RenderBlogPost;
use super::render_context::RenderBlogPostPage;
use super::BlogPost;
use super::BlogPostPage;
pub(crate) fn convert_blog_post_to_render_context(post: &BlogPost) -> RenderBlogPost {
RenderBlogPost::new(post.id.clone())
pub(crate) fn convert_blog_post_page_to_render_context(
_post: &BlogPost,
page: &BlogPostPage,
) -> RenderBlogPostPage {
RenderBlogPostPage::new(page.title.clone())
}

View File

@ -11,7 +11,7 @@ use super::BlogPostPage;
#[derive(Debug)]
pub(crate) struct BlogPost {
pub(crate) id: String,
pages: Vec<BlogPostPage>,
pub(crate) pages: Vec<BlogPostPage>,
}
impl BlogPost {

View File

@ -2,6 +2,6 @@ mod convert;
mod definition;
mod page;
mod render_context;
pub(crate) use convert::convert_blog_post_to_render_context;
pub(crate) use convert::convert_blog_post_page_to_render_context;
pub(crate) use definition::BlogPost;
pub(crate) use page::BlogPostPage;

View File

@ -5,9 +5,9 @@ use crate::error::CustomError;
#[derive(Debug)]
pub(crate) struct BlogPostPage {
/// Relative path from the root of the blog post.
path: PathBuf,
pub(crate) path: PathBuf,
title: Option<String>,
pub(crate) title: Option<String>,
}
impl BlogPostPage {
@ -21,6 +21,13 @@ impl BlogPostPage {
title: get_title(&document),
})
}
/// Get the output path relative to the post directory.
pub(crate) fn get_output_path(&self) -> PathBuf {
let mut ret = self.path.clone();
ret.set_extension("html");
ret
}
}
fn get_title(document: &organic::types::Document<'_>) -> Option<String> {

View File

@ -2,13 +2,13 @@ use serde::Serialize;
#[derive(Debug, Serialize)]
#[serde(tag = "type")]
#[serde(rename = "blog_post")]
pub(crate) struct RenderBlogPost {
id: String,
#[serde(rename = "blog_post_page")]
pub(crate) struct RenderBlogPostPage {
title: Option<String>,
}
impl RenderBlogPost {
pub(crate) fn new(id: String) -> RenderBlogPost {
RenderBlogPost { id }
impl RenderBlogPostPage {
pub(crate) fn new(title: Option<String>) -> RenderBlogPostPage {
RenderBlogPostPage { title }
}
}

View File

@ -4,7 +4,7 @@ use std::path::PathBuf;
use include_dir::include_dir;
use include_dir::Dir;
use crate::blog_post::convert_blog_post_to_render_context;
use crate::blog_post::convert_blog_post_page_to_render_context;
use crate::blog_post::BlogPost;
use crate::error::CustomError;
use crate::render::DusterRenderer;
@ -56,9 +56,23 @@ impl SiteRenderer {
}
for blog_post in &self.blog_posts {
let render_context = convert_blog_post_to_render_context(blog_post);
let rendered_output = renderer_integration.render(render_context)?;
println!("Rendered: {}", rendered_output);
for blog_post_page in &blog_post.pages {
let output_path = self
.output_directory
.join("posts")
.join(&blog_post.id)
.join(blog_post_page.get_output_path());
println!("Output path: {:?}", output_path);
let render_context =
convert_blog_post_page_to_render_context(blog_post, blog_post_page);
let rendered_output = renderer_integration.render(render_context)?;
println!("Rendered: {}", rendered_output);
let parent_directory = output_path
.parent()
.ok_or("Output file should have a containing directory.")?;
tokio::fs::create_dir_all(parent_directory).await?;
tokio::fs::write(output_path, rendered_output).await?;
}
}
Ok(())