|
|
|
@ -11,12 +11,14 @@ use crate::data::{load_page, FolderData};
|
|
|
|
|
pub struct ContentRenderer {
|
|
|
|
|
template_glob: String,
|
|
|
|
|
out_dir: PathBuf,
|
|
|
|
|
content_dir: PathBuf,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl ContentRenderer {
|
|
|
|
|
pub fn new(template_glob: String, out_dir: PathBuf) -> Self {
|
|
|
|
|
pub fn new(template_glob: String, content_dir: PathBuf, out_dir: PathBuf) -> Self {
|
|
|
|
|
Self {
|
|
|
|
|
template_glob,
|
|
|
|
|
content_dir,
|
|
|
|
|
out_dir,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
@ -26,7 +28,8 @@ impl ContentRenderer {
|
|
|
|
|
if self.out_dir.exists() {
|
|
|
|
|
fs::remove_dir_all(&self.out_dir).await.into_diagnostic()?;
|
|
|
|
|
}
|
|
|
|
|
let tera = Tera::new(&self.template_glob).into_diagnostic()?;
|
|
|
|
|
let mut tera = Tera::new(&self.template_glob).into_diagnostic()?;
|
|
|
|
|
super::processors::register_all(&mut tera);
|
|
|
|
|
future::try_join_all(dirs.into_iter().map(|data| self.render_folder(&tera, data))).await?;
|
|
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
@ -34,14 +37,41 @@ impl ContentRenderer {
|
|
|
|
|
|
|
|
|
|
#[tracing::instrument(level = "trace", skip_all)]
|
|
|
|
|
async fn render_folder(&self, tera: &Tera, data: FolderData) -> Result<()> {
|
|
|
|
|
for page_path in data.pages {
|
|
|
|
|
let page = load_page(&page_path).await?;
|
|
|
|
|
let mut context = Context::new();
|
|
|
|
|
let mut template_name = data
|
|
|
|
|
let dir_name = data
|
|
|
|
|
.path
|
|
|
|
|
.components()
|
|
|
|
|
.last()
|
|
|
|
|
.unwrap()
|
|
|
|
|
.as_os_str()
|
|
|
|
|
.to_string_lossy();
|
|
|
|
|
let default_template = data
|
|
|
|
|
.index
|
|
|
|
|
.default_template
|
|
|
|
|
.to_owned()
|
|
|
|
|
.unwrap_or("default".into());
|
|
|
|
|
.unwrap_or(dir_name.into());
|
|
|
|
|
|
|
|
|
|
future::try_join_all(
|
|
|
|
|
data.pages
|
|
|
|
|
.into_iter()
|
|
|
|
|
.map(|page| self.render_page(tera, default_template.clone(), page)),
|
|
|
|
|
)
|
|
|
|
|
.await?;
|
|
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[tracing::instrument(level = "trace", skip_all)]
|
|
|
|
|
async fn render_page(
|
|
|
|
|
&self,
|
|
|
|
|
tera: &Tera,
|
|
|
|
|
default_template: String,
|
|
|
|
|
page_path: PathBuf,
|
|
|
|
|
) -> Result<()> {
|
|
|
|
|
tracing::debug!("Rendering {page_path:?}");
|
|
|
|
|
|
|
|
|
|
let page = load_page(&page_path).await?;
|
|
|
|
|
let mut context = Context::new();
|
|
|
|
|
let mut template_name = default_template;
|
|
|
|
|
|
|
|
|
|
match page {
|
|
|
|
|
crate::data::Page::Data(data) => {
|
|
|
|
@ -53,11 +83,11 @@ impl ContentRenderer {
|
|
|
|
|
crate::data::Page::Content(content) => context.insert("content", &content),
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
tracing::debug!("context = {:?}", context);
|
|
|
|
|
tracing::debug!("context = {context:?}");
|
|
|
|
|
|
|
|
|
|
let html = tera.render(&template_name, &context).into_diagnostic()?;
|
|
|
|
|
let rel_path = page_path
|
|
|
|
|
.strip_prefix(&data.content_root)
|
|
|
|
|
.strip_prefix(&self.content_dir)
|
|
|
|
|
.into_diagnostic()?;
|
|
|
|
|
let mut out_path = self.out_dir.join(rel_path);
|
|
|
|
|
out_path.set_extension("html");
|
|
|
|
@ -67,7 +97,6 @@ impl ContentRenderer {
|
|
|
|
|
fs::create_dir_all(parent).await.into_diagnostic()?;
|
|
|
|
|
}
|
|
|
|
|
fs::write(out_path, html).await.into_diagnostic()?;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|