2023-10-22 17:44:03 +00:00
|
|
|
use std::path::Path;
|
|
|
|
use std::path::PathBuf;
|
|
|
|
|
|
|
|
use tokio::task::JoinHandle;
|
|
|
|
use walkdir::WalkDir;
|
|
|
|
|
|
|
|
use crate::error::CustomError;
|
|
|
|
|
2023-10-23 20:03:37 +00:00
|
|
|
use super::BlogPostPage;
|
2023-10-24 04:36:08 +00:00
|
|
|
use super::DocumentElement;
|
2023-10-23 20:03:37 +00:00
|
|
|
|
2023-10-22 16:04:09 +00:00
|
|
|
#[derive(Debug)]
|
2023-10-22 17:44:03 +00:00
|
|
|
pub(crate) struct BlogPost {
|
2023-10-22 20:01:42 +00:00
|
|
|
pub(crate) id: String,
|
2023-10-24 00:30:43 +00:00
|
|
|
pub(crate) pages: Vec<BlogPostPage>,
|
2023-10-24 04:36:08 +00:00
|
|
|
pub(crate) children: Vec<DocumentElement>,
|
2023-10-22 16:04:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl BlogPost {
|
|
|
|
pub(crate) async fn load_blog_post<P: AsRef<Path>, R: AsRef<Path>>(
|
|
|
|
root_dir: R,
|
|
|
|
post_dir: P,
|
2023-10-22 17:44:03 +00:00
|
|
|
) -> Result<BlogPost, CustomError> {
|
2023-10-24 02:10:26 +00:00
|
|
|
async fn inner(_root_dir: &Path, post_dir: &Path) -> Result<BlogPost, CustomError> {
|
2023-10-22 17:50:11 +00:00
|
|
|
let post_id = post_dir
|
|
|
|
.file_name()
|
|
|
|
.expect("The post directory should have a name.");
|
|
|
|
|
2023-10-22 16:04:09 +00: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 17:44:03 +00: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 16:04:09 +00:00
|
|
|
ret.push((path, contents, parsed));
|
|
|
|
}
|
|
|
|
ret
|
|
|
|
};
|
|
|
|
|
2023-10-23 20:03:37 +00:00
|
|
|
let pages = {
|
|
|
|
let mut ret = Vec::new();
|
|
|
|
for (real_path, _contents, parsed_document) in parsed_org_files {
|
|
|
|
let relative_to_post_dir_path = real_path.strip_prefix(post_dir)?;
|
|
|
|
ret.push(BlogPostPage::new(
|
|
|
|
relative_to_post_dir_path,
|
|
|
|
parsed_document,
|
|
|
|
)?);
|
|
|
|
}
|
|
|
|
ret
|
|
|
|
};
|
|
|
|
|
2023-10-22 16:04:09 +00:00
|
|
|
Ok(BlogPost {
|
2023-10-22 17:50:11 +00:00
|
|
|
id: post_id.to_string_lossy().into_owned(),
|
2023-10-23 20:03:37 +00:00
|
|
|
pages,
|
2023-10-24 04:36:08 +00:00
|
|
|
children: Vec::new(),
|
2023-10-22 16:04:09 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
inner(root_dir.as_ref(), post_dir.as_ref()).await
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-22 17:44:03 +00: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-10-22 16:04:09 +00:00
|
|
|
fn get_org_files<P: AsRef<Path>>(
|
|
|
|
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)
|
|
|
|
}
|