2023-10-22 13:44:03 -04:00
|
|
|
use std::path::Path;
|
|
|
|
use std::path::PathBuf;
|
2023-10-29 21:19:30 -04:00
|
|
|
use std::sync::Arc;
|
|
|
|
use std::sync::Mutex;
|
2023-10-22 13:44:03 -04:00
|
|
|
|
|
|
|
use tokio::task::JoinHandle;
|
|
|
|
use walkdir::WalkDir;
|
|
|
|
|
|
|
|
use crate::error::CustomError;
|
2023-12-23 16:37:19 -05:00
|
|
|
use crate::intermediate::blog_post_page::BlogPostPageInput;
|
2023-10-27 14:43:06 -04:00
|
|
|
use crate::intermediate::registry::Registry;
|
2023-12-21 13:53:56 -05:00
|
|
|
use crate::intermediate::IntermediateContext;
|
2023-10-22 13:44:03 -04:00
|
|
|
|
2023-10-23 16:03:37 -04:00
|
|
|
use super::BlogPostPage;
|
|
|
|
|
2023-10-22 12:04:09 -04:00
|
|
|
#[derive(Debug)]
|
2023-10-22 13:44:03 -04:00
|
|
|
pub(crate) struct BlogPost {
|
2023-10-22 16:01:42 -04:00
|
|
|
pub(crate) id: String,
|
2023-10-23 20:30:43 -04:00
|
|
|
pub(crate) pages: Vec<BlogPostPage>,
|
2023-10-22 12:04:09 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
impl BlogPost {
|
2024-10-19 16:55:38 -04:00
|
|
|
pub(crate) async fn load_blog_post<P: AsRef<Path>, R: AsRef<Path>, S: AsRef<Path>>(
|
2023-10-22 12:04:09 -04:00
|
|
|
root_dir: R,
|
2024-10-19 16:55:38 -04:00
|
|
|
posts_dir: S,
|
2023-10-22 12:04:09 -04:00
|
|
|
post_dir: P,
|
2023-10-22 13:44:03 -04:00
|
|
|
) -> Result<BlogPost, CustomError> {
|
2024-10-19 16:55:38 -04:00
|
|
|
async fn inner(
|
|
|
|
_root_dir: &Path,
|
|
|
|
posts_dir: &Path,
|
|
|
|
post_dir: &Path,
|
|
|
|
) -> Result<BlogPost, CustomError> {
|
|
|
|
let post_id = post_dir.strip_prefix(posts_dir)?.as_os_str();
|
2023-10-22 13:50:11 -04:00
|
|
|
|
2023-10-22 12:04:09 -04:00
|
|
|
let org_files = {
|
|
|
|
let mut ret = Vec::new();
|
|
|
|
let org_files_iter = get_org_files(post_dir)?;
|
|
|
|
for entry in org_files_iter {
|
|
|
|
ret.push(entry.await??);
|
|
|
|
}
|
|
|
|
ret
|
|
|
|
};
|
|
|
|
let parsed_org_files = {
|
|
|
|
let mut ret = Vec::new();
|
|
|
|
for (path, contents) in org_files.iter() {
|
2023-10-22 13:44:03 -04:00
|
|
|
let parsed = organic::parser::parse_file(contents.as_str(), Some(path))
|
|
|
|
.map_err(|_| CustomError::Static("Failed to parse org-mode document."))?;
|
2023-10-22 12:04:09 -04:00
|
|
|
ret.push((path, contents, parsed));
|
|
|
|
}
|
|
|
|
ret
|
|
|
|
};
|
|
|
|
|
2023-10-23 16:03:37 -04:00
|
|
|
let pages = {
|
|
|
|
let mut ret = Vec::new();
|
2023-10-27 14:43:06 -04:00
|
|
|
for (real_path, _contents, parsed_document) in parsed_org_files.iter() {
|
2023-10-29 13:51:32 -04:00
|
|
|
let mut registry = Registry::new();
|
|
|
|
|
|
|
|
// Assign IDs to the targets
|
|
|
|
organic::types::AstNode::from(parsed_document)
|
|
|
|
.iter_all_ast_nodes()
|
2023-12-23 06:38:23 -05:00
|
|
|
.for_each(|node| {
|
|
|
|
if let organic::types::AstNode::Target(target) = node {
|
2023-10-29 13:51:32 -04:00
|
|
|
registry.get_target(target.value);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2023-10-29 21:19:30 -04:00
|
|
|
let registry = Arc::new(Mutex::new(registry));
|
2023-12-21 13:53:56 -05:00
|
|
|
let intermediate_context = IntermediateContext::new(registry)?;
|
2023-10-23 16:03:37 -04:00
|
|
|
let relative_to_post_dir_path = real_path.strip_prefix(post_dir)?;
|
2023-10-27 15:55:19 -04:00
|
|
|
ret.push(
|
2023-12-19 17:09:11 -05:00
|
|
|
BlogPostPage::new(
|
2023-12-21 13:53:56 -05:00
|
|
|
intermediate_context,
|
2023-12-19 17:09:11 -05:00
|
|
|
BlogPostPageInput::new(relative_to_post_dir_path, parsed_document),
|
|
|
|
)
|
|
|
|
.await?,
|
2023-10-27 15:55:19 -04:00
|
|
|
);
|
2023-10-23 16:03:37 -04:00
|
|
|
}
|
|
|
|
ret
|
|
|
|
};
|
|
|
|
|
2023-10-22 12:04:09 -04:00
|
|
|
Ok(BlogPost {
|
2023-10-22 13:50:11 -04:00
|
|
|
id: post_id.to_string_lossy().into_owned(),
|
2023-10-23 16:03:37 -04:00
|
|
|
pages,
|
2023-10-22 12:04:09 -04:00
|
|
|
})
|
|
|
|
}
|
2024-10-19 16:55:38 -04:00
|
|
|
inner(root_dir.as_ref(), posts_dir.as_ref(), post_dir.as_ref()).await
|
2023-10-22 12:04:09 -04:00
|
|
|
}
|
2023-12-17 15:45:50 -05:00
|
|
|
|
|
|
|
/// Get the date for a blog post.
|
|
|
|
///
|
|
|
|
/// The date is set by the "#+date" export setting. This will
|
|
|
|
/// first attempt to read the date from an index.org if such a
|
|
|
|
/// file exists. If that file does not exist or that file does not
|
|
|
|
/// contain a date export setting, then this will iterate through
|
|
|
|
/// all the pages under the blog post looking for any page that
|
|
|
|
/// contains a date export setting. It will return the first date
|
|
|
|
/// found.
|
2023-12-17 16:57:37 -05:00
|
|
|
pub(crate) fn get_date(&self) -> Option<&str> {
|
|
|
|
let index_page_date = self
|
|
|
|
.get_index_page()
|
2023-12-23 06:38:23 -05:00
|
|
|
.and_then(|index_page| index_page.date.as_deref());
|
2023-12-17 16:57:37 -05:00
|
|
|
if index_page_date.is_some() {
|
|
|
|
return index_page_date;
|
2023-12-17 15:45:50 -05:00
|
|
|
}
|
2023-12-17 16:57:37 -05:00
|
|
|
|
|
|
|
self.pages
|
|
|
|
.iter()
|
2023-12-23 06:38:23 -05:00
|
|
|
.filter_map(|page| page.date.as_deref())
|
2023-12-17 16:57:37 -05:00
|
|
|
.next()
|
2023-12-17 15:45:50 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Get the blog post page for index.org
|
2023-12-19 10:59:34 -05:00
|
|
|
pub(crate) fn get_index_page(&self) -> Option<&BlogPostPage> {
|
2023-12-17 16:57:37 -05:00
|
|
|
self.pages
|
2023-12-17 15:45:50 -05:00
|
|
|
.iter()
|
2023-12-17 16:57:37 -05:00
|
|
|
.find(|page| page.path == Path::new("index.org"))
|
2023-12-17 15:45:50 -05:00
|
|
|
}
|
2023-10-22 12:04:09 -04:00
|
|
|
}
|
|
|
|
|
2023-10-22 13:44:03 -04:00
|
|
|
async fn read_file(path: PathBuf) -> std::io::Result<(PathBuf, String)> {
|
|
|
|
let contents = tokio::fs::read_to_string(&path).await?;
|
|
|
|
Ok((path, contents))
|
|
|
|
}
|
|
|
|
|
2023-12-23 16:37:19 -05:00
|
|
|
pub(crate) fn get_org_files<P: AsRef<Path>>(
|
2023-10-22 12:04:09 -04:00
|
|
|
root_dir: P,
|
|
|
|
) -> Result<impl Iterator<Item = JoinHandle<std::io::Result<(PathBuf, String)>>>, walkdir::Error> {
|
|
|
|
let org_files = WalkDir::new(root_dir)
|
|
|
|
.into_iter()
|
|
|
|
.filter(|e| match e {
|
|
|
|
Ok(dir_entry) => {
|
|
|
|
dir_entry.file_type().is_file()
|
|
|
|
&& Path::new(dir_entry.file_name())
|
|
|
|
.extension()
|
|
|
|
.map(|ext| ext.to_ascii_lowercase() == "org")
|
|
|
|
.unwrap_or(false)
|
|
|
|
}
|
|
|
|
Err(_) => true,
|
|
|
|
})
|
|
|
|
.collect::<Result<Vec<_>, _>>()?;
|
|
|
|
let org_files = org_files
|
|
|
|
.into_iter()
|
|
|
|
.map(walkdir::DirEntry::into_path)
|
|
|
|
.map(|path| tokio::spawn(read_file(path)));
|
|
|
|
Ok(org_files)
|
|
|
|
}
|