diff --git a/doc-generator/Cargo.lock b/doc-generator/Cargo.lock index e234391..0f42418 100644 --- a/doc-generator/Cargo.lock +++ b/doc-generator/Cargo.lock @@ -243,6 +243,7 @@ dependencies = [ "markdown", "misti", "toml", + "yaml-rust", ] [[package]] @@ -380,6 +381,12 @@ dependencies = [ "cc", ] +[[package]] +name = "linked-hash-map" +version = "0.5.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0717cef1bc8b636c6e1c1bbdefc09e6322da8a9321966e8928ef80d20f7f770f" + [[package]] name = "linux-raw-sys" version = "0.1.4" @@ -777,3 +784,12 @@ checksum = "ae8970b36c66498d8ff1d66685dc86b91b29db0c7739899012f63a63814b4b28" dependencies = [ "memchr", ] + +[[package]] +name = "yaml-rust" +version = "0.4.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "56c1936c4cc7a1c9ab21a1ebb602eb942ba868cbd44a99cb7cdc5892335e1c85" +dependencies = [ + "linked-hash-map", +] diff --git a/doc-generator/Cargo.toml b/doc-generator/Cargo.toml index 130898e..eba76cf 100644 --- a/doc-generator/Cargo.toml +++ b/doc-generator/Cargo.toml @@ -10,4 +10,4 @@ misti = { path = "../compiler"} clap = { version = "4.2.0", features = ["derive"] } markdown = "1.0.0-alpha.7" toml = "0.7.3" - +yaml-rust = "0.4.5" diff --git a/doc-generator/src/main.rs b/doc-generator/src/main.rs index b577ecb..ee4b183 100644 --- a/doc-generator/src/main.rs +++ b/doc-generator/src/main.rs @@ -2,6 +2,7 @@ use clap::Parser; use std::path::Path; mod generator; +mod pages; mod processor; mod sidebar; mod utils; diff --git a/doc-generator/src/pages/mod.rs b/doc-generator/src/pages/mod.rs new file mode 100644 index 0000000..a04095b --- /dev/null +++ b/doc-generator/src/pages/mod.rs @@ -0,0 +1,105 @@ +use std::{path::Path, fs}; + +use yaml_rust::{Yaml, YamlLoader}; + +pub enum Node<'a> { + File(File<'a>), + Folder(Folder<'a>), +} + +pub struct File<'a> { + /// Name of the file + path: &'a String, +} + +pub struct Folder<'a> { + /// Name of the folder + path: &'a String, + /// Display name of the folder + name: &'a String, + /// If true, then there MUST be a `File {path: "index"}` in the `children` field + has_index: bool, + /// Sub files or folders + children: Box>>, +} + +/// Creates a `YAML::String` from a `&str` +macro_rules! y_str { + ($str:literal) => { + &Yaml::String(String::from($str)) + }; +} + +fn generate_pages_tree(values: &Yaml) -> Node { + let Yaml::Hash(table) = values + else {panic!("YAML: input MUST be an object")}; + + // Node path + let Yaml::String(path) = table.get(y_str!("path")).expect("YAML: Node MUST have a `path` key") + else { panic!("YAML: `path` MUST be a String") }; + + let input_data = ( + table.get(y_str!("name")), + table.get(y_str!("has_index")), + table.get(y_str!("children")), + ); + + match input_data { + (None, None, None) => Node::File(File { path }), + (Some(name), has_index, Some(children)) => { + let Yaml::String(name) = name + else { panic!("YAML: `name` MUST be a String") }; + + let has_index = match has_index { + Some(Yaml::Boolean(v)) => *v, + Some(_) => panic!("YAML: if key `has_index` exists, it MUST be a Boolean"), + None => false, + }; + + let Yaml::Array(children) = children + else {panic!("YAML: `children` MUST be an Array")}; + + let children_nodes: Vec = children + .into_iter() + .map(|values| { + generate_pages_tree(values) + }) + .collect(); + + Node::Folder(Folder { + path, + name, + has_index, + children: Box::new(children_nodes), + }) + } + _ => { + panic!("YAML: A Node is missing a `name` or `children` key") + } + } +} + +fn generate_pages_html(file_tree: &Node) -> String { + match file_tree { + Node::File(file) => { + String::from("File :D") + } + Node::Folder(folder) => { + String::from("Folder :D") + } + } +} + +pub fn generate_pages(yaml_folder: &Path) -> String { + let mut yaml_path = yaml_folder.canonicalize().unwrap(); + yaml_path.push("index.yaml"); + + 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"); + + let yaml_docs = YamlLoader::load_from_str(yaml.as_str()).expect("YAML file MUST contain valid YAML"); + let yaml = &yaml_docs[0]; + + let root_node = generate_pages_tree(yaml); + generate_pages_html(&root_node) +} diff --git a/doc-generator/src/processor.rs b/doc-generator/src/processor.rs index 508fbe3..9032f56 100644 --- a/doc-generator/src/processor.rs +++ b/doc-generator/src/processor.rs @@ -59,6 +59,7 @@ pub fn search_config_file(current_path: &Path, input_folder: &Path, output_folde } fn process_toml(current_path: &Path, input_folder: &Path, output_folder: &Path) { + println!("YAML:{}", crate::pages::generate_pages(current_path)); let mut toml_file_path = current_path.canonicalize().unwrap(); toml_file_path.push("index.toml");