From f31d940ed0b9b67b92212adcc01b44fc4d528f69 Mon Sep 17 00:00:00 2001 From: curly Date: Wed, 28 Dec 2022 15:25:22 -0700 Subject: initial --- .gitignore | 1 + Cargo.lock | 25 ++++++++++++++ Cargo.toml | 9 +++++ TODO | 4 +++ settings.toml | 14 ++++++++ src/lib.rs | 108 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/main.rs | 72 +++++++++++++++++++++++++++++++++++++++ 7 files changed, 233 insertions(+) create mode 100644 .gitignore create mode 100644 Cargo.lock create mode 100644 Cargo.toml create mode 100644 TODO create mode 100644 settings.toml create mode 100644 src/lib.rs create mode 100644 src/main.rs diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ea8c4bf --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/target diff --git a/Cargo.lock b/Cargo.lock new file mode 100644 index 0000000..e8c46fd --- /dev/null +++ b/Cargo.lock @@ -0,0 +1,25 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "serde" +version = "1.0.152" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bb7d1f0d3021d347a83e556fc4683dea2ea09d87bccdf88ff5c12545d89d5efb" + +[[package]] +name = "simple_static_site_generator" +version = "0.1.0" +dependencies = [ + "toml", +] + +[[package]] +name = "toml" +version = "0.5.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1333c76748e868a4d9d1017b5ab53171dfd095f70c712fdb4653a406547f598f" +dependencies = [ + "serde", +] diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..86009c7 --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,9 @@ +[package] +name = "simple_static_site_generator" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +toml = "0.5.10" \ No newline at end of file diff --git a/TODO b/TODO new file mode 100644 index 0000000..1b92c0f --- /dev/null +++ b/TODO @@ -0,0 +1,4 @@ +- Scan sets of paths and recurse on finding a directory, + creating at the same time in the build folder + - Checking the exclude and no-scan lists should be done + at this time \ No newline at end of file diff --git a/settings.toml b/settings.toml new file mode 100644 index 0000000..ded0571 --- /dev/null +++ b/settings.toml @@ -0,0 +1,14 @@ +[config] +source = "src" +build_dir = "build" + +do_not_scan = ["assets"] +exclude = ["nav.html"] + +file_delim = ":?" +command_delim = ":!" +commands_file = "settings.toml" + +[commands] +"b" = "" +"/b" = "" diff --git a/src/lib.rs b/src/lib.rs new file mode 100644 index 0000000..3edd379 --- /dev/null +++ b/src/lib.rs @@ -0,0 +1,108 @@ +use std::{fs, path}; +use std::io::ErrorKind; +use toml; + +pub struct Config { + config: toml::Value, + commands: toml::Value, +} +impl Config { + pub fn new() -> Config { + println!("Using built-in config"); + Config { + config: toml::toml!{ + source = "src" + build_dir = "build" + + do_not_scan = [] + exclude = [] + + file_delim = ":?" + command_delim = ":!" + commands_file = "commands.toml" + }, + commands: toml::toml!{ + empty = "" + } + } + } + + pub fn import(config: &str) -> Config { + println!("Using found config"); + let config: toml::Value = toml::from_str(config).expect("Could not load config"); + let config = config.get("config").expect("Could not find \"[config]\" field"); + + // Attempt to read commands file + let commands: toml::Value = match config.get("commands_file") { + Some(n) => { + if n.is_str() { + let file_string = n.as_str().unwrap(); + let file_string = match fs::read_to_string(file_string) { + Ok(n) => n, + Err(_) => {println!("Could not load commands from file"); String::from("[commands]")}, + }; + toml::from_str(file_string.as_str()).expect("Invalid toml") + } else { + panic!() + } + }, + None => {println!("Could not load commands from file"); toml::toml!{[commands]}}, + }; + let commands = commands.get("commands").expect("Could not find \"[commands]\" field"); + + Config { + config: config.clone(), + commands: commands.clone(), + } + } + + pub fn get_str(&self, string: &str) -> Option<&str> { + match self.config.get(string) { + Some(n) => n.as_str(), + None => None + } + } + + pub fn get_cmd_str(&self, string: &str) -> Option<&str> { + match self.commands.get(string) { + Some(n) => n.as_str(), + None => None + } + } +} + + +pub fn create_dir(path: &str) -> Result<(), ()> { + match fs::create_dir(std::path::PathBuf::from(path)) { + Ok(_) => (), + Err(n) => match n.kind() { + ErrorKind::AlreadyExists => (), + _ => return Err(()), + } + } + return Ok(()) +} + +pub fn remove_path_head(path: &path::PathBuf) -> path::PathBuf { + let mut path = path.to_str().unwrap().split("/"); + path.next(); + let path: path::PathBuf = path.collect(); + + return path +} + +pub fn append_path_head(path: &path::PathBuf, head: &str) -> path::PathBuf { + let mut head = path::PathBuf::from(head); + head.push(path); + return head +} + +pub fn replace_path_head(path: &path::PathBuf, head: &str) -> path::PathBuf { + let path = remove_path_head(path); + append_path_head(&path, head) +} + +pub fn remove_path_tail(path: &mut path::PathBuf) -> &path::PathBuf { + path.pop(); + path +} \ No newline at end of file diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..a6f8e1f --- /dev/null +++ b/src/main.rs @@ -0,0 +1,72 @@ +use std::fs; + +fn main() { + + let config = match fs::read_to_string("settings.toml") { + Ok(n) => simple_static_site_generator::Config::import(&(n.as_str())), + Err(n) => {print!("\"settings.toml\": {}: ", n); simple_static_site_generator::Config::new()}, + }; + + println!("{}", config.get_str("source").unwrap()); + + let mut path_vec = vec![]; + let mut ex_path_vec = vec![]; + + let dirs = fs::read_dir("src").unwrap(); + for dir in dirs { + + let path = dir.as_ref().unwrap().path(); + if path != std::path::PathBuf::from("assets") { + path_vec.push(path); + } else { + ex_path_vec.push(path); + } + } + + println!("INCLUDED"); + for x in &path_vec { + println!("{:?}", x) + } + println!("EXCLUDED"); + for x in &ex_path_vec { + println!("{:?}", x) + } + println!(); + + + // create build directory or panic + simple_static_site_generator::create_dir("build").unwrap(); + + for path in path_vec { + let mut file_string = String::new(); + + if path.is_dir() { + // RECURSE HERE + continue + } + + for line in fs::read_to_string(&path).unwrap().lines() { + if line.contains(":?") { + let v: Vec<&str> = line.trim().split(":?").collect(); + let v = v.get(1).unwrap(); + + println!("INSERTING {} into {:?}", v, path); + + file_string.push_str(fs::read_to_string(v).unwrap().as_ref()); + } else { + file_string.push_str(line); + file_string.push('\n') + } + } + + + let dest = simple_static_site_generator::replace_path_head(&path, "build/"); + + // NEEDS TO CREATE DIRECTORY STRUCTURE + fs::write(dest, file_string).expect("Could not write file"); + + } + + + // NEEDS TO COPY NOT SCANNED DIRECTORIES +} \ No newline at end of file -- cgit v1.2.3