From 01b948cfd526b842ff987387d1e68cb94a9ee7a5 Mon Sep 17 00:00:00 2001 From: "Dawid J. Kubis" Date: Thu, 7 Oct 2021 18:30:33 +0200 Subject: [PATCH] formatting, added deunicode crate dependency, fixed output names --- Cargo.lock | 7 +++++++ Cargo.toml | 1 + src/main.rs | 53 +++++++++++++++++++++++++++++++++++++++-------------- 3 files changed, 47 insertions(+), 14 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 74247ff..38fa40a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -43,6 +43,12 @@ dependencies = [ "vec_map", ] +[[package]] +name = "deunicode" +version = "1.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f2c9736e15e7df1638a7f6eee92a6511615c738246a052af5ba86f039b65aede" + [[package]] name = "heck" version = "0.3.3" @@ -77,6 +83,7 @@ checksum = "dd8f7255a17a627354f321ef0055d63b898c6fb27eff628af4d1b66b7331edf6" name = "prezgen" version = "0.2.0" dependencies = [ + "deunicode", "structopt", "toml", ] diff --git a/Cargo.toml b/Cargo.toml index 2acfddf..cf2487f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,3 +8,4 @@ edition = "2021" [dependencies] structopt = "0.3.23" toml = "0.5.8" +deunicode = "1.3.1" diff --git a/src/main.rs b/src/main.rs index abcbdb7..e5bbf38 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,3 +1,4 @@ +use deunicode::deunicode; use structopt::StructOpt; use toml::Value; @@ -35,12 +36,12 @@ fn replace(mut template: String, rules: &[(String, String)]) -> String { /// collects table into a vector of String pairs fn collect_table(table: &Value) -> Vec<(String, String)> { - table.as_table() + table + .as_table() .unwrap() .keys() - .map( - |x| (x.clone(), String::from(table[x].as_str().unwrap())) - ).collect() + .map(|x| (x.clone(), String::from(table[x].as_str().unwrap()))) + .collect() } fn main() { @@ -63,14 +64,19 @@ fn main() { &[opt.template, PathBuf::from("front.html")] .iter() .collect::(), - ).unwrap(); - + ) + .unwrap(); // create rules - // hope to god the toml is correctly formatted + // hope to god the toml is correctly formatted // start with removing page rules - let page = value.as_table_mut().unwrap().remove("page").unwrap().clone(); + let page = value + .as_table_mut() + .unwrap() + .remove("page") + .unwrap() + .clone(); // collect global rules into a vec let global = collect_table(&value); @@ -78,18 +84,37 @@ fn main() { // iterate over pages for (index, value) in page.as_array().unwrap().iter().enumerate() { // add global variables to table - // FIXME let mut table = collect_table(&value); table.extend(global.clone()); - + // TODO make this less of a pain let content = replace(input.clone(), table.as_slice()); // create the files - let target = format!("front-{}.html", index); + let (code, day, time_start) = ( + value["code"].as_str().unwrap(), + value["day"].as_str().unwrap(), + value["time_start"].as_str().unwrap(), + ); + // tweaks + // day + let mut day = String::from(day); + day = deunicode(&day).to_uppercase(); + + // time_start + let mut time_start = String::from(time_start); + time_start = time_start.replace(":", ""); + + let target = format!("PREZ_{}_{}_{}.html", code, &day[0..2], time_start); + println!("writing {}", target); - write(&[&opt.output, &PathBuf::from(target)].iter().collect::(), content) - .unwrap(); + write( + &[&opt.output, &PathBuf::from(target)] + .iter() + .collect::(), + content, + ) + .unwrap(); } - + //println!("{:#?}", global); }