2023-10-23 20:03:37 +00:00
|
|
|
use std::path::PathBuf;
|
|
|
|
|
|
|
|
use crate::error::CustomError;
|
|
|
|
|
2023-10-27 18:43:06 +00:00
|
|
|
use super::registry::Registry;
|
2023-10-27 17:05:34 +00:00
|
|
|
use super::IDocumentElement;
|
|
|
|
use super::IHeading;
|
|
|
|
use super::ISection;
|
2023-10-24 04:36:08 +00:00
|
|
|
|
2023-10-23 20:03:37 +00:00
|
|
|
#[derive(Debug)]
|
|
|
|
pub(crate) struct BlogPostPage {
|
|
|
|
/// Relative path from the root of the blog post.
|
2023-10-24 00:30:43 +00:00
|
|
|
pub(crate) path: PathBuf,
|
2023-10-23 20:03:37 +00:00
|
|
|
|
2023-10-24 00:30:43 +00:00
|
|
|
pub(crate) title: Option<String>,
|
2023-10-24 04:36:08 +00:00
|
|
|
|
2023-10-27 17:05:34 +00:00
|
|
|
pub(crate) children: Vec<IDocumentElement>,
|
2023-10-23 20:03:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl BlogPostPage {
|
|
|
|
pub(crate) fn new<P: Into<PathBuf>>(
|
|
|
|
path: P,
|
2023-10-27 18:43:06 +00:00
|
|
|
registry: &mut Registry<'_>,
|
|
|
|
document: &organic::types::Document<'_>,
|
2023-10-23 20:03:37 +00:00
|
|
|
) -> Result<BlogPostPage, CustomError> {
|
|
|
|
let path = path.into();
|
2023-10-24 04:36:08 +00:00
|
|
|
let mut children = Vec::new();
|
|
|
|
if let Some(section) = document.zeroth_section.as_ref() {
|
2023-10-27 18:43:06 +00:00
|
|
|
children.push(IDocumentElement::Section(ISection::new(registry, section)?));
|
2023-10-24 04:36:08 +00:00
|
|
|
}
|
|
|
|
for heading in document.children.iter() {
|
2023-10-27 18:43:06 +00:00
|
|
|
children.push(IDocumentElement::Heading(IHeading::new(registry, heading)?));
|
2023-10-24 04:36:08 +00:00
|
|
|
}
|
|
|
|
|
2023-10-23 20:03:37 +00:00
|
|
|
Ok(BlogPostPage {
|
|
|
|
path,
|
2023-10-23 22:39:54 +00:00
|
|
|
title: get_title(&document),
|
2023-10-24 04:36:08 +00:00
|
|
|
children,
|
2023-10-23 20:03:37 +00:00
|
|
|
})
|
|
|
|
}
|
2023-10-24 00:30:43 +00:00
|
|
|
|
|
|
|
/// 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
|
|
|
|
}
|
2023-10-23 20:03:37 +00:00
|
|
|
}
|
2023-10-23 22:39:54 +00:00
|
|
|
|
|
|
|
fn get_title(document: &organic::types::Document<'_>) -> Option<String> {
|
|
|
|
organic::types::AstNode::from(document)
|
|
|
|
.iter_all_ast_nodes()
|
|
|
|
.filter_map(|node| match node {
|
|
|
|
organic::types::AstNode::Keyword(kw) if kw.key.eq_ignore_ascii_case("title") => {
|
|
|
|
Some(kw)
|
|
|
|
}
|
|
|
|
_ => None,
|
|
|
|
})
|
|
|
|
.last()
|
|
|
|
.map(|kw| kw.value.to_owned())
|
|
|
|
}
|