cleanup just a littel bit of code

This commit is contained in:
2026-01-30 10:41:09 +01:00
parent 266850dccf
commit 5e8d2e04e0
5 changed files with 379 additions and 292 deletions

259
Cargo.lock generated
View File

@@ -4,13 +4,63 @@ version = 4
[[package]] [[package]]
name = "aho-corasick" name = "aho-corasick"
version = "1.1.3" version = "1.1.4"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8e60d3430d3a69478ad0993f19238d2df97c507009a52b3c10addcd7f6bcb916" checksum = "ddd31a130427c27518df266943a5308ed92d4b226cc639f5a8f1002816174301"
dependencies = [ dependencies = [
"memchr", "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]] [[package]]
name = "arraydeque" name = "arraydeque"
version = "0.5.1" version = "0.5.1"
@@ -19,15 +69,61 @@ checksum = "7d902e3d592a523def97af8f317b08ce16b7ab854c1985a0c671e6f15cebc236"
[[package]] [[package]]
name = "bitflags" name = "bitflags"
version = "2.9.3" version = "2.10.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "34efbcccd345379ca2868b2b2c9d3782e9cc58ba87bc7d79d5b53d9c9ae6f25d" checksum = "812e12b5285cc515a9c72a5c1d3b6d46a19dac5acfef5265968c166106e31dd3"
[[package]] [[package]]
name = "cfg-if" name = "cfg-if"
version = "1.0.3" version = "1.0.4"
source = "registry+https://github.com/rust-lang/crates.io-index" 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]] [[package]]
name = "encoding_rs" name = "encoding_rs"
@@ -46,9 +142,9 @@ checksum = "d9c4f5dac5e15c24eb999c26181a6ca40b39fe946cbe4c263c7209467bc83af2"
[[package]] [[package]]
name = "getopts" name = "getopts"
version = "0.2.23" version = "0.2.24"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cba6ae63eb948698e300f645f87c70f76630d505f23b8907cf1e193ee85048c1" checksum = "cfe4fbac503b8d1f88e6676011885f34b7174f46e59956bba534ba83abded4df"
dependencies = [ dependencies = [
"unicode-width", "unicode-width",
] ]
@@ -72,16 +168,28 @@ dependencies = [
] ]
[[package]] [[package]]
name = "itoa" name = "heck"
version = "1.0.15" version = "0.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index" 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]] [[package]]
name = "memchr" name = "memchr"
version = "2.7.5" version = "2.7.6"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "32a282da65faaf38286cf3be983213fcf1d2e2a58700e808f83f4ea9a4804bc0" checksum = "f52b00d39961fc5b2736ea853c9cc86238e165017a493d1d5c8eac6bdc4cc273"
[[package]] [[package]]
name = "memo-map" name = "memo-map"
@@ -91,9 +199,9 @@ checksum = "38d1115007560874e373613744c6fba374c17688327a71c1476d1a5954cc857b"
[[package]] [[package]]
name = "minijinja" name = "minijinja"
version = "2.12.0" version = "2.15.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a9f264d75233323f4b7d2f03aefe8a990690cdebfbfe26ea86bcbaec5e9ac990" checksum = "b479616bb6f0779fb0f3964246beda02d4b01144e1b0d5519616e012ccc2a245"
dependencies = [ dependencies = [
"memo-map", "memo-map",
"self_cell", "self_cell",
@@ -102,10 +210,16 @@ dependencies = [
] ]
[[package]] [[package]]
name = "proc-macro2" name = "once_cell_polyfill"
version = "1.0.101" version = "1.70.2"
source = "registry+https://github.com/rust-lang/crates.io-index" 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 = [ dependencies = [
"unicode-ident", "unicode-ident",
] ]
@@ -132,18 +246,18 @@ checksum = "007d8adb5ddab6f8e3f491ac63566a7d5002cc7ed73901f72057943fa71ae1ae"
[[package]] [[package]]
name = "quote" name = "quote"
version = "1.0.40" version = "1.0.44"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1885c039570dc00dcb4ff087a89e185fd56bae234ddc7f056a945bf36467248d" checksum = "21b2ebcf727b7760c461f091f9f0f539b77b8e87f2fd88131e7f1b433b3cece4"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
] ]
[[package]] [[package]]
name = "regex" name = "regex"
version = "1.11.2" version = "1.12.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "23d7fd106d8c02486a8d64e778353d1cffe08ce79ac2e82f540c86d0facf6912" checksum = "843bc0191f75f3e22651ae5f1e72939ab2f72a4bc30fa80a066bd66edefc24d4"
dependencies = [ dependencies = [
"aho-corasick", "aho-corasick",
"memchr", "memchr",
@@ -153,9 +267,9 @@ dependencies = [
[[package]] [[package]]
name = "regex-automata" name = "regex-automata"
version = "0.4.10" version = "0.4.13"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6b9458fa0bfeeac22b5ca447c63aaf45f28439a709ccd244698632f9aa6394d6" checksum = "5276caf25ac86c8d810222b3dbb938e512c55c6831a10f3e6ed1c93b84041f1c"
dependencies = [ dependencies = [
"aho-corasick", "aho-corasick",
"memchr", "memchr",
@@ -164,36 +278,40 @@ dependencies = [
[[package]] [[package]]
name = "regex-syntax" name = "regex-syntax"
version = "0.8.6" version = "0.8.8"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "caf4aa5b0f434c91fe5c7f1ecb6a5ece2130b02ad2a590589dda5146df959001" checksum = "7a2d987857b319362043e95f5353c0535c1f58eec5336fdfcf626430af7def58"
[[package]]
name = "ryu"
version = "1.0.20"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "28d3b2b1366ec20994f1fd18c3c594f05c5dd4bc44d8bb0c1c632c8d6829481f"
[[package]] [[package]]
name = "self_cell" name = "self_cell"
version = "1.2.0" version = "1.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0f7d95a54511e0c7be3f51e8867aa8cf35148d7b9445d44de2f943e2b206e749" checksum = "b12e76d157a900eb52e81bc6e9f3069344290341720e9178cde2407113ac8d89"
[[package]] [[package]]
name = "serde" name = "serde"
version = "1.0.219" version = "1.0.228"
source = "registry+https://github.com/rust-lang/crates.io-index" 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 = [ dependencies = [
"serde_derive", "serde_derive",
] ]
[[package]] [[package]]
name = "serde_derive" name = "serde_derive"
version = "1.0.219" version = "1.0.228"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5b0276cf7f2c73365f7157c8123c21cd9a50fbbd844757af28ca1f5925fc2a00" checksum = "d540f220d3187173da220f885ab66608367b6574e925011a9353e4badda91d79"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
"quote", "quote",
@@ -202,21 +320,28 @@ dependencies = [
[[package]] [[package]]
name = "serde_json" name = "serde_json"
version = "1.0.143" version = "1.0.149"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d401abef1d108fbd9cbaebc3e46611f4b1021f714a0597a71f41ee463f5f4a5a" checksum = "83fc039473c5595ace860d8c4fafa220ff474b3fc6bfdb4293327f1a37e94d86"
dependencies = [ dependencies = [
"itoa", "itoa",
"memchr", "memchr",
"ryu",
"serde", "serde",
"serde_core",
"zmij",
] ]
[[package]] [[package]]
name = "syn" name = "strsim"
version = "2.0.106" version = "0.11.1"
source = "registry+https://github.com/rust-lang/crates.io-index" 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 = [ dependencies = [
"proc-macro2", "proc-macro2",
"quote", "quote",
@@ -225,26 +350,33 @@ dependencies = [
[[package]] [[package]]
name = "unicase" name = "unicase"
version = "2.8.1" version = "2.9.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "75b844d17643ee918803943289730bec8aac480150456169e647ed0b576ba539" checksum = "dbc4bc3a9f746d862c45cb89d705aa10f187bb96c76001afab07a0d35ce60142"
[[package]] [[package]]
name = "unicode-ident" name = "unicode-ident"
version = "1.0.18" version = "1.0.22"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5a5f39404a5da50712a4c1eecf25e90dd62b613502b7e925fd4e4d19b5c96512" checksum = "9312f7c4f6ff9069b165498234ce8be658059c6728633667c526e27dc2cf1df5"
[[package]] [[package]]
name = "unicode-width" name = "unicode-width"
version = "0.2.1" version = "0.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index" 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]] [[package]]
name = "webTemplate" name = "webTemplate"
version = "0.4.3" version = "0.4.3"
dependencies = [ dependencies = [
"clap",
"hashlink", "hashlink",
"minijinja", "minijinja",
"pulldown-cmark", "pulldown-cmark",
@@ -253,12 +385,33 @@ dependencies = [
] ]
[[package]] [[package]]
name = "yaml-rust2" name = "windows-link"
version = "0.10.3" version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index" 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 = [ dependencies = [
"arraydeque", "arraydeque",
"encoding_rs", "encoding_rs",
"hashlink", "hashlink",
] ]
[[package]]
name = "zmij"
version = "1.0.17"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "02aae0f83f69aafc94776e879363e9771d7ecbffe2c7fbb6c14c5e00dfe88439"

View File

@@ -4,6 +4,7 @@ version = "0.4.3"
edition = "2024" edition = "2024"
[dependencies] [dependencies]
clap = { version = "4.5.55", features = ["derive"] }
hashlink = "0.10.0" hashlink = "0.10.0"
minijinja = { version = "2.10.2", features = ["loader", "builtins", "json"] } minijinja = { version = "2.10.2", features = ["loader", "builtins", "json"] }
pulldown-cmark = { version = "0.13.0", features = ["serde"] } pulldown-cmark = { version = "0.13.0", features = ["serde"] }

View File

@@ -1,4 +1,5 @@
use std::{path::Path, fs, env}; use std::{path::Path, fs};
use clap::Parser;
use minijinja::Environment; use minijinja::Environment;
mod render; mod render;
@@ -7,9 +8,6 @@ use render::{Renderer, IndexItem, build_jinja_env, Template};
mod version; mod version;
use version::VERSION; use version::VERSION;
const SRC_PATH: &'static str = "./src/";
const OUT_PATH: &'static str = "./html/";
fn render_index( fn render_index(
out_path: &Path, out_path: &Path,
index: &IndexItem, index: &IndexItem,
@@ -23,7 +21,7 @@ fn render_index(
let templates = match templates { let templates = match templates {
Some(template) => template, 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 { let jinja_env = match jinja_env {
@@ -69,12 +67,8 @@ fn render_index(
return; return;
} }
println!("INFO: render {}", dest_path_str.unwrap()); println!("INFO: render {}", dest_path_str.unwrap());
match site_index.render_page(index, &site_index.site, jinja_env) { let content = site_index.render_page(index, &site_index.site, jinja_env).unwrap();
Some(content) => { fs::write(dest_path, content).unwrap();
let _ = fs::write(dest_path, content);
},
None => todo!(),
};
} }
else { else {
// file is an asset, no rendering done for assets // file is an asset, no rendering done for assets
@@ -100,82 +94,38 @@ fn render_index(
} }
} }
fn version() { /// render static website using markdown and jinja templates
println!("version: {}", VERSION); #[derive(Parser, Debug)]
} #[command(about, long_about = None)]
struct Args {
/// path output directory
#[arg(short, long, default_value = "./public_html")]
output: String,
fn usage() { /// path source directory
println!("webtemplate [--output=<dir>] [--src=<dir>]"); #[arg(short, long, default_value = "./src")]
source: String,
/// print version
#[arg(short, long, default_value_t = false)]
version: bool
} }
fn main() { fn main() {
let args = Args::parse();
let mut src_path_str: String = SRC_PATH.to_string(); if args.version {
let mut out_path_str: String = OUT_PATH.to_string(); println!("version: {}", VERSION);
return;
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 src_path = Path::new(&src_path_str); let src_path = Path::new(&args.source);
let out_path = Path::new(&out_path_str); let out_path = Path::new(&args.output);
// scan all pages, template, components, etc. // scan all pages, template, components, etc.
let index = match Renderer::index(src_path) { let index = match Renderer::index(src_path).unwrap() {
Some(index) => index, Some(index) => index,
None => { 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; return;
}, },
}; };

View File

@@ -1,14 +1,21 @@
mod index; mod index;
mod parse_md; mod parse_md;
use std::{collections::BTreeMap, fs, path::Path}; use std::{collections::BTreeMap, fs, path::Path, io::Error as IoError};
use minijinja::{context, Environment, Value}; use minijinja::{context, Environment, Value, Error as JinjaError};
use index::indexer; use index::indexer;
use parse_md::parse_md; use parse_md::parse_md;
use yaml_rust2::Yaml; use yaml_rust2::Yaml;
pub use index::indexer::{IndexItem, Template}; pub use index::indexer::{IndexItem, Template};
#[derive(Debug)]
pub enum Error {
NoSrc,
Io(IoError),
Jinja(JinjaError)
}
pub fn build_jinja_env<'a>(templates: Vec<Box<indexer::Template>>) -> Environment<'a> { pub fn build_jinja_env<'a>(templates: Vec<Box<indexer::Template>>) -> Environment<'a> {
build_jinja_env_dir(templates) build_jinja_env_dir(templates)
} }
@@ -46,15 +53,15 @@ pub struct Renderer {
pub path: Box<Path> pub path: Box<Path>
} }
impl Renderer { impl Renderer {
pub fn index(path: &Path) -> Option<Renderer> { pub fn index(path: &Path) -> Result<Option<Renderer>, indexer::Error> {
if let Some(site_index) = IndexItem::index(&path) { if let Some(site_index) = IndexItem::index(&path)? {
Some(Renderer { Ok(Some(Renderer {
site: site_index, site: site_index,
path: Box::from(path) path: Box::from(path)
}) }))
} }
else { else {
None Ok(None)
} }
} }
@@ -89,42 +96,26 @@ impl Renderer {
} }
} }
pub fn render_page(&self, page: &IndexItem, index: &IndexItem, jinja_env: &Environment) -> Option<String> { pub fn render_page(&self, page: &IndexItem, index: &IndexItem, jinja_env: &Environment) -> Result<String, Error> {
if page.src == None { if page.src == None {
return None; return Err(Error::NoSrc);
} }
match fs::read_to_string(page.src.as_ref().unwrap()) { let content = fs::read_to_string(page.src.as_ref().unwrap()).or_else(|e| Err(Error::Io(e)))?;
Ok(content) => { let split = indexer::split_params(content);
let split = indexer::split_params(content); match parse_md(split.md, jinja_env) {
match parse_md(split.md, jinja_env) { Some(md) => {
Some(md) => { let template = jinja_env.get_template(&page.template).or_else(|e| Err(Error::Jinja(e)))?;
let template = match jinja_env.get_template(&page.template) { let html = template.render(context! {
Ok(templ) => templ, index => index.to_minijinja(),
Err(_) => { is_error_not_found => false,
println!("ERROR: jinja: failed to get template"); url => page.get_url(),
return None; body => md,
}, params => Renderer::convert_yaml(split.yaml)
}; }).or_else(|e| Err(Error::Jinja(e)))?;
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;
},
};
Some(html) Ok(html)
},
None => None
}
}, },
Err(_) => None, None => todo!()
} }
} }
} }

View File

@@ -1,10 +1,35 @@
pub mod indexer { 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 hashlink::LinkedHashMap;
use regex::Regex; use regex::Regex;
use yaml_rust2::{Yaml, YamlLoader}; 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<String> = 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)] #[derive(Debug)]
pub struct IndexItem { pub struct IndexItem {
// path to source file. If none, it is a directory without an index page // 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<IndexItem> { pub fn index(path: &Path) -> Result<Option<IndexItem>, Error> {
IndexItem::scan_entry(path, String::from("/")) IndexItem::scan_entry(path, String::from("/"))
} }
fn scan_entry(path: &Path, last_url: String) -> Option<IndexItem> { fn scan_entry(path: &Path, last_url: String) -> Result<Option<IndexItem>, Error> {
let mut item = IndexItem::new(); let mut item = IndexItem::new();
if let Some(file_name) = path.file_name() { let file_name = path.file_name().ok_or(Error::InvalidPathComponent)?;
if let Some(file_name) = file_name.to_str() { let file_name = file_name.to_str().ok_or(Error::UtfError(file_name.to_owned()))?;
let file_name = String::from(file_name); if path.is_dir() {
if path.is_dir() { if file_name != "templates" {
if file_name != "templates" { item.friendly = file_name.to_string();
item.friendly = file_name.clone(); item.path = last_url.clone();
item.path = last_url.clone(); let url = if last_url.ends_with("/") {
let url = if last_url == String::from("/") { format!("{last_url}{file_name}")
format!("/{file_name}") } else {
} else { format!("{last_url}/{file_name}")
format!("{}/{file_name}", last_url.clone()) };
}; let index_path = path.join("index.md");
let index_path = path.join("index.md"); match IndexItem::scan_entry(&index_path, last_url.clone())? {
match IndexItem::scan_entry(&index_path, last_url.clone()) { Some(mut index_item) => {
Some(mut item) => { let sub_pages = IndexItem::scan_dir(&path, last_url)?;
let sub_pages = IndexItem::scan_dir(&path, last_url); index_item.sub_pages = sub_pages.0;
item.sub_pages = sub_pages.0; index_item.public = sub_pages.1;
item.public = sub_pages.1; return Ok(Some(index_item));
return Some(item); },
}, None => {
None => { let sub_pages = IndexItem::scan_dir(&path, url)?;
let sub_pages = IndexItem::scan_dir(&path, url); item.sub_pages = sub_pages.0;
item.sub_pages = sub_pages.0; item.public = sub_pages.1;
item.public = sub_pages.1; return Ok(Some(item));
return Some(item);
}
}
} }
} }
else if path.is_file() { }
item.src = match path.to_str() { }
Some(path) => Some(String::from(path)), else if path.is_file() {
None => None, item.src = match path.to_str() {
}; Some(path) => Some(String::from(path)),
if item.src == None || file_name.len() < 4 { None => None,
return None; };
} if item.src == None || file_name.len() < 4 {
let extention: String = file_name.clone().drain(file_name.len()-3..).collect(); return Ok(None);
if extention == ".md" { }
let friendly = file_name.clone().drain(..file_name.len()-3).collect(); if file_name.ends_with(".md") {
item.friendly = if friendly == "index" { item.friendly = file_name.to_string().drain(..file_name.len()-3).collect();
String::from("") if item.friendly == "index" {
} else { item.friendly = String::from("");
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);
}
} }
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<Box<IndexItem>>, bool) { fn scan_dir(path: &Path, last_url: String) -> Result<(Vec<Box<IndexItem>>, bool), Error> {
let mut items: Vec<Box<IndexItem>> = Vec::new(); let mut items: Vec<Box<IndexItem>> = Vec::new();
let mut has_public: bool = false; let mut has_public: bool = false;
let list = fs::read_dir(path); let list = fs::read_dir(path).or_else(|e| Err(Error::ReadDir(e)))?;
match list { for entry in list {
Ok(list) => { let entry = entry.or_else(|e| Err(Error::ReadDir(e)))?;
for entry in list { if entry.file_name() != OsString::from("index.md") {
match entry { let item = IndexItem::scan_entry(&entry.path(), last_url.clone())?;
Ok(entry) => { if let Some(i) = item {
if entry.file_name() != OsString::from("index.md") { if i.public {
let item = IndexItem::scan_entry(&entry.path(), last_url.clone()); has_public = true;
if let Some(i) = item {
if i.public {
has_public = true;
}
items.push(Box::from(i));
}
}
},
Err(_) => todo!()
} }
items.push(Box::from(i));
} }
}, }
Err(_) => todo!() }
};
items.sort_by(|i, c| i.friendly.cmp(&c.friendly)); 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) { fn parse_params(item: &mut IndexItem, params: Yaml) {
@@ -217,7 +221,7 @@ pub mod indexer {
pub sub_templates: Vec<Box<Template>> pub sub_templates: Vec<Box<Template>>
} }
impl Template { impl Template {
fn new() -> Template { const fn new() -> Template {
Template { Template {
name: String::new(), name: String::new(),
extention: String::new(), extention: String::new(),
@@ -254,31 +258,28 @@ pub mod indexer {
None None
} }
pub fn index(path: &Path) -> Vec<Box<Self>> { pub fn index(path: &Path) -> Result<Vec<Box<Self>>, Error> {
match Self::scan_template(path) { match Self::scan_template(path)? {
Some(templates) => templates.sub_templates, Some(templates) => Ok(templates.sub_templates),
None => Vec::new(), None => Ok(Vec::new()),
} }
} }
fn scan_template(path: &Path) -> Option<Self> { fn scan_template(path: &Path) -> Result<Option<Self>, Error> {
let mut item = Template::new(); let mut item = Template::new();
if let Some(file_name) = path.file_name() { if let Some(file_name) = path.file_name() {
if let Some(file_name) = file_name.to_str() { if let Some(file_name) = file_name.to_str() {
let file_name = String::from(file_name); let file_name = String::from(file_name);
if path.is_dir() { if path.is_dir() {
item.name = file_name; item.name = file_name;
item.sub_templates = Template::scan_dir(&path); item.sub_templates = Template::scan_dir(&path)?;
return Some(item); return Ok(Some(item));
} }
else if path.is_file() { else if path.is_file() {
item.path = match path.to_str() { item.path = match path.to_str() {
Some(path) => Some(String::from(path)), Some(path) => Some(String::from(path)),
None => None, None => return Ok(None),
}; };
if item.path == None {
return None;
}
let path = item.path.clone().unwrap(); let path = item.path.clone().unwrap();
item.src = match fs::read_to_string(path) { item.src = match fs::read_to_string(path) {
@@ -292,43 +293,34 @@ pub mod indexer {
let dot = dot.get(0).unwrap(); let dot = dot.get(0).unwrap();
item.extention = file_name.clone().drain(dot.end()..).collect(); item.extention = file_name.clone().drain(dot.end()..).collect();
item.name = file_name.clone().drain(..dot.start()).collect(); item.name = file_name.clone().drain(..dot.start()).collect();
return Some(item); return Ok(Some(item));
} }
else { else {
item.name = file_name; 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<Box<Template>> { fn scan_dir(path: &Path) -> Result<Vec<Box<Template>>, Error> {
let mut items: Vec<Box<Template>> = Vec::new(); let mut items: Vec<Box<Template>> = Vec::new();
let list = fs::read_dir(path); let list = fs::read_dir(path).or(Err(Error::InvalidPathComponent))?;
match list { for entry in list {
Ok(list) => { let entry = entry.or_else(|e| Err(Error::ReadDir(e)))?;
for entry in list { let item: Option<Template> = Template::scan_template(&entry.path())?;
match entry { if let Some(i) = item {
Ok(entry) => { items.push(Box::from(i));
let item = Template::scan_template(&entry.path()); }
if let Some(i) = item { }
items.push(Box::from(i));
}
},
Err(_) => todo!()
}
}
},
Err(_) => todo!()
};
items.sort_by(|i, c| i.name.cmp(&c.name)); items.sort_by(|i, c| i.name.cmp(&c.name));
items Ok(items)
} }
} }