2023-04-14 01:21:03 +00:00
|
|
|
use crate::pages::{compile_md_to_html, generate_pages_html, parse_yaml};
|
|
|
|
use crate::utils;
|
2023-04-13 02:15:00 +00:00
|
|
|
use std::{
|
2023-04-14 01:21:03 +00:00
|
|
|
fs,
|
|
|
|
path::Path,
|
2023-04-13 02:15:00 +00:00
|
|
|
};
|
2023-04-14 01:21:03 +00:00
|
|
|
use yaml_rust::YamlLoader;
|
2023-04-13 02:15:00 +00:00
|
|
|
|
|
|
|
enum EntryFound {
|
2023-04-14 01:21:03 +00:00
|
|
|
YamlFile,
|
2023-04-13 02:15:00 +00:00
|
|
|
OtherFile,
|
|
|
|
None,
|
|
|
|
}
|
|
|
|
|
2023-04-14 01:21:03 +00:00
|
|
|
// Traverses the current path searching for a YAML file
|
2023-04-13 02:15:00 +00:00
|
|
|
pub fn search_config_file(current_path: &Path, input_folder: &Path, output_folder: &Path) {
|
2023-04-14 01:21:03 +00:00
|
|
|
// Iterate over all the files searching for a YAML file
|
2023-04-13 02:15:00 +00:00
|
|
|
let result = current_path
|
|
|
|
.read_dir()
|
|
|
|
.unwrap()
|
|
|
|
.fold(&EntryFound::None, |acc, next| {
|
|
|
|
let p = next.unwrap().path();
|
|
|
|
let is_file = p.is_file();
|
|
|
|
let ext = p.extension();
|
|
|
|
|
|
|
|
match (acc, is_file, ext) {
|
2023-04-14 01:21:03 +00:00
|
|
|
(EntryFound::YamlFile, true, Some(x)) if x == "yaml" => {
|
|
|
|
panic!("FOUND A SECOND YAML FILE!!!")
|
2023-04-13 02:15:00 +00:00
|
|
|
}
|
2023-04-14 01:21:03 +00:00
|
|
|
(EntryFound::YamlFile, _, _) => acc,
|
|
|
|
(EntryFound::OtherFile, true, Some(x)) if x == "yaml" => &EntryFound::YamlFile,
|
|
|
|
(EntryFound::None, true, Some(x)) if x == "yaml" => &EntryFound::YamlFile,
|
2023-04-13 02:15:00 +00:00
|
|
|
(EntryFound::None, true, Some(_)) => &EntryFound::OtherFile,
|
|
|
|
_ => acc,
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
match result {
|
2023-04-14 01:21:03 +00:00
|
|
|
// If a file other than a YAML file is found, panic
|
2023-04-13 02:15:00 +00:00
|
|
|
EntryFound::OtherFile => panic!(
|
2023-04-14 01:21:03 +00:00
|
|
|
"Found an orphan file without a YAML parent at {:?}",
|
2023-04-13 02:15:00 +00:00
|
|
|
current_path
|
|
|
|
),
|
2023-04-14 01:21:03 +00:00
|
|
|
// Process the YAML file
|
|
|
|
EntryFound::YamlFile => process_yaml(current_path, input_folder, output_folder),
|
2023-04-13 02:15:00 +00:00
|
|
|
// No files found, recursively read children folders
|
|
|
|
EntryFound::None => {
|
|
|
|
for entry in current_path.read_dir().unwrap() {
|
|
|
|
// Should always succeed, and countain a folder
|
|
|
|
let x = entry.unwrap();
|
|
|
|
let path = x.path();
|
|
|
|
|
2023-04-14 01:21:03 +00:00
|
|
|
utils::ensure_folder_exists(&path, input_folder, output_folder).unwrap();
|
2023-04-13 02:15:00 +00:00
|
|
|
search_config_file(&path, input_folder, output_folder);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2023-04-14 01:21:03 +00:00
|
|
|
fn process_yaml(current_path: &Path, input_folder: &Path, output_folder: &Path) {
|
2023-04-13 02:15:00 +00:00
|
|
|
//
|
2023-04-14 01:21:03 +00:00
|
|
|
// Read YAML file
|
2023-04-13 02:15:00 +00:00
|
|
|
//
|
2023-04-14 01:21:03 +00:00
|
|
|
let mut yaml_path = current_path.canonicalize().unwrap();
|
|
|
|
yaml_path.push("index.yaml");
|
2023-04-13 02:15:00 +00:00
|
|
|
|
2023-04-14 01:21:03 +00:00
|
|
|
let yaml_bytes = fs::read(yaml_path).expect("File index.yaml MUST exist");
|
|
|
|
let yaml = String::from_utf8(yaml_bytes).expect("YAML index file MUST be valid UTF-8");
|
2023-04-13 02:15:00 +00:00
|
|
|
|
2023-04-14 01:21:03 +00:00
|
|
|
let yaml_docs =
|
|
|
|
YamlLoader::load_from_str(yaml.as_str()).expect("YAML file MUST contain valid YAML");
|
|
|
|
let yaml = &yaml_docs[0];
|
2023-04-13 02:15:00 +00:00
|
|
|
|
|
|
|
//
|
2023-04-14 01:21:03 +00:00
|
|
|
// Parse YAML
|
2023-04-13 02:15:00 +00:00
|
|
|
//
|
2023-04-14 01:21:03 +00:00
|
|
|
let file_tree = parse_yaml(&yaml);
|
2023-04-13 02:15:00 +00:00
|
|
|
|
2023-04-14 01:21:03 +00:00
|
|
|
//
|
|
|
|
// Generate File Tree HTML
|
|
|
|
//
|
|
|
|
let tree_html = {
|
|
|
|
let input_folder = input_folder.canonicalize().unwrap();
|
|
|
|
let yaml_folder_temp = current_path.canonicalize().unwrap();
|
|
|
|
let web_absolute_path = yaml_folder_temp.strip_prefix(input_folder).unwrap();
|
2023-04-13 02:15:00 +00:00
|
|
|
|
2023-04-14 01:21:03 +00:00
|
|
|
generate_pages_html(&file_tree, web_absolute_path)
|
|
|
|
};
|
2023-04-13 02:15:00 +00:00
|
|
|
|
2023-04-14 01:21:03 +00:00
|
|
|
//
|
|
|
|
// Compile MD to HTML
|
|
|
|
//
|
|
|
|
compile_md_to_html(
|
|
|
|
&file_tree,
|
|
|
|
current_path,
|
|
|
|
input_folder,
|
|
|
|
output_folder,
|
|
|
|
&tree_html,
|
|
|
|
);
|
2023-04-13 02:15:00 +00:00
|
|
|
}
|