2023-10-22 17:31:12 -04:00
|
|
|
use std::ffi::OsStr;
|
2023-10-22 16:10:41 -04:00
|
|
|
use std::path::PathBuf;
|
|
|
|
|
2023-10-22 17:31:12 -04:00
|
|
|
use include_dir::include_dir;
|
|
|
|
use include_dir::Dir;
|
|
|
|
|
2023-10-22 16:10:41 -04:00
|
|
|
use crate::blog_post::convert_blog_post_to_render_context;
|
|
|
|
use crate::blog_post::BlogPost;
|
|
|
|
use crate::error::CustomError;
|
2023-10-22 16:26:43 -04:00
|
|
|
use crate::render::DusterRenderer;
|
2023-10-22 16:40:58 -04:00
|
|
|
use crate::render::RendererIntegration;
|
2023-10-22 16:10:41 -04:00
|
|
|
|
2023-10-22 17:31:12 -04:00
|
|
|
static MAIN_TEMPLATES: Dir = include_dir!("$CARGO_MANIFEST_DIR/default_environment/templates/html");
|
|
|
|
|
2023-10-22 16:10:41 -04:00
|
|
|
pub(crate) struct SiteRenderer {
|
2023-10-22 17:31:12 -04:00
|
|
|
output_directory: PathBuf,
|
|
|
|
blog_posts: Vec<BlogPost>,
|
2023-10-22 16:10:41 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
impl SiteRenderer {
|
2023-10-22 17:31:12 -04:00
|
|
|
pub(crate) fn new<P: Into<PathBuf>>(
|
|
|
|
output_directory: P,
|
|
|
|
blog_posts: Vec<BlogPost>,
|
|
|
|
) -> SiteRenderer {
|
|
|
|
SiteRenderer {
|
|
|
|
output_directory: output_directory.into(),
|
|
|
|
blog_posts,
|
|
|
|
}
|
|
|
|
}
|
2023-10-22 16:10:41 -04:00
|
|
|
pub(crate) async fn render_blog_posts(&self) -> Result<(), CustomError> {
|
2023-10-22 17:31:12 -04:00
|
|
|
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());
|
|
|
|
}
|
|
|
|
|
2023-10-22 17:43:30 -04:00
|
|
|
let decoded_templates = {
|
|
|
|
let mut decoded_templates =
|
|
|
|
Vec::with_capacity(main_template.len() + other_templates.len());
|
|
|
|
for entry in main_template {
|
|
|
|
decoded_templates.push(build_name_contents_pairs(entry)?);
|
|
|
|
}
|
|
|
|
for entry in other_templates {
|
|
|
|
decoded_templates.push(build_name_contents_pairs(entry)?);
|
|
|
|
}
|
|
|
|
decoded_templates
|
|
|
|
};
|
2023-10-22 17:31:12 -04:00
|
|
|
|
2023-10-22 17:43:30 -04:00
|
|
|
for (name, contents) in decoded_templates {
|
|
|
|
renderer_integration.load_template(name, contents)?;
|
2023-10-22 17:31:12 -04:00
|
|
|
}
|
|
|
|
|
2023-10-22 16:10:41 -04:00
|
|
|
for blog_post in &self.blog_posts {
|
|
|
|
let render_context = convert_blog_post_to_render_context(blog_post);
|
2023-10-22 16:40:58 -04:00
|
|
|
renderer_integration.render(render_context)?;
|
2023-10-22 16:10:41 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
2023-10-22 17:31:12 -04:00
|
|
|
|
2023-10-22 17:43:30 -04:00
|
|
|
fn build_name_contents_pairs<'a>(
|
|
|
|
entry: &'a include_dir::File<'_>,
|
|
|
|
) -> Result<(&'a str, &'a str), CustomError> {
|
2023-10-22 17:31:12 -04:00
|
|
|
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())?;
|
2023-10-22 17:43:30 -04:00
|
|
|
Ok((name, contents))
|
2023-10-22 17:31:12 -04:00
|
|
|
}
|