Read the org files inside the writer directory.
This commit is contained in:
parent
051e86e65a
commit
acaa12cb6e
1
Cargo.lock
generated
1
Cargo.lock
generated
@ -514,4 +514,5 @@ dependencies = [
|
|||||||
"serde",
|
"serde",
|
||||||
"tokio",
|
"tokio",
|
||||||
"toml",
|
"toml",
|
||||||
|
"walkdir",
|
||||||
]
|
]
|
||||||
|
@ -14,3 +14,4 @@ serde = { version = "1.0.189", default-features = false, features = ["std", "der
|
|||||||
tokio = { version = "1.30.0", default-features = false, features = ["rt", "rt-multi-thread", "fs", "io-util"] }
|
tokio = { version = "1.30.0", default-features = false, features = ["rt", "rt-multi-thread", "fs", "io-util"] }
|
||||||
# display, parse | indexmap, preserve_order
|
# display, parse | indexmap, preserve_order
|
||||||
toml = "0.8.2"
|
toml = "0.8.2"
|
||||||
|
walkdir = "2.4.0"
|
||||||
|
@ -1,7 +1,42 @@
|
|||||||
|
use std::path::Path;
|
||||||
|
use std::path::PathBuf;
|
||||||
|
|
||||||
use crate::cli::parameters::BuildArgs;
|
use crate::cli::parameters::BuildArgs;
|
||||||
use crate::config::Config;
|
use crate::config::Config;
|
||||||
|
use tokio::task::JoinHandle;
|
||||||
|
use walkdir::WalkDir;
|
||||||
|
|
||||||
pub(crate) async fn build_site(args: BuildArgs) -> Result<(), Box<dyn std::error::Error>> {
|
pub(crate) async fn build_site(args: BuildArgs) -> Result<(), Box<dyn std::error::Error>> {
|
||||||
let _config = Config::load_from_file(args.config).await?;
|
let config = Config::load_from_file(args.config).await?;
|
||||||
|
let org_files = get_org_files(config.get_root_directory())?;
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async fn read_file(path: PathBuf) -> std::io::Result<(PathBuf, String)> {
|
||||||
|
let contents = tokio::fs::read_to_string(&path).await?;
|
||||||
|
Ok((path, contents))
|
||||||
|
}
|
||||||
|
|
||||||
|
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)
|
||||||
|
}
|
||||||
|
@ -44,4 +44,11 @@ impl Config {
|
|||||||
.await?;
|
.await?;
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub(crate) fn get_root_directory(&self) -> &Path {
|
||||||
|
&self
|
||||||
|
.config_path
|
||||||
|
.parent()
|
||||||
|
.expect("Config file must exist inside a directory.")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user