diff --git a/Cargo.lock b/Cargo.lock index d28da08..b43e8a6 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4,13 +4,63 @@ version = 4 [[package]] name = "aho-corasick" -version = "1.1.3" +version = "1.1.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8e60d3430d3a69478ad0993f19238d2df97c507009a52b3c10addcd7f6bcb916" +checksum = "ddd31a130427c27518df266943a5308ed92d4b226cc639f5a8f1002816174301" dependencies = [ "memchr", ] +[[package]] +name = "anstream" +version = "0.6.21" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "43d5b281e737544384e969a5ccad3f1cdd24b48086a0fc1b2a5262a26b8f4f4a" +dependencies = [ + "anstyle", + "anstyle-parse", + "anstyle-query", + "anstyle-wincon", + "colorchoice", + "is_terminal_polyfill", + "utf8parse", +] + +[[package]] +name = "anstyle" +version = "1.0.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5192cca8006f1fd4f7237516f40fa183bb07f8fbdfedaa0036de5ea9b0b45e78" + +[[package]] +name = "anstyle-parse" +version = "0.2.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4e7644824f0aa2c7b9384579234ef10eb7efb6a0deb83f9630a49594dd9c15c2" +dependencies = [ + "utf8parse", +] + +[[package]] +name = "anstyle-query" +version = "1.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "40c48f72fd53cd289104fc64099abca73db4166ad86ea0b4341abe65af83dadc" +dependencies = [ + "windows-sys", +] + +[[package]] +name = "anstyle-wincon" +version = "3.0.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "291e6a250ff86cd4a820112fb8898808a366d8f9f58ce16d1f538353ad55747d" +dependencies = [ + "anstyle", + "once_cell_polyfill", + "windows-sys", +] + [[package]] name = "arraydeque" version = "0.5.1" @@ -19,15 +69,61 @@ checksum = "7d902e3d592a523def97af8f317b08ce16b7ab854c1985a0c671e6f15cebc236" [[package]] name = "bitflags" -version = "2.9.3" +version = "2.10.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "34efbcccd345379ca2868b2b2c9d3782e9cc58ba87bc7d79d5b53d9c9ae6f25d" +checksum = "812e12b5285cc515a9c72a5c1d3b6d46a19dac5acfef5265968c166106e31dd3" [[package]] name = "cfg-if" -version = "1.0.3" +version = "1.0.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2fd1289c04a9ea8cb22300a459a72a385d7c73d3259e2ed7dcb2af674838cfa9" +checksum = "9330f8b2ff13f34540b44e946ef35111825727b38d33286ef986142615121801" + +[[package]] +name = "clap" +version = "4.5.55" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3e34525d5bbbd55da2bb745d34b36121baac88d07619a9a09cfcf4a6c0832785" +dependencies = [ + "clap_builder", + "clap_derive", +] + +[[package]] +name = "clap_builder" +version = "4.5.55" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "59a20016a20a3da95bef50ec7238dbd09baeef4311dcdd38ec15aba69812fb61" +dependencies = [ + "anstream", + "anstyle", + "clap_lex", + "strsim", +] + +[[package]] +name = "clap_derive" +version = "4.5.55" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a92793da1a46a5f2a02a6f4c46c6496b28c43638adea8306fcb0caa1634f24e5" +dependencies = [ + "heck", + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "clap_lex" +version = "0.7.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c3e64b0cc0439b12df2fa678eae89a1c56a529fd067a9115f7827f1fffd22b32" + +[[package]] +name = "colorchoice" +version = "1.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b05b61dc5112cbb17e4b6cd61790d9845d13888356391624cbe7e41efeac1e75" [[package]] name = "encoding_rs" @@ -46,9 +142,9 @@ checksum = "d9c4f5dac5e15c24eb999c26181a6ca40b39fe946cbe4c263c7209467bc83af2" [[package]] name = "getopts" -version = "0.2.23" +version = "0.2.24" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cba6ae63eb948698e300f645f87c70f76630d505f23b8907cf1e193ee85048c1" +checksum = "cfe4fbac503b8d1f88e6676011885f34b7174f46e59956bba534ba83abded4df" dependencies = [ "unicode-width", ] @@ -72,16 +168,28 @@ dependencies = [ ] [[package]] -name = "itoa" -version = "1.0.15" +name = "heck" +version = "0.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4a5f13b858c8d314ee3e8f639011f7ccefe71f97f96e50151fb991f267928e2c" +checksum = "2304e00983f87ffb38b55b444b5e3b60a884b5d30c0fca7d82fe33449bbe55ea" + +[[package]] +name = "is_terminal_polyfill" +version = "1.70.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a6cb138bb79a146c1bd460005623e142ef0181e3d0219cb493e02f7d08a35695" + +[[package]] +name = "itoa" +version = "1.0.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "92ecc6618181def0457392ccd0ee51198e065e016d1d527a7ac1b6dc7c1f09d2" [[package]] name = "memchr" -version = "2.7.5" +version = "2.7.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "32a282da65faaf38286cf3be983213fcf1d2e2a58700e808f83f4ea9a4804bc0" +checksum = "f52b00d39961fc5b2736ea853c9cc86238e165017a493d1d5c8eac6bdc4cc273" [[package]] name = "memo-map" @@ -91,9 +199,9 @@ checksum = "38d1115007560874e373613744c6fba374c17688327a71c1476d1a5954cc857b" [[package]] name = "minijinja" -version = "2.12.0" +version = "2.15.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a9f264d75233323f4b7d2f03aefe8a990690cdebfbfe26ea86bcbaec5e9ac990" +checksum = "b479616bb6f0779fb0f3964246beda02d4b01144e1b0d5519616e012ccc2a245" dependencies = [ "memo-map", "self_cell", @@ -102,10 +210,16 @@ dependencies = [ ] [[package]] -name = "proc-macro2" -version = "1.0.101" +name = "once_cell_polyfill" +version = "1.70.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "89ae43fd86e4158d6db51ad8e2b80f313af9cc74f5c0e03ccb87de09998732de" +checksum = "384b8ab6d37215f3c5301a95a4accb5d64aa607f1fcb26a11b5303878451b4fe" + +[[package]] +name = "proc-macro2" +version = "1.0.106" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8fd00f0bb2e90d81d1044c2b32617f68fcb9fa3bb7640c23e9c748e53fb30934" dependencies = [ "unicode-ident", ] @@ -132,18 +246,18 @@ checksum = "007d8adb5ddab6f8e3f491ac63566a7d5002cc7ed73901f72057943fa71ae1ae" [[package]] name = "quote" -version = "1.0.40" +version = "1.0.44" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1885c039570dc00dcb4ff087a89e185fd56bae234ddc7f056a945bf36467248d" +checksum = "21b2ebcf727b7760c461f091f9f0f539b77b8e87f2fd88131e7f1b433b3cece4" dependencies = [ "proc-macro2", ] [[package]] name = "regex" -version = "1.11.2" +version = "1.12.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "23d7fd106d8c02486a8d64e778353d1cffe08ce79ac2e82f540c86d0facf6912" +checksum = "843bc0191f75f3e22651ae5f1e72939ab2f72a4bc30fa80a066bd66edefc24d4" dependencies = [ "aho-corasick", "memchr", @@ -153,9 +267,9 @@ dependencies = [ [[package]] name = "regex-automata" -version = "0.4.10" +version = "0.4.13" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6b9458fa0bfeeac22b5ca447c63aaf45f28439a709ccd244698632f9aa6394d6" +checksum = "5276caf25ac86c8d810222b3dbb938e512c55c6831a10f3e6ed1c93b84041f1c" dependencies = [ "aho-corasick", "memchr", @@ -164,36 +278,40 @@ dependencies = [ [[package]] name = "regex-syntax" -version = "0.8.6" +version = "0.8.8" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "caf4aa5b0f434c91fe5c7f1ecb6a5ece2130b02ad2a590589dda5146df959001" - -[[package]] -name = "ryu" -version = "1.0.20" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "28d3b2b1366ec20994f1fd18c3c594f05c5dd4bc44d8bb0c1c632c8d6829481f" +checksum = "7a2d987857b319362043e95f5353c0535c1f58eec5336fdfcf626430af7def58" [[package]] name = "self_cell" -version = "1.2.0" +version = "1.2.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0f7d95a54511e0c7be3f51e8867aa8cf35148d7b9445d44de2f943e2b206e749" +checksum = "b12e76d157a900eb52e81bc6e9f3069344290341720e9178cde2407113ac8d89" [[package]] name = "serde" -version = "1.0.219" +version = "1.0.228" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5f0e2c6ed6606019b4e29e69dbaba95b11854410e5347d525002456dbbb786b6" +checksum = "9a8e94ea7f378bd32cbbd37198a4a91436180c5bb472411e48b5ec2e2124ae9e" +dependencies = [ + "serde_core", + "serde_derive", +] + +[[package]] +name = "serde_core" +version = "1.0.228" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "41d385c7d4ca58e59fc732af25c3983b67ac852c1a25000afe1175de458b67ad" dependencies = [ "serde_derive", ] [[package]] name = "serde_derive" -version = "1.0.219" +version = "1.0.228" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5b0276cf7f2c73365f7157c8123c21cd9a50fbbd844757af28ca1f5925fc2a00" +checksum = "d540f220d3187173da220f885ab66608367b6574e925011a9353e4badda91d79" dependencies = [ "proc-macro2", "quote", @@ -202,21 +320,28 @@ dependencies = [ [[package]] name = "serde_json" -version = "1.0.143" +version = "1.0.149" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d401abef1d108fbd9cbaebc3e46611f4b1021f714a0597a71f41ee463f5f4a5a" +checksum = "83fc039473c5595ace860d8c4fafa220ff474b3fc6bfdb4293327f1a37e94d86" dependencies = [ "itoa", "memchr", - "ryu", "serde", + "serde_core", + "zmij", ] [[package]] -name = "syn" -version = "2.0.106" +name = "strsim" +version = "0.11.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ede7c438028d4436d71104916910f5bb611972c5cfd7f89b8300a8186e6fada6" +checksum = "7da8b5736845d9f2fcb837ea5d9e2628564b3b043a70948a3f0b778838c5fb4f" + +[[package]] +name = "syn" +version = "2.0.114" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d4d107df263a3013ef9b1879b0df87d706ff80f65a86ea879bd9c31f9b307c2a" dependencies = [ "proc-macro2", "quote", @@ -225,26 +350,33 @@ dependencies = [ [[package]] name = "unicase" -version = "2.8.1" +version = "2.9.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "75b844d17643ee918803943289730bec8aac480150456169e647ed0b576ba539" +checksum = "dbc4bc3a9f746d862c45cb89d705aa10f187bb96c76001afab07a0d35ce60142" [[package]] name = "unicode-ident" -version = "1.0.18" +version = "1.0.22" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5a5f39404a5da50712a4c1eecf25e90dd62b613502b7e925fd4e4d19b5c96512" +checksum = "9312f7c4f6ff9069b165498234ce8be658059c6728633667c526e27dc2cf1df5" [[package]] name = "unicode-width" -version = "0.2.1" +version = "0.2.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4a1a07cc7db3810833284e8d372ccdc6da29741639ecc70c9ec107df0fa6154c" +checksum = "b4ac048d71ede7ee76d585517add45da530660ef4390e49b098733c6e897f254" + +[[package]] +name = "utf8parse" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "06abde3611657adf66d383f00b093d7faecc7fa57071cce2578660c9f1010821" [[package]] name = "webTemplate" version = "0.4.3" dependencies = [ + "clap", "hashlink", "minijinja", "pulldown-cmark", @@ -253,12 +385,33 @@ dependencies = [ ] [[package]] -name = "yaml-rust2" -version = "0.10.3" +name = "windows-link" +version = "0.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4ce2a4ff45552406d02501cea6c18d8a7e50228e7736a872951fe2fe75c91be7" +checksum = "f0805222e57f7521d6a62e36fa9163bc891acd422f971defe97d64e70d0a4fe5" + +[[package]] +name = "windows-sys" +version = "0.61.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ae137229bcbd6cdf0f7b80a31df61766145077ddf49416a728b02cb3921ff3fc" +dependencies = [ + "windows-link", +] + +[[package]] +name = "yaml-rust2" +version = "0.10.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2462ea039c445496d8793d052e13787f2b90e750b833afee748e601c17621ed9" dependencies = [ "arraydeque", "encoding_rs", "hashlink", ] + +[[package]] +name = "zmij" +version = "1.0.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "02aae0f83f69aafc94776e879363e9771d7ecbffe2c7fbb6c14c5e00dfe88439" diff --git a/Cargo.toml b/Cargo.toml index 760c097..7eaafc3 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -4,6 +4,7 @@ version = "0.4.3" edition = "2024" [dependencies] +clap = { version = "4.5.55", features = ["derive"] } hashlink = "0.10.0" minijinja = { version = "2.10.2", features = ["loader", "builtins", "json"] } pulldown-cmark = { version = "0.13.0", features = ["serde"] } diff --git a/src/main.rs b/src/main.rs index a14496a..6643966 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,4 +1,5 @@ -use std::{path::Path, fs, env}; +use std::{path::Path, fs}; +use clap::Parser; use minijinja::Environment; mod render; @@ -7,9 +8,6 @@ use render::{Renderer, IndexItem, build_jinja_env, Template}; mod version; use version::VERSION; -const SRC_PATH: &'static str = "./src/"; -const OUT_PATH: &'static str = "./html/"; - fn render_index( out_path: &Path, index: &IndexItem, @@ -23,7 +21,7 @@ fn render_index( let templates = match templates { Some(template) => template, - None => Template::index(&site_index.path.join("templates")), + None => Template::index(&site_index.path.join("templates")).unwrap(), }; let jinja_env = match jinja_env { @@ -69,12 +67,8 @@ fn render_index( return; } println!("INFO: render {}", dest_path_str.unwrap()); - match site_index.render_page(index, &site_index.site, jinja_env) { - Some(content) => { - let _ = fs::write(dest_path, content); - }, - None => todo!(), - }; + let content = site_index.render_page(index, &site_index.site, jinja_env).unwrap(); + fs::write(dest_path, content).unwrap(); } else { // file is an asset, no rendering done for assets @@ -100,82 +94,38 @@ fn render_index( } } -fn version() { - println!("version: {}", VERSION); -} +/// render static website using markdown and jinja templates +#[derive(Parser, Debug)] +#[command(about, long_about = None)] +struct Args { + /// path output directory + #[arg(short, long, default_value = "./public_html")] + output: String, -fn usage() { - println!("webtemplate [--output=] [--src=]"); + /// path source directory + #[arg(short, long, default_value = "./src")] + source: String, + + /// print version + #[arg(short, long, default_value_t = false)] + version: bool } fn main() { - - let mut src_path_str: String = SRC_PATH.to_string(); - let mut out_path_str: String = OUT_PATH.to_string(); - - for arg in env::args() { - if arg.starts_with("--") { - let mut parts = arg.split('='); - let option: String = match parts.next() { - None => { - println!("WARN: argument parsing: faild to split argument"); - continue; - }, - Some(str) => { - let mut str = String::from(str); - str.remove(0); - str.remove(0); - str - } - }; - let value = match parts.next() { - None => None, - Some(str) => Some(String::from(str)) - }; - - match option.as_str() { - "output" => { - if let Some(value) = value { - out_path_str = value; - } else { - println!("ERROR: no value given for output"); - usage(); - return; - } - } - "src" => { - if let Some(value) = value { - src_path_str = value; - } else { - println!("ERROR: no value given for output"); - usage(); - return; - } - } - "help" => { - usage(); - } - "version" => { - version(); - return; - } - _ => { - println!("ERROR: invalid option {}", option); - usage(); - return; - } - } - } + let args = Args::parse(); + if args.version { + println!("version: {}", VERSION); + return; } - let src_path = Path::new(&src_path_str); - let out_path = Path::new(&out_path_str); + let src_path = Path::new(&args.source); + let out_path = Path::new(&args.output); // scan all pages, template, components, etc. - let index = match Renderer::index(src_path) { + let index = match Renderer::index(src_path).unwrap() { Some(index) => index, None => { - println!("ERROR: src directory not found ({})", src_path_str); + println!("ERROR: src directory not found ({})", src_path.to_str().or(Some("Utf parse error")).unwrap()); return; }, }; diff --git a/src/render.rs b/src/render.rs index b3725ca..fb2fb65 100644 --- a/src/render.rs +++ b/src/render.rs @@ -1,14 +1,21 @@ mod index; mod parse_md; -use std::{collections::BTreeMap, fs, path::Path}; -use minijinja::{context, Environment, Value}; +use std::{collections::BTreeMap, fs, path::Path, io::Error as IoError}; +use minijinja::{context, Environment, Value, Error as JinjaError}; use index::indexer; use parse_md::parse_md; use yaml_rust2::Yaml; pub use index::indexer::{IndexItem, Template}; +#[derive(Debug)] +pub enum Error { + NoSrc, + Io(IoError), + Jinja(JinjaError) +} + pub fn build_jinja_env<'a>(templates: Vec>) -> Environment<'a> { build_jinja_env_dir(templates) } @@ -46,15 +53,15 @@ pub struct Renderer { pub path: Box } impl Renderer { - pub fn index(path: &Path) -> Option { - if let Some(site_index) = IndexItem::index(&path) { - Some(Renderer { + pub fn index(path: &Path) -> Result, indexer::Error> { + if let Some(site_index) = IndexItem::index(&path)? { + Ok(Some(Renderer { site: site_index, path: Box::from(path) - }) + })) } else { - None + Ok(None) } } @@ -89,42 +96,26 @@ impl Renderer { } } - pub fn render_page(&self, page: &IndexItem, index: &IndexItem, jinja_env: &Environment) -> Option { + pub fn render_page(&self, page: &IndexItem, index: &IndexItem, jinja_env: &Environment) -> Result { if page.src == None { - return None; + return Err(Error::NoSrc); } - match fs::read_to_string(page.src.as_ref().unwrap()) { - Ok(content) => { - let split = indexer::split_params(content); - match parse_md(split.md, jinja_env) { - Some(md) => { - let template = match jinja_env.get_template(&page.template) { - Ok(templ) => templ, - Err(_) => { - println!("ERROR: jinja: failed to get template"); - return None; - }, - }; - let html = match template.render(context! { - index => index.to_minijinja(), - is_error_not_found => false, - url => page.get_url(), - body => md, - params => Renderer::convert_yaml(split.yaml) - }) { - Ok(out) => out, - Err(_) => { - println!("ERROR: jinja: failed to render template"); - return None; - }, - }; + let content = fs::read_to_string(page.src.as_ref().unwrap()).or_else(|e| Err(Error::Io(e)))?; + let split = indexer::split_params(content); + match parse_md(split.md, jinja_env) { + Some(md) => { + let template = jinja_env.get_template(&page.template).or_else(|e| Err(Error::Jinja(e)))?; + let html = template.render(context! { + index => index.to_minijinja(), + is_error_not_found => false, + url => page.get_url(), + body => md, + params => Renderer::convert_yaml(split.yaml) + }).or_else(|e| Err(Error::Jinja(e)))?; - Some(html) - }, - None => None - } + Ok(html) }, - Err(_) => None, + None => todo!() } } } diff --git a/src/render/index.rs b/src/render/index.rs index 00eaa93..22b2a50 100644 --- a/src/render/index.rs +++ b/src/render/index.rs @@ -1,10 +1,35 @@ pub mod indexer { - use std::{collections::BTreeMap, ffi::OsString, fs, path::Path}; + use core::fmt; + use std::{collections::BTreeMap, ffi::OsString, fs, path::Path, io::Error as IoError}; use hashlink::LinkedHashMap; use regex::Regex; use yaml_rust2::{Yaml, YamlLoader}; + pub enum Error { + ReadDir(IoError), + ReadMd(IoError), + UtfError(OsString), + InvalidPathComponent, + } + impl fmt::Debug for Error { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + match self { + Self::ReadDir(arg0) => f.debug_tuple("ReadDir").field(arg0).finish(), + Self::ReadMd(arg0) => f.debug_tuple("ReadMd").field(arg0).finish(), + Self::UtfError(arg0) => { + let hex_code: Vec = arg0.clone().into_encoded_bytes().iter().map(|byte| { + let nibels = [((byte&0xF)+0x30), (((byte>>4)&0xF)+0x30)]; + str::from_utf8(&nibels).unwrap().to_owned() + }).collect(); + write!(f, "UtfError 0x{}", hex_code.join("")) + // f.debug_tuple("UtfError").field(arg0).finish() + }, + Self::InvalidPathComponent => write!(f, "InvalidPathComponent"), + } + } + } + #[derive(Debug)] pub struct IndexItem { // path to source file. If none, it is a directory without an index page @@ -41,111 +66,90 @@ pub mod indexer { } } - pub fn index(path: &Path) -> Option { + pub fn index(path: &Path) -> Result, Error> { IndexItem::scan_entry(path, String::from("/")) } - fn scan_entry(path: &Path, last_url: String) -> Option { + fn scan_entry(path: &Path, last_url: String) -> Result, Error> { let mut item = IndexItem::new(); - if let Some(file_name) = path.file_name() { - if let Some(file_name) = file_name.to_str() { - let file_name = String::from(file_name); - if path.is_dir() { - if file_name != "templates" { - item.friendly = file_name.clone(); - item.path = last_url.clone(); - let url = if last_url == String::from("/") { - format!("/{file_name}") - } else { - format!("{}/{file_name}", last_url.clone()) - }; - let index_path = path.join("index.md"); - match IndexItem::scan_entry(&index_path, last_url.clone()) { - Some(mut item) => { - let sub_pages = IndexItem::scan_dir(&path, last_url); - item.sub_pages = sub_pages.0; - item.public = sub_pages.1; - return Some(item); - }, - None => { - let sub_pages = IndexItem::scan_dir(&path, url); - item.sub_pages = sub_pages.0; - item.public = sub_pages.1; - return Some(item); - } - } + let file_name = path.file_name().ok_or(Error::InvalidPathComponent)?; + let file_name = file_name.to_str().ok_or(Error::UtfError(file_name.to_owned()))?; + if path.is_dir() { + if file_name != "templates" { + item.friendly = file_name.to_string(); + item.path = last_url.clone(); + let url = if last_url.ends_with("/") { + format!("{last_url}{file_name}") + } else { + format!("{last_url}/{file_name}") + }; + let index_path = path.join("index.md"); + match IndexItem::scan_entry(&index_path, last_url.clone())? { + Some(mut index_item) => { + let sub_pages = IndexItem::scan_dir(&path, last_url)?; + index_item.sub_pages = sub_pages.0; + index_item.public = sub_pages.1; + return Ok(Some(index_item)); + }, + None => { + let sub_pages = IndexItem::scan_dir(&path, url)?; + item.sub_pages = sub_pages.0; + item.public = sub_pages.1; + return Ok(Some(item)); } } - else if path.is_file() { - item.src = match path.to_str() { - Some(path) => Some(String::from(path)), - None => None, - }; - if item.src == None || file_name.len() < 4 { - return None; - } - let extention: String = file_name.clone().drain(file_name.len()-3..).collect(); - if extention == ".md" { - let friendly = file_name.clone().drain(..file_name.len()-3).collect(); - item.friendly = if friendly == "index" { - String::from("") - } else { - friendly - }; - item.path = last_url; - let md = fs::read_to_string(path); - match md { - Ok(md) => { - let params = split_params(md).yaml; - IndexItem::parse_params(&mut item, params); - return Some(item) - }, - Err(_) => todo!() - } - } - else { - item.is_asset = true; - item.friendly = file_name.clone(); - item.path = last_url; - return Some(item); - } + } + } + else if path.is_file() { + item.src = match path.to_str() { + Some(path) => Some(String::from(path)), + None => None, + }; + if item.src == None || file_name.len() < 4 { + return Ok(None); + } + if file_name.ends_with(".md") { + item.friendly = file_name.to_string().drain(..file_name.len()-3).collect(); + if item.friendly == "index" { + item.friendly = String::from(""); } - return None + item.path = last_url; + let md = fs::read_to_string(path).or_else(|e| Err(Error::ReadMd(e)))?; + let params = split_params(md).yaml; + IndexItem::parse_params(&mut item, params); + return Ok(Some(item)) + } + else { + item.is_asset = true; + item.friendly = file_name.to_string(); + item.path = last_url; + return Ok(Some(item)); } } - None + Ok(None) } - fn scan_dir(path: &Path, last_url: String) -> (Vec>, bool) { + fn scan_dir(path: &Path, last_url: String) -> Result<(Vec>, bool), Error> { let mut items: Vec> = Vec::new(); let mut has_public: bool = false; - - let list = fs::read_dir(path); - match list { - Ok(list) => { - for entry in list { - match entry { - Ok(entry) => { - if entry.file_name() != OsString::from("index.md") { - let item = IndexItem::scan_entry(&entry.path(), last_url.clone()); - if let Some(i) = item { - if i.public { - has_public = true; - } - items.push(Box::from(i)); - } - } - }, - Err(_) => todo!() + + let list = fs::read_dir(path).or_else(|e| Err(Error::ReadDir(e)))?; + for entry in list { + let entry = entry.or_else(|e| Err(Error::ReadDir(e)))?; + if entry.file_name() != OsString::from("index.md") { + let item = IndexItem::scan_entry(&entry.path(), last_url.clone())?; + if let Some(i) = item { + if i.public { + has_public = true; } + items.push(Box::from(i)); } - }, - Err(_) => todo!() - }; + } + } items.sort_by(|i, c| i.friendly.cmp(&c.friendly)); - (items, has_public) + Ok((items, has_public)) } fn parse_params(item: &mut IndexItem, params: Yaml) { @@ -217,7 +221,7 @@ pub mod indexer { pub sub_templates: Vec> } impl Template { - fn new() -> Template { + const fn new() -> Template { Template { name: String::new(), extention: String::new(), @@ -254,31 +258,28 @@ pub mod indexer { None } - pub fn index(path: &Path) -> Vec> { - match Self::scan_template(path) { - Some(templates) => templates.sub_templates, - None => Vec::new(), + pub fn index(path: &Path) -> Result>, Error> { + match Self::scan_template(path)? { + Some(templates) => Ok(templates.sub_templates), + None => Ok(Vec::new()), } } - fn scan_template(path: &Path) -> Option { + fn scan_template(path: &Path) -> Result, Error> { let mut item = Template::new(); if let Some(file_name) = path.file_name() { if let Some(file_name) = file_name.to_str() { let file_name = String::from(file_name); if path.is_dir() { item.name = file_name; - item.sub_templates = Template::scan_dir(&path); - return Some(item); + item.sub_templates = Template::scan_dir(&path)?; + return Ok(Some(item)); } else if path.is_file() { item.path = match path.to_str() { Some(path) => Some(String::from(path)), - None => None, + None => return Ok(None), }; - if item.path == None { - return None; - } let path = item.path.clone().unwrap(); item.src = match fs::read_to_string(path) { @@ -292,43 +293,34 @@ pub mod indexer { let dot = dot.get(0).unwrap(); item.extention = file_name.clone().drain(dot.end()..).collect(); item.name = file_name.clone().drain(..dot.start()).collect(); - return Some(item); + return Ok(Some(item)); } else { item.name = file_name; - return Some(item); + return Ok(Some(item)); } } - return None + return Ok(None) } } - None + Ok(None) } - fn scan_dir(path: &Path) -> Vec> { + fn scan_dir(path: &Path) -> Result>, Error> { let mut items: Vec> = Vec::new(); - let list = fs::read_dir(path); - match list { - Ok(list) => { - for entry in list { - match entry { - Ok(entry) => { - let item = Template::scan_template(&entry.path()); - if let Some(i) = item { - items.push(Box::from(i)); - } - }, - Err(_) => todo!() - } - } - }, - Err(_) => todo!() - }; + let list = fs::read_dir(path).or(Err(Error::InvalidPathComponent))?; + for entry in list { + let entry = entry.or_else(|e| Err(Error::ReadDir(e)))?; + let item: Option