[Web] Begin migration from TOML to YAML for the documentation tree

master
Araozu 2023-04-13 14:02:59 -05:00
parent 5ec34369e9
commit 11c30b64af
5 changed files with 124 additions and 1 deletions

View File

@ -243,6 +243,7 @@ dependencies = [
"markdown", "markdown",
"misti", "misti",
"toml", "toml",
"yaml-rust",
] ]
[[package]] [[package]]
@ -380,6 +381,12 @@ dependencies = [
"cc", "cc",
] ]
[[package]]
name = "linked-hash-map"
version = "0.5.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0717cef1bc8b636c6e1c1bbdefc09e6322da8a9321966e8928ef80d20f7f770f"
[[package]] [[package]]
name = "linux-raw-sys" name = "linux-raw-sys"
version = "0.1.4" version = "0.1.4"
@ -777,3 +784,12 @@ checksum = "ae8970b36c66498d8ff1d66685dc86b91b29db0c7739899012f63a63814b4b28"
dependencies = [ dependencies = [
"memchr", "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",
]

View File

@ -10,4 +10,4 @@ misti = { path = "../compiler"}
clap = { version = "4.2.0", features = ["derive"] } clap = { version = "4.2.0", features = ["derive"] }
markdown = "1.0.0-alpha.7" markdown = "1.0.0-alpha.7"
toml = "0.7.3" toml = "0.7.3"
yaml-rust = "0.4.5"

View File

@ -2,6 +2,7 @@ use clap::Parser;
use std::path::Path; use std::path::Path;
mod generator; mod generator;
mod pages;
mod processor; mod processor;
mod sidebar; mod sidebar;
mod utils; mod utils;

View File

@ -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<Vec<Node<'a>>>,
}
/// 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<Node> = 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)
}

View File

@ -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) { 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(); let mut toml_file_path = current_path.canonicalize().unwrap();
toml_file_path.push("index.toml"); toml_file_path.push("index.toml");