Rename blog_post module to intermediate.

This module is mostly the intermediate representation of the AST, so the renaming is to make that more clear. The three forms are parsed => intermediate => render.

Parsed comes from Organic and is a direct translation of the org-mode text.

Intermediate converts the parsed data into owned values and does any calculations that are needed on the data (for example: assigning numbers to footnotes.)

Render takes intermediate and translates it into the format expected by the dust templates. The processing in this step should be minimal since all the logic should be in the intermediate step.
This commit is contained in:
Tom Alexander
2023-10-27 13:05:34 -04:00
parent 1ac39c2a6f
commit e3b5f7f74f
20 changed files with 64 additions and 64 deletions

59
src/intermediate/page.rs Normal file
View File

@@ -0,0 +1,59 @@
use std::path::PathBuf;
use crate::error::CustomError;
use super::IDocumentElement;
use super::IHeading;
use super::ISection;
#[derive(Debug)]
pub(crate) struct BlogPostPage {
/// Relative path from the root of the blog post.
pub(crate) path: PathBuf,
pub(crate) title: Option<String>,
pub(crate) children: Vec<IDocumentElement>,
}
impl BlogPostPage {
pub(crate) fn new<P: Into<PathBuf>>(
path: P,
document: organic::types::Document<'_>,
) -> Result<BlogPostPage, CustomError> {
let path = path.into();
let mut children = Vec::new();
if let Some(section) = document.zeroth_section.as_ref() {
children.push(IDocumentElement::Section(ISection::new(section)?));
}
for heading in document.children.iter() {
children.push(IDocumentElement::Heading(IHeading::new(heading)?));
}
Ok(BlogPostPage {
path,
title: get_title(&document),
children,
})
}
/// 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
}
}
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())
}