natter/src/blog_post/page.rs

45 lines
1.1 KiB
Rust
Raw Normal View History

use std::path::PathBuf;
use crate::error::CustomError;
#[derive(Debug)]
pub(crate) struct BlogPostPage {
/// Relative path from the root of the blog post.
pub(crate) path: PathBuf,
pub(crate) title: Option<String>,
}
impl BlogPostPage {
pub(crate) fn new<P: Into<PathBuf>>(
path: P,
document: organic::types::Document<'_>,
) -> Result<BlogPostPage, CustomError> {
let path = path.into();
Ok(BlogPostPage {
path,
2023-10-23 22:39:54 +00:00
title: get_title(&document),
})
}
/// 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 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())
}