natter/src/command/build/render.rs

151 lines
5.1 KiB
Rust
Raw Normal View History

use std::ffi::OsStr;
2023-10-23 21:51:15 -04:00
use std::path::Path;
use std::path::PathBuf;
use include_dir::include_dir;
use include_dir::Dir;
use crate::blog_post::convert_blog_post_page_to_render_context;
use crate::blog_post::BlogPost;
2023-10-23 21:51:15 -04:00
use crate::blog_post::GlobalSettings;
use crate::config::Config;
use crate::error::CustomError;
use crate::render::DusterRenderer;
2023-10-22 16:40:58 -04:00
use crate::render::RendererIntegration;
static MAIN_TEMPLATES: Dir = include_dir!("$CARGO_MANIFEST_DIR/default_environment/templates/html");
pub(crate) struct SiteRenderer {
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,
}
}
2023-10-23 21:51:15 -04:00
pub(crate) async fn render_blog_posts(&self, config: &Config) -> Result<(), CustomError> {
let mut renderer_integration = DusterRenderer::new();
let sources: Vec<_> = MAIN_TEMPLATES
.files()
.filter(|f| f.path().extension() == Some(OsStr::new("dust")))
.collect();
if sources
.iter()
.filter(|f| f.path().file_stem() == Some(OsStr::new("main")))
.count()
!= 1
{
return Err("Expect exactly 1 main.dust template file.".into());
}
let decoded_templates = {
let mut decoded_templates = Vec::with_capacity(sources.len());
for entry in sources {
decoded_templates.push(build_name_contents_pairs(entry)?);
}
decoded_templates
};
for (name, contents) in decoded_templates {
renderer_integration.load_template(name, contents)?;
}
for blog_post in &self.blog_posts {
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());
2023-10-23 21:51:15 -04:00
let css_files = vec![get_web_path(
config,
&self.output_directory,
&output_path,
"main.css",
)?];
let js_files = vec![get_web_path(
config,
&self.output_directory,
&output_path,
"blog_post.js",
)?];
let global_settings =
GlobalSettings::new(blog_post_page.title.clone(), css_files, js_files);
let render_context = convert_blog_post_page_to_render_context(
blog_post,
blog_post_page,
global_settings,
);
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(())
}
}
fn build_name_contents_pairs<'a>(
entry: &'a include_dir::File<'_>,
) -> Result<(&'a str, &'a str), 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())?;
Ok((name, contents))
}
2023-10-23 21:51:15 -04:00
fn get_web_path<D: AsRef<Path>, F: AsRef<Path>, P: AsRef<Path>>(
config: &Config,
output_directory: D,
containing_file: F,
path_from_web_root: P,
) -> Result<String, CustomError> {
let path_from_web_root = path_from_web_root.as_ref();
if config.use_relative_paths() {
let output_directory = output_directory.as_ref();
let containing_file = containing_file.as_ref();
// Subtracting 1 from the depth to "remove" the file name.
let depth_from_web_root = containing_file
.strip_prefix(output_directory)?
.components()
.count()
- 1;
let prefix = "../".repeat(depth_from_web_root);
let final_path = PathBuf::from(prefix).join(path_from_web_root);
let final_string = final_path
.as_path()
.to_str()
.map(str::to_string)
.ok_or("Path should be valid utf-8.")?;
Ok(final_string)
} else {
let web_root = config
.get_web_root()
.ok_or("Must either use_relative_paths or set the web_root in the config.")?;
let final_path = PathBuf::from(web_root).join(path_from_web_root);
let final_string = final_path
.as_path()
.to_str()
.map(str::to_string)
.ok_or("Path should be valid utf-8.")?;
Ok(final_string)
}
}