Feeding the templates into the renderer integration.

This commit is contained in:
Tom Alexander 2023-10-22 17:31:12 -04:00
parent fc5342adce
commit ce0819e85b
Signed by: talexander
GPG Key ID: D3A179C9A53C0EDE
6 changed files with 89 additions and 16 deletions

View File

@ -0,0 +1,9 @@
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
<p>Hello world!</p>
</body>
</html>

View File

@ -1,19 +1,51 @@
use std::ffi::OsStr;
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::BlogPost;
use crate::error::CustomError;
use crate::render::DusterRenderer;
use crate::render::RendererIntegration;
static MAIN_TEMPLATES: Dir = include_dir!("$CARGO_MANIFEST_DIR/default_environment/templates/html");
pub(crate) struct SiteRenderer {
pub(crate) output_directory: PathBuf,
pub(crate) blog_posts: Vec<BlogPost>,
output_directory: PathBuf,
blog_posts: Vec<BlogPost>,
}
impl SiteRenderer {
pub(crate) fn new<P: Into<PathBuf>>(
output_directory: P,
blog_posts: Vec<BlogPost>,
) -> SiteRenderer {
SiteRenderer {
output_directory: output_directory.into(),
blog_posts,
}
}
pub(crate) async fn render_blog_posts(&self) -> Result<(), CustomError> {
let mut renderer_integration = DusterRenderer {};
let mut renderer_integration = DusterRenderer::new();
let (main_template, other_templates): (Vec<_>, Vec<_>) = MAIN_TEMPLATES
.files()
.filter(|f| f.path().extension() == Some(OsStr::new("dust")))
.partition(|f| f.path().file_stem() == Some(OsStr::new("main")));
if main_template.len() != 1 {
return Err("Expect exactly 1 main.dust template file.".into());
}
for entry in main_template {
load_template_from_include_dir(&mut renderer_integration, entry)?;
}
for entry in other_templates {
load_template_from_include_dir(&mut renderer_integration, entry)?;
}
for blog_post in &self.blog_posts {
let render_context = convert_blog_post_to_render_context(blog_post);
renderer_integration.render(render_context)?;
@ -22,3 +54,18 @@ impl SiteRenderer {
Ok(())
}
}
fn load_template_from_include_dir<RI: RendererIntegration>(
renderer_integration: &mut RI,
entry: &include_dir::File<'_>,
) -> Result<(), CustomError> {
let path = entry.path();
let name = path
.file_stem()
.ok_or("All templates should have a stem.")?
.to_str()
.ok_or("All template filenames should be valid utf-8.")?;
let contents = std::str::from_utf8(entry.contents())?;
renderer_integration.load_template(name, contents)?;
Ok(())
}

View File

@ -9,10 +9,7 @@ use crate::error::CustomError;
pub(crate) async fn build_site(args: BuildArgs) -> Result<(), CustomError> {
let config = Config::load_from_file(args.config).await?;
let blog_posts = load_blog_posts(&config).await?;
let renderer = SiteRenderer {
output_directory: get_output_directory(&config).await?,
blog_posts,
};
let renderer = SiteRenderer::new(get_output_directory(&config).await?, blog_posts);
renderer.render_blog_posts().await?;
Ok(())

View File

@ -1,3 +1,6 @@
use std::str::Utf8Error;
use std::string::FromUtf8Error;
#[derive(Debug)]
pub(crate) enum CustomError {
Static(&'static str),
@ -7,6 +10,8 @@ pub(crate) enum CustomError {
WalkDir(walkdir::Error),
Tokio(tokio::task::JoinError),
Serde(serde_json::Error),
Utf8(Utf8Error),
FromUtf8(FromUtf8Error),
}
impl From<std::io::Error> for CustomError {
@ -50,3 +55,15 @@ impl From<serde_json::Error> for CustomError {
CustomError::Serde(value)
}
}
impl From<Utf8Error> for CustomError {
fn from(value: Utf8Error) -> Self {
CustomError::Utf8(value)
}
}
impl From<FromUtf8Error> for CustomError {
fn from(value: FromUtf8Error) -> Self {
CustomError::FromUtf8(value)
}
}

View File

@ -4,13 +4,18 @@ use serde::Serialize;
pub(crate) struct DusterRenderer {}
impl DusterRenderer {
pub(crate) fn new() -> DusterRenderer {
DusterRenderer {}
}
}
impl RendererIntegration for DusterRenderer {
fn load_templates<I, P>(&mut self, dust_templates: I) -> Result<(), crate::error::CustomError>
fn load_template<N, C>(&mut self, name: N, contents: C) -> Result<(), crate::error::CustomError>
where
I: Iterator<Item = P>,
P: Into<std::path::PathBuf>,
N: Into<String>,
C: Into<String>,
{
// TODO
Ok(())
}

View File

@ -1,14 +1,12 @@
use std::path::PathBuf;
use serde::Serialize;
use crate::error::CustomError;
pub(crate) trait RendererIntegration {
fn load_templates<I, P>(&mut self, dust_templates: I) -> Result<(), CustomError>
fn load_template<N, C>(&mut self, name: N, contents: C) -> Result<(), CustomError>
where
I: Iterator<Item = P>,
P: Into<PathBuf>;
N: Into<String>,
C: Into<String>;
fn render<C>(&self, context: C) -> Result<String, crate::error::CustomError>
where