Compare commits
3 Commits
266850dccf
...
live
| Author | SHA1 | Date | |
|---|---|---|---|
|
c538b44f6b
|
|||
|
865387d021
|
|||
|
5e8d2e04e0
|
265
Cargo.lock
generated
265
Cargo.lock
generated
@@ -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",
|
||||
]
|
||||
@@ -113,8 +227,7 @@ dependencies = [
|
||||
[[package]]
|
||||
name = "pulldown-cmark"
|
||||
version = "0.13.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "1e8bbe1a966bd2f362681a44f6edce3c2310ac21e4d5067a6e7ec396297a6ea0"
|
||||
source = "git+https://git.gay/LailaTheElf/pulldown-cmark.git?branch=feature%2FeventTree#2300d0db6541279c41928b44d12eac510ecc3199"
|
||||
dependencies = [
|
||||
"bitflags",
|
||||
"getopts",
|
||||
@@ -127,23 +240,22 @@ dependencies = [
|
||||
[[package]]
|
||||
name = "pulldown-cmark-escape"
|
||||
version = "0.11.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "007d8adb5ddab6f8e3f491ac63566a7d5002cc7ed73901f72057943fa71ae1ae"
|
||||
source = "git+https://git.gay/LailaTheElf/pulldown-cmark.git?branch=feature%2FeventTree#2300d0db6541279c41928b44d12eac510ecc3199"
|
||||
|
||||
[[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 +265,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 +276,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 +318,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 +348,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 +383,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"
|
||||
|
||||
@@ -4,8 +4,13 @@ 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"] }
|
||||
regex = "1.11.1"
|
||||
yaml-rust2 = "0.10.2"
|
||||
|
||||
[dependencies.pulldown-cmark]
|
||||
git = "https://git.gay/LailaTheElf/pulldown-cmark.git"
|
||||
branch = "feature/eventTree"
|
||||
features = ["serde"]
|
||||
|
||||
237
src/main.rs
237
src/main.rs
@@ -1,186 +1,105 @@
|
||||
use std::{path::Path, fs, env};
|
||||
use minijinja::Environment;
|
||||
use std::{convert::Infallible, fs, path::Path};
|
||||
use clap::Parser;
|
||||
|
||||
mod render;
|
||||
use render::{Renderer, IndexItem, build_jinja_env, Template};
|
||||
use render::{Renderer, IndexItem};
|
||||
|
||||
mod version;
|
||||
use version::VERSION;
|
||||
|
||||
const SRC_PATH: &'static str = "./src/";
|
||||
const OUT_PATH: &'static str = "./html/";
|
||||
|
||||
fn render_index(
|
||||
fn render_page(
|
||||
page: &IndexItem,
|
||||
out_path: &Path,
|
||||
index: &IndexItem,
|
||||
index: &Renderer,
|
||||
cur_path: &Path,
|
||||
site_index: &Renderer,
|
||||
templates: Option<Vec<Box<Template>>>,
|
||||
jinja_env: Option<&Environment>
|
||||
) {
|
||||
let dest_path: &Path = &cur_path.join(&index.friendly);
|
||||
// println!("dest_path: {:?}", dest_path);
|
||||
|
||||
let templates = match templates {
|
||||
Some(template) => template,
|
||||
None => Template::index(&site_index.path.join("templates")),
|
||||
let friendly = match page.friendly.len() {
|
||||
0 => "index",
|
||||
_ => &page.friendly
|
||||
};
|
||||
|
||||
let jinja_env = match jinja_env {
|
||||
Some(env) => env,
|
||||
None => &build_jinja_env(templates.clone()),
|
||||
};
|
||||
|
||||
match &index.src {
|
||||
Some(src) => {
|
||||
// a source file is available. try to render it
|
||||
|
||||
let friendly = match index.friendly.len() {
|
||||
0 => String::from("index"),
|
||||
_ => index.friendly.clone()
|
||||
if let Some(src) = &page.src {
|
||||
if !page.is_asset {
|
||||
// get output file extention
|
||||
let extention = match page.target_extention.len() {
|
||||
0 => String::from("html"),
|
||||
_ => page.target_extention.clone()
|
||||
};
|
||||
|
||||
if !index.is_asset {
|
||||
// find template
|
||||
let templ: Option<Template> = {
|
||||
let mut out = None;
|
||||
for templ in templates.clone() {
|
||||
if let Some(templ) = templ.search(index.template.clone()) {
|
||||
out = Some(templ);
|
||||
break;
|
||||
}
|
||||
}
|
||||
out
|
||||
};
|
||||
|
||||
// get output file extention
|
||||
let extention = match index.target_extention.len() {
|
||||
0 => match templ {
|
||||
Some(templ) => templ.extention,
|
||||
None => String::from("html")
|
||||
}
|
||||
_ => index.target_extention.clone()
|
||||
};
|
||||
let dest_path: &Path = &cur_path.join(format!("{}.{}", friendly, extention));
|
||||
// render the content
|
||||
let dest_path_str = dest_path.to_str();
|
||||
if dest_path_str == None {
|
||||
println!("ERROR: dest path is None for page: {}", friendly);
|
||||
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!(),
|
||||
};
|
||||
}
|
||||
else {
|
||||
// file is an asset, no rendering done for assets
|
||||
let dest_path: &Path = &cur_path.join(friendly.clone());
|
||||
match dest_path.to_str() {
|
||||
Some(dest_path_str) => {
|
||||
println!("INFO: copy {}", dest_path_str);
|
||||
let _ = fs::copy(src, dest_path);
|
||||
}
|
||||
None => {
|
||||
println!("ERROR: dest path is None for asset: {}", friendly);
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
None => {
|
||||
let _ = fs::create_dir(dest_path);
|
||||
},
|
||||
};
|
||||
|
||||
for page in &index.sub_pages {
|
||||
render_index(out_path, page, dest_path, &site_index, Some(templates.clone()), Some(&jinja_env));
|
||||
}
|
||||
}
|
||||
|
||||
fn version() {
|
||||
println!("version: {}", VERSION);
|
||||
}
|
||||
|
||||
fn usage() {
|
||||
println!("webtemplate [--output=<dir>] [--src=<dir>]");
|
||||
}
|
||||
|
||||
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;
|
||||
let dest_path: &Path = &cur_path.join(format!("{}.{}", friendly, extention));
|
||||
// render the content
|
||||
println!("INFO: render {}", dest_path.to_str().unwrap());
|
||||
match index.render_page(page) {
|
||||
Ok(content) => {
|
||||
let _: Result<(), Infallible> = fs::write(dest_path, content).or_else(|e|{
|
||||
println!("ERROR during writing: {:?}", e);
|
||||
Ok(())
|
||||
});
|
||||
},
|
||||
Err(e) => {
|
||||
println!("ERROR during rendering: {:?}", e);
|
||||
},
|
||||
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;
|
||||
}
|
||||
}
|
||||
else {
|
||||
// file is an asset, no rendering done for assets
|
||||
let dest_path: &Path = &cur_path.join(friendly);
|
||||
match dest_path.to_str() {
|
||||
Some(dest_path_str) => {
|
||||
println!("INFO: copy {}", dest_path_str);
|
||||
let _: Result<u64, Infallible> = fs::copy(src, dest_path).or_else(|e|{
|
||||
println!("ERROR during copying: {:?}", e);
|
||||
Ok(0)
|
||||
});
|
||||
}
|
||||
"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;
|
||||
None => {
|
||||
println!("ERROR: dest path is None for asset: {}", friendly);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
let src_path = Path::new(&src_path_str);
|
||||
let out_path = Path::new(&out_path_str);
|
||||
for page in page.sub_pages.iter() {
|
||||
render_page(page, out_path, &index, cur_path);
|
||||
}
|
||||
}
|
||||
|
||||
// scan all pages, template, components, etc.
|
||||
let index = match Renderer::index(src_path) {
|
||||
/// 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,
|
||||
|
||||
/// 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 args = Args::parse();
|
||||
if args.version {
|
||||
println!("version: {}", VERSION);
|
||||
return;
|
||||
}
|
||||
|
||||
let src_path = Path::new(&args.source);
|
||||
let out_path = Path::new(&args.output);
|
||||
|
||||
// scan all pages adn templates
|
||||
let index = match Renderer::index(src_path).or_else::<Infallible, _>(|e| {
|
||||
panic!("PANIC: faild to index: {:?}", e);
|
||||
}).unwrap() {
|
||||
Some(index) => index,
|
||||
None => {
|
||||
println!("ERROR: src directory not found ({})", src_path_str);
|
||||
return;
|
||||
panic!("ERROR: src directory not found ({})", src_path.to_str().or(Some("Utf parse error")).unwrap());
|
||||
},
|
||||
};
|
||||
|
||||
// render website
|
||||
let _ = fs::create_dir_all(out_path);
|
||||
render_index(out_path, &index.site, out_path, &index, None, None);
|
||||
// render pages
|
||||
render_page(&index.site, out_path, &index, out_path);
|
||||
}
|
||||
|
||||
119
src/render.rs
119
src/render.rs
@@ -1,60 +1,46 @@
|
||||
mod index;
|
||||
mod parse_md;
|
||||
|
||||
use std::{collections::BTreeMap, fs, path::Path};
|
||||
use minijinja::{context, Environment, Value};
|
||||
use index::indexer;
|
||||
use parse_md::parse_md;
|
||||
use std::{collections::BTreeMap, fs, io::Error as IoError, path::Path};
|
||||
use minijinja::{Environment, Error as JinjaError, Value, context};
|
||||
use index::indexer::Error as IndexError;
|
||||
use yaml_rust2::Yaml;
|
||||
use crate::render::{index::indexer::split_params, parse_md::{Parser, Error as ParserError}};
|
||||
|
||||
pub use index::indexer::{IndexItem, Template};
|
||||
|
||||
pub fn build_jinja_env<'a>(templates: Vec<Box<indexer::Template>>) -> Environment<'a> {
|
||||
build_jinja_env_dir(templates)
|
||||
}
|
||||
fn build_jinja_env_dir<'a>(templates: Vec<Box<indexer::Template>>) -> Environment<'a> {
|
||||
let mut env: Environment = Environment::new();
|
||||
for template in templates {
|
||||
match template.src {
|
||||
Some(src) => {
|
||||
match env.add_template_owned(template.name.clone(), src) {
|
||||
Ok(_) => {},
|
||||
Err(err) => println!("ERROR: failt to add template \"{}\" ({err:?})", template.name),
|
||||
}
|
||||
},
|
||||
None => {
|
||||
for plate in template.sub_templates {
|
||||
if let Some(src) = plate.src {
|
||||
let name = format!("{}/{}", template.name.clone(), plate.name.clone());
|
||||
match env.add_template_owned(name.clone(), src) {
|
||||
Ok(_) => {}, //println!("template name: {name}"),
|
||||
Err(err) => println!("ERROR: failt to add template \"{name}\" ({err:?})"),
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
env
|
||||
#[derive(Debug)]
|
||||
pub enum Error {
|
||||
NoSrc,
|
||||
Io(IoError),
|
||||
Jinja(JinjaError),
|
||||
Indexer(IndexError),
|
||||
Parser(ParserError),
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct Renderer {
|
||||
// jinja_env: Environment<'a>,
|
||||
pub struct Renderer<'a> {
|
||||
pub site: IndexItem,
|
||||
pub path: Box<Path>
|
||||
templates: Vec<Template>,
|
||||
jinja_env: Environment<'a>,
|
||||
}
|
||||
impl Renderer {
|
||||
pub fn index(path: &Path) -> Option<Renderer> {
|
||||
if let Some(site_index) = IndexItem::index(&path) {
|
||||
Some(Renderer {
|
||||
impl<'a> Renderer<'a> {
|
||||
pub fn index(path: &Path) -> Result<Option<Renderer>, Error> {
|
||||
if let Some(site_index) = IndexItem::index(&path).or_else(|e| Err(Error::Indexer(e)))? {
|
||||
let templates = Template::index(&path.join("templates")).or_else(|e| Err(Error::Indexer(e)))?;
|
||||
let mut jinja_env = Environment::new();
|
||||
for tmpl in templates.iter() {
|
||||
tmpl.add_to_jinja_env(&mut jinja_env).or_else(|e| Err(Error::Jinja(e)))?;
|
||||
};
|
||||
Ok(Some(Renderer {
|
||||
site: site_index,
|
||||
path: Box::from(path)
|
||||
})
|
||||
templates,
|
||||
jinja_env,
|
||||
}))
|
||||
}
|
||||
else {
|
||||
None
|
||||
Ok(None)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -89,42 +75,27 @@ impl Renderer {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn render_page(&self, page: &IndexItem, index: &IndexItem, jinja_env: &Environment) -> Option<String> {
|
||||
pub fn render_page(&self, page: &IndexItem) -> Result<String, Error> {
|
||||
if page.src == None {
|
||||
return None;
|
||||
}
|
||||
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;
|
||||
},
|
||||
};
|
||||
return Err(Error::NoSrc);
|
||||
};
|
||||
let content = fs::read_to_string(page.src.as_ref().unwrap()).or_else(|e| Err(Error::Io(e)))?;
|
||||
let split = split_params(content);
|
||||
let mut parser = Parser::new(&split.md, &self.templates);
|
||||
match parser.render() {
|
||||
Ok(body) => {
|
||||
let template = self.jinja_env.get_template(&page.template).or_else(|e| Err(Error::Jinja(e)))?;
|
||||
let html = template.render(context! {
|
||||
index => self.site.to_minijinja(),
|
||||
is_error_not_found => false,
|
||||
url => page.get_url(),
|
||||
body,
|
||||
params => Renderer::convert_yaml(split.yaml)
|
||||
}).or_else(|e| Err(Error::Jinja(e)))?;
|
||||
|
||||
Some(html)
|
||||
},
|
||||
None => None
|
||||
}
|
||||
Ok(html)
|
||||
},
|
||||
Err(_) => None,
|
||||
Err(e) => Err(Error::Parser(e))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,10 +1,36 @@
|
||||
|
||||
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 minijinja::{Environment, Error as JinjaError};
|
||||
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<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)]
|
||||
pub struct IndexItem {
|
||||
// path to source file. If none, it is a directory without an index page
|
||||
@@ -41,111 +67,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("/"))
|
||||
}
|
||||
|
||||
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();
|
||||
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<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 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) {
|
||||
@@ -214,10 +219,10 @@ pub mod indexer {
|
||||
pub extention: String,
|
||||
pub path: Option<String>,
|
||||
pub src: Option<String>,
|
||||
pub sub_templates: Vec<Box<Template>>
|
||||
pub sub_templates: Vec<Template>
|
||||
}
|
||||
impl Template {
|
||||
fn new() -> Template {
|
||||
const fn new() -> Template {
|
||||
Template {
|
||||
name: String::new(),
|
||||
extention: String::new(),
|
||||
@@ -254,31 +259,28 @@ pub mod indexer {
|
||||
None
|
||||
}
|
||||
|
||||
pub fn index(path: &Path) -> Vec<Box<Self>> {
|
||||
match Self::scan_template(path) {
|
||||
Some(templates) => templates.sub_templates,
|
||||
None => Vec::new(),
|
||||
pub fn index(path: &Path) -> Result<Vec<Self>, Error> {
|
||||
match Self::scan_template(path)? {
|
||||
Some(templates) => Ok(templates.sub_templates),
|
||||
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();
|
||||
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 +294,52 @@ 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<Box<Template>> {
|
||||
let mut items: Vec<Box<Template>> = Vec::new();
|
||||
fn scan_dir(path: &Path) -> Result<Vec<Template>, Error> {
|
||||
let mut items: Vec<Template> = Vec::new();
|
||||
|
||||
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<Template> = Template::scan_template(&entry.path())?;
|
||||
if let Some(i) = item {
|
||||
items.push(i);
|
||||
}
|
||||
}
|
||||
|
||||
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!()
|
||||
};
|
||||
|
||||
items.sort_by(|i, c| i.name.cmp(&c.name));
|
||||
items
|
||||
Ok(items)
|
||||
}
|
||||
}
|
||||
impl Template {
|
||||
pub fn add_to_jinja_env(&self, env: &mut Environment) -> Result<(), JinjaError> {
|
||||
self.add_to_jinja_env_with_path(env, String::new())
|
||||
}
|
||||
|
||||
fn add_to_jinja_env_with_path(&self, env: &mut Environment, mut path: String) -> Result<(), JinjaError> {
|
||||
path.push_str(&self.name);
|
||||
if let Some(source) = &self.src {
|
||||
env.add_template_owned(path.clone(), source.clone())?;
|
||||
}
|
||||
path.push_str("/");
|
||||
for tmpl in self.sub_templates.iter() {
|
||||
tmpl.add_to_jinja_env_with_path(env, path.clone())?;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,131 +1,578 @@
|
||||
use std::{collections::BTreeMap};
|
||||
|
||||
use pulldown_cmark::{Event, Options, Parser, Tag};
|
||||
use minijinja::{context, Environment, Value};
|
||||
use pulldown_cmark::{Options, Parser as PulldownParser, Tree, TreeEvent};
|
||||
use minijinja::{Environment, Value, context, Error as JinjaError};
|
||||
use regex::Regex;
|
||||
|
||||
pub fn parse_md(md: String, jinja_env: &Environment) -> Option<String> {
|
||||
let mut options = Options::empty();
|
||||
options.insert(Options::ENABLE_TABLES);
|
||||
options.insert(Options::ENABLE_FOOTNOTES);
|
||||
options.insert(Options::ENABLE_STRIKETHROUGH);
|
||||
options.insert(Options::ENABLE_TASKLISTS);
|
||||
// options.insert(Options::ENABLE_SMART_PUNCTUATION);
|
||||
// options.insert(Options::ENABLE_HEADING_ATTRIBUTES);
|
||||
options.insert(Options::ENABLE_YAML_STYLE_METADATA_BLOCKS);
|
||||
// options.insert(Options::ENABLE_PLUSES_DELIMITED_METADATA_BLOCKS);
|
||||
// options.insert(Options::ENABLE_OLD_FOOTNOTES);
|
||||
options.insert(Options::ENABLE_MATH);
|
||||
options.insert(Options::ENABLE_GFM);
|
||||
options.insert(Options::ENABLE_DEFINITION_LIST);
|
||||
options.insert(Options::ENABLE_SUPERSCRIPT);
|
||||
options.insert(Options::ENABLE_SUBSCRIPT);
|
||||
options.insert(Options::ENABLE_WIKILINKS);
|
||||
use crate::render::Template;
|
||||
|
||||
let md = md_preprocessor(&md, jinja_env);
|
||||
#[derive(Debug)]
|
||||
pub enum Error {
|
||||
NoCurrentEvent,
|
||||
JinjaError(JinjaError),
|
||||
TemplateNotFound
|
||||
}
|
||||
impl PartialEq for Error {
|
||||
fn eq(&self, other: &Self) -> bool {
|
||||
match (self, other) {
|
||||
(Self::JinjaError(l0), Self::JinjaError(r0)) => {
|
||||
l0.kind() == r0.kind()
|
||||
},
|
||||
_ => core::mem::discriminant(self) == core::mem::discriminant(other),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// let mut parser = Parser::new_ext("", options);
|
||||
// let mut in_heading_two: Vec<Event> = Vec::new();
|
||||
let parser = Parser::new_ext(&md, options).filter_map(|event| {
|
||||
match &event {
|
||||
Event::Start(tag) => {
|
||||
match tag {
|
||||
Tag::Paragraph => Some(event),
|
||||
Tag::Heading { level, id, classes, attrs } => Some(event),
|
||||
Tag::BlockQuote(_block_quote_kind) => Some(event),
|
||||
Tag::CodeBlock(_code_block_kind) => Some(event),
|
||||
Tag::HtmlBlock => Some(event),
|
||||
Tag::List(_) => Some(event),
|
||||
Tag::Item => Some(event),
|
||||
Tag::FootnoteDefinition(_cow_str) => Some(event),
|
||||
Tag::DefinitionList => Some(event),
|
||||
Tag::DefinitionListTitle => Some(event),
|
||||
Tag::DefinitionListDefinition => Some(event),
|
||||
Tag::Table(_alignments) => Some(event),
|
||||
Tag::TableHead => Some(event),
|
||||
Tag::TableRow => Some(event),
|
||||
Tag::TableCell => Some(event),
|
||||
Tag::Emphasis => Some(event),
|
||||
Tag::Strong => Some(event),
|
||||
Tag::Strikethrough => Some(event),
|
||||
Tag::Superscript => Some(event),
|
||||
Tag::Subscript => Some(event),
|
||||
Tag::Link { link_type, dest_url, title, id } => {
|
||||
let re = Regex::new(r"^%(?<tags>[^%]+)%").unwrap();
|
||||
// let out = .unwrap();
|
||||
let (url, args) = match re.captures(&dest_url) {
|
||||
Some(args) => {
|
||||
(dest_url.strip_prefix(args.get(0).unwrap().as_str()).unwrap().to_string(), parse_args(args.name("tags").unwrap().as_str()))
|
||||
},
|
||||
None => (dest_url.to_string(), Value::UNDEFINED)
|
||||
};
|
||||
match render_component("link_start", context! {
|
||||
link_type => link_type,
|
||||
dest_url => url,
|
||||
title => title,
|
||||
id => id,
|
||||
args => args
|
||||
}, jinja_env)
|
||||
{
|
||||
Some(html) => Some(Event::Html(html.into())),
|
||||
None => Some(event),
|
||||
}
|
||||
pub struct Parser<'a> {
|
||||
md: &'a str,
|
||||
tree: Option<Tree<TreeEvent>>,
|
||||
templates: &'a Vec<Template>,
|
||||
jinja_env: Environment<'a>
|
||||
}
|
||||
impl<'a> Parser<'a> {
|
||||
pub fn new(md: &'a str, templates: &'a Vec<Template>) -> Self {
|
||||
let mut env = Environment::new();
|
||||
for tmpl in templates.iter() {
|
||||
tmpl.add_to_jinja_env(&mut env).unwrap();
|
||||
};
|
||||
|
||||
Self {
|
||||
md,
|
||||
tree: None,
|
||||
templates,
|
||||
jinja_env: env
|
||||
}
|
||||
}
|
||||
|
||||
pub fn render(&mut self) -> Result<String, Error> {
|
||||
let mut options = Options::empty();
|
||||
options.insert(Options::ENABLE_TABLES);
|
||||
options.insert(Options::ENABLE_FOOTNOTES);
|
||||
options.insert(Options::ENABLE_STRIKETHROUGH);
|
||||
options.insert(Options::ENABLE_TASKLISTS);
|
||||
// options.insert(Options::ENABLE_SMART_PUNCTUATION);
|
||||
// options.insert(Options::ENABLE_HEADING_ATTRIBUTES);
|
||||
options.insert(Options::ENABLE_YAML_STYLE_METADATA_BLOCKS);
|
||||
// options.insert(Options::ENABLE_PLUSES_DELIMITED_METADATA_BLOCKS);
|
||||
// options.insert(Options::ENABLE_OLD_FOOTNOTES);
|
||||
options.insert(Options::ENABLE_MATH);
|
||||
options.insert(Options::ENABLE_GFM);
|
||||
options.insert(Options::ENABLE_DEFINITION_LIST);
|
||||
options.insert(Options::ENABLE_SUPERSCRIPT);
|
||||
options.insert(Options::ENABLE_SUBSCRIPT);
|
||||
options.insert(Options::ENABLE_WIKILINKS);
|
||||
|
||||
let md = self.md_preprocessor()?;
|
||||
let mut parser = PulldownParser::new_ext(&md, options);
|
||||
self.tree = Some(parser.to_event_tree());
|
||||
let mut branch = Vec::new();
|
||||
|
||||
self.md_parser_children(&mut branch)
|
||||
}
|
||||
|
||||
fn md_preprocessor(&self) -> Result<String, Error> {
|
||||
let mut new = String::with_capacity(self.md.len());
|
||||
let mut last_match = 0;
|
||||
let re = Regex::new(r"\{\% *(.*)\((.*)\).*\%\}").unwrap();
|
||||
for caps in re.captures_iter(self.md) {
|
||||
let m = caps.get(0).unwrap();
|
||||
new.push_str(&self.md[last_match..m.start()]);
|
||||
let template = &caps[1];
|
||||
let args = &caps[2];
|
||||
new.push_str(&self.render_component(template, context! { args => parse_args(args)})?);
|
||||
last_match = m.end();
|
||||
}
|
||||
new.push_str(&self.md[last_match..]);
|
||||
Ok(new)
|
||||
}
|
||||
|
||||
fn md_parser_children(&mut self, branch: &mut Vec<TreeEvent>) -> Result<String, Error> {
|
||||
let mut content = String::new();
|
||||
loop {
|
||||
let event = self.tree.as_ref().unwrap().get_cur().unwrap();
|
||||
let child_content = match self.tree.as_mut().unwrap().goto_first_child() {
|
||||
Some(_) => {
|
||||
branch.push(event.clone());
|
||||
let content = self.md_parser_children(branch)?;
|
||||
branch.pop();
|
||||
self.tree.as_mut().unwrap().goto_parent();
|
||||
|
||||
content
|
||||
},
|
||||
None => String::new(),
|
||||
};
|
||||
content.push_str(&self.md_parser_event(branch, child_content)?);
|
||||
|
||||
if self.tree.as_mut().unwrap().goto_next() == None {
|
||||
break;
|
||||
}
|
||||
}
|
||||
Ok(content)
|
||||
}
|
||||
|
||||
fn md_parser_event(&self, branch: &Vec<TreeEvent>, content: String) -> Result<String, Error> {
|
||||
match self.tree.as_ref().unwrap().get_cur().ok_or(Error::NoCurrentEvent)? {
|
||||
TreeEvent::Root => {
|
||||
Ok(content)
|
||||
},
|
||||
TreeEvent::Paragraph => {
|
||||
self.render_component("Paragraph", context! {
|
||||
content,
|
||||
parents => branch
|
||||
}).or_else(|e| {
|
||||
if e == Error::TemplateNotFound {
|
||||
todo!()
|
||||
} else {
|
||||
Err(e)
|
||||
}
|
||||
})
|
||||
},
|
||||
TreeEvent::Heading { level, id, classes, attrs } => {
|
||||
self.render_component("Heading", context! {
|
||||
content,
|
||||
parents => branch,
|
||||
level => level as u8,
|
||||
id, classes, attrs
|
||||
}).or_else(|e| {
|
||||
if e == Error::TemplateNotFound {
|
||||
todo!()
|
||||
} else {
|
||||
Err(e)
|
||||
}
|
||||
})
|
||||
},
|
||||
TreeEvent::BlockQuote(block_quote_kind) => {
|
||||
self.render_component("BlockQuote", context! {
|
||||
content,
|
||||
parents => branch,
|
||||
block_quote_kind
|
||||
}).or_else(|e| {
|
||||
if e == Error::TemplateNotFound {
|
||||
todo!()
|
||||
} else {
|
||||
Err(e)
|
||||
}
|
||||
})
|
||||
},
|
||||
TreeEvent::CodeBlock => {
|
||||
self.render_component("CodeBlock", context! {
|
||||
content,
|
||||
parents => branch
|
||||
}).or_else(|e| {
|
||||
if e == Error::TemplateNotFound {
|
||||
todo!()
|
||||
} else {
|
||||
Err(e)
|
||||
}
|
||||
})
|
||||
},
|
||||
TreeEvent::ContainerBlock(container_kind, _) => {
|
||||
self.render_component("ContainerBlock", context! {
|
||||
content,
|
||||
parents => branch,
|
||||
container_kind
|
||||
}).or_else(|e| {
|
||||
if e == Error::TemplateNotFound {
|
||||
todo!()
|
||||
} else {
|
||||
Err(e)
|
||||
}
|
||||
})
|
||||
},
|
||||
TreeEvent::HtmlBlock => {
|
||||
self.render_component("HtmlBlock", context! {
|
||||
content,
|
||||
parents => branch
|
||||
}).or_else(|e| {
|
||||
if e == Error::TemplateNotFound {
|
||||
todo!()
|
||||
} else {
|
||||
Err(e)
|
||||
}
|
||||
})
|
||||
},
|
||||
TreeEvent::List(_) => {
|
||||
self.render_component("List", context! {
|
||||
content,
|
||||
parents => branch
|
||||
}).or_else(|e| {
|
||||
if e == Error::TemplateNotFound {
|
||||
todo!()
|
||||
} else {
|
||||
Err(e)
|
||||
}
|
||||
})
|
||||
},
|
||||
TreeEvent::Item => {
|
||||
self.render_component("Item", context! {
|
||||
content,
|
||||
parents => branch
|
||||
}).or_else(|e| {
|
||||
if e == Error::TemplateNotFound {
|
||||
todo!()
|
||||
} else {
|
||||
Err(e)
|
||||
}
|
||||
})
|
||||
},
|
||||
TreeEvent::FootnoteDefinition(_) => {
|
||||
self.render_component("FootnoteDefinition", context! {
|
||||
content,
|
||||
parents => branch
|
||||
}).or_else(|e| {
|
||||
if e == Error::TemplateNotFound {
|
||||
todo!()
|
||||
} else {
|
||||
Err(e)
|
||||
}
|
||||
})
|
||||
},
|
||||
TreeEvent::DefinitionList => {
|
||||
self.render_component("DefinitionList", context! {
|
||||
content,
|
||||
parents => branch
|
||||
}).or_else(|e| {
|
||||
if e == Error::TemplateNotFound {
|
||||
todo!()
|
||||
} else {
|
||||
Err(e)
|
||||
}
|
||||
})
|
||||
},
|
||||
TreeEvent::DefinitionListTitle => {
|
||||
self.render_component("DefinitionListTitle", context! {
|
||||
content,
|
||||
parents => branch
|
||||
}).or_else(|e| {
|
||||
if e == Error::TemplateNotFound {
|
||||
todo!()
|
||||
} else {
|
||||
Err(e)
|
||||
}
|
||||
})
|
||||
},
|
||||
TreeEvent::DefinitionListDefinition => {
|
||||
self.render_component("DefinitionListDefinition", context! {
|
||||
content,
|
||||
parents => branch
|
||||
}).or_else(|e| {
|
||||
if e == Error::TemplateNotFound {
|
||||
todo!()
|
||||
} else {
|
||||
Err(e)
|
||||
}
|
||||
})
|
||||
},
|
||||
TreeEvent::Table(alignments) => {
|
||||
self.render_component("Table", context! {
|
||||
content,
|
||||
parents => branch,
|
||||
alignments
|
||||
}).or_else(|e| {
|
||||
if e == Error::TemplateNotFound {
|
||||
todo!()
|
||||
} else {
|
||||
Err(e)
|
||||
}
|
||||
})
|
||||
},
|
||||
TreeEvent::TableHead => {
|
||||
self.render_component("TableHead", context! {
|
||||
content,
|
||||
parents => branch
|
||||
}).or_else(|e| {
|
||||
if e == Error::TemplateNotFound {
|
||||
todo!()
|
||||
} else {
|
||||
Err(e)
|
||||
}
|
||||
})
|
||||
},
|
||||
TreeEvent::TableRow => {
|
||||
self.render_component("TableRow", context! {
|
||||
content,
|
||||
parents => branch
|
||||
}).or_else(|e| {
|
||||
if e == Error::TemplateNotFound {
|
||||
todo!()
|
||||
} else {
|
||||
Err(e)
|
||||
}
|
||||
})
|
||||
},
|
||||
TreeEvent::TableCell => {
|
||||
self.render_component("TableCell", context! {
|
||||
content,
|
||||
parents => branch
|
||||
}).or_else(|e| {
|
||||
if e == Error::TemplateNotFound {
|
||||
todo!()
|
||||
} else {
|
||||
Err(e)
|
||||
}
|
||||
})
|
||||
},
|
||||
TreeEvent::Emphasis => {
|
||||
self.render_component("Emphasis", context! {
|
||||
content,
|
||||
parents => branch
|
||||
}).or_else(|e| {
|
||||
if e == Error::TemplateNotFound {
|
||||
todo!()
|
||||
} else {
|
||||
Err(e)
|
||||
}
|
||||
})
|
||||
},
|
||||
TreeEvent::Strong => {
|
||||
self.render_component("Strong", context! {
|
||||
content,
|
||||
parents => branch
|
||||
}).or_else(|e| {
|
||||
if e == Error::TemplateNotFound {
|
||||
todo!()
|
||||
} else {
|
||||
Err(e)
|
||||
}
|
||||
})
|
||||
},
|
||||
TreeEvent::Strikethrough => {
|
||||
self.render_component("Strikethrough", context! {
|
||||
content,
|
||||
parents => branch
|
||||
}).or_else(|e| {
|
||||
if e == Error::TemplateNotFound {
|
||||
todo!()
|
||||
} else {
|
||||
Err(e)
|
||||
}
|
||||
})
|
||||
},
|
||||
TreeEvent::Superscript => {
|
||||
self.render_component("Superscript", context! {
|
||||
content,
|
||||
parents => branch
|
||||
}).or_else(|e| {
|
||||
if e == Error::TemplateNotFound {
|
||||
todo!()
|
||||
} else {
|
||||
Err(e)
|
||||
}
|
||||
})
|
||||
},
|
||||
TreeEvent::Subscript => {
|
||||
self.render_component("Subscript", context! {
|
||||
content,
|
||||
parents => branch
|
||||
}).or_else(|e| {
|
||||
if e == Error::TemplateNotFound {
|
||||
todo!()
|
||||
} else {
|
||||
Err(e)
|
||||
}
|
||||
})
|
||||
},
|
||||
TreeEvent::Link { link_type, dest_url, title, id } => {
|
||||
let re = Regex::new(r"^%(?<tags>[^%]+)%").unwrap();
|
||||
let (_, args) = match re.captures(&dest_url) {
|
||||
Some(args) => {
|
||||
(
|
||||
dest_url.strip_prefix(args.get(0).unwrap().as_str()).unwrap().to_string(),
|
||||
parse_args(args.name("tags").unwrap().as_str())
|
||||
)
|
||||
},
|
||||
Tag::Image { link_type, dest_url, title, id } => Some(event),
|
||||
Tag::MetadataBlock(_metadata_block_kind) => Some(event),
|
||||
None => (dest_url.to_string(), Value::UNDEFINED)
|
||||
};
|
||||
self.render_component("Link", context! {
|
||||
content,
|
||||
parents => branch,
|
||||
link_type, dest_url, title, id, args
|
||||
}).or_else(|e| {
|
||||
if e == Error::TemplateNotFound {
|
||||
todo!()
|
||||
} else {
|
||||
Err(e)
|
||||
}
|
||||
})
|
||||
},
|
||||
TreeEvent::Image { link_type, dest_url, title, id } => {
|
||||
self.render_component("Image", context! {
|
||||
content,
|
||||
parents => branch,
|
||||
link_type, dest_url, title, id
|
||||
}).or_else(|e| {
|
||||
if e == Error::TemplateNotFound {
|
||||
todo!()
|
||||
} else {
|
||||
Err(e)
|
||||
}
|
||||
})
|
||||
},
|
||||
TreeEvent::MetadataBlock(metadata_block_kind) => {
|
||||
self.render_component("MetadataBlock", context! {
|
||||
content,
|
||||
parents => branch,
|
||||
metadata_block_kind
|
||||
}).or_else(|e| {
|
||||
if e == Error::TemplateNotFound {
|
||||
todo!()
|
||||
} else {
|
||||
Err(e)
|
||||
}
|
||||
})
|
||||
},
|
||||
TreeEvent::Text(text) => {
|
||||
self.render_component("Text", context! {
|
||||
content,
|
||||
parents => branch,
|
||||
text
|
||||
}).or_else(|e| {
|
||||
if e == Error::TemplateNotFound {
|
||||
todo!()
|
||||
} else {
|
||||
Err(e)
|
||||
}
|
||||
})
|
||||
},
|
||||
TreeEvent::Code(_) => {
|
||||
self.render_component("Code", context! {
|
||||
content,
|
||||
parents => branch
|
||||
}).or_else(|e| {
|
||||
if e == Error::TemplateNotFound {
|
||||
todo!()
|
||||
} else {
|
||||
Err(e)
|
||||
}
|
||||
})
|
||||
},
|
||||
TreeEvent::InlineMath(_) => {
|
||||
self.render_component("InlineMath", context! {
|
||||
content,
|
||||
parents => branch
|
||||
}).or_else(|e| {
|
||||
if e == Error::TemplateNotFound {
|
||||
todo!()
|
||||
} else {
|
||||
Err(e)
|
||||
}
|
||||
})
|
||||
},
|
||||
TreeEvent::DisplayMath(_) => {
|
||||
self.render_component("DisplayMath", context! {
|
||||
content,
|
||||
parents => branch
|
||||
}).or_else(|e| {
|
||||
if e == Error::TemplateNotFound {
|
||||
todo!()
|
||||
} else {
|
||||
Err(e)
|
||||
}
|
||||
})
|
||||
},
|
||||
TreeEvent::Html(_) => {
|
||||
self.render_component("Html", context! {
|
||||
content,
|
||||
parents => branch
|
||||
}).or_else(|e| {
|
||||
if e == Error::TemplateNotFound {
|
||||
todo!()
|
||||
} else {
|
||||
Err(e)
|
||||
}
|
||||
})
|
||||
},
|
||||
TreeEvent::InlineHtml(_) => {
|
||||
self.render_component("InlineHtml", context! {
|
||||
content,
|
||||
parents => branch
|
||||
}).or_else(|e| {
|
||||
if e == Error::TemplateNotFound {
|
||||
todo!()
|
||||
} else {
|
||||
Err(e)
|
||||
}
|
||||
})
|
||||
},
|
||||
TreeEvent::FootnoteReference(_) => {
|
||||
self.render_component("FootnoteReference", context! {
|
||||
content,
|
||||
parents => branch
|
||||
}).or_else(|e| {
|
||||
if e == Error::TemplateNotFound {
|
||||
todo!()
|
||||
} else {
|
||||
Err(e)
|
||||
}
|
||||
})
|
||||
},
|
||||
TreeEvent::SoftBreak => {
|
||||
self.render_component("SoftBreak", context! {
|
||||
content,
|
||||
parents => branch
|
||||
}).or_else(|e| {
|
||||
if e == Error::TemplateNotFound {
|
||||
todo!()
|
||||
} else {
|
||||
Err(e)
|
||||
}
|
||||
})
|
||||
},
|
||||
TreeEvent::HardBreak => {
|
||||
self.render_component("HardBreak", context! {
|
||||
content,
|
||||
parents => branch
|
||||
}).or_else(|e| {
|
||||
if e == Error::TemplateNotFound {
|
||||
todo!()
|
||||
} else {
|
||||
Err(e)
|
||||
}
|
||||
})
|
||||
},
|
||||
TreeEvent::Rule => {
|
||||
self.render_component("Rule", context! {
|
||||
content,
|
||||
parents => branch
|
||||
}).or_else(|e| {
|
||||
if e == Error::TemplateNotFound {
|
||||
todo!()
|
||||
} else {
|
||||
Err(e)
|
||||
}
|
||||
})
|
||||
},
|
||||
TreeEvent::TaskListMarker(checked) => {
|
||||
self.render_component("TaskListMarker", context! {
|
||||
content,
|
||||
parents => branch,
|
||||
checked
|
||||
}).or_else(|e| {
|
||||
if e == Error::TemplateNotFound {
|
||||
todo!()
|
||||
} else {
|
||||
Err(e)
|
||||
}
|
||||
})
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
fn render_component(&self, component: &str, args: Value) -> Result<String, Error> {
|
||||
match self.jinja_env.get_template(&format!("components/{component}")) {
|
||||
Ok(tmpl) => {
|
||||
match tmpl.render(args) {
|
||||
Ok(html) => Ok(html),
|
||||
Err(err) => Err(Error::JinjaError(err)),
|
||||
}
|
||||
},
|
||||
Err(e) => {
|
||||
match e.kind() {
|
||||
minijinja::ErrorKind::TemplateNotFound => Err(Error::TemplateNotFound),
|
||||
_ => Err(Error::JinjaError(e)),
|
||||
}
|
||||
},
|
||||
Event::End(_tag_end) => Some(event),
|
||||
Event::Text(_cow_str) => Some(event),
|
||||
Event::Code(_cow_str) => Some(event),
|
||||
Event::InlineMath(_cow_str) => Some(event),
|
||||
Event::DisplayMath(_cow_str) => Some(event),
|
||||
Event::Html(_cow_str) => Some(event),
|
||||
Event::InlineHtml(_cow_str) => Some(event),
|
||||
Event::FootnoteReference(_cow_str) => Some(event),
|
||||
Event::SoftBreak => Some(event),
|
||||
Event::HardBreak => Some(event),
|
||||
Event::Rule => Some(event),
|
||||
Event::TaskListMarker(_) => Some(event),
|
||||
}
|
||||
});
|
||||
let mut html = String::new();
|
||||
pulldown_cmark::html::push_html(&mut html, parser);
|
||||
|
||||
Some(html)
|
||||
}
|
||||
|
||||
fn md_preprocessor(md: &String, jinja_env: &Environment) -> String {
|
||||
let re = Regex::new(r"\{\% *(.*)\((.*)\).*\%\}").unwrap();
|
||||
let out = re.replace_all(md, |capture: ®ex::Captures<'_>| {
|
||||
let template = &capture[1];
|
||||
let args = &capture[2];
|
||||
match render_component(template, context! { args => parse_args(args)}, jinja_env){
|
||||
Some(html) => html,
|
||||
None => String::from("")
|
||||
}
|
||||
});
|
||||
|
||||
String::from(out)
|
||||
}
|
||||
|
||||
fn render_component(component: &str, args: Value, jinja_env: &Environment) -> Option<String> {
|
||||
// println!(" tag found: {component} <- {args}");
|
||||
match jinja_env.get_template(&format!("components/{component}")) {
|
||||
Ok(ding) => {
|
||||
// match ding.render(context! { args => args }) {
|
||||
match ding.render(args) {
|
||||
Ok(html) => Some(html),
|
||||
Err(err) => {
|
||||
println!("ERROR: faild to render template: {component} ({err:?})");
|
||||
None
|
||||
},
|
||||
}
|
||||
},
|
||||
Err(_) => {
|
||||
println!("ERROR: could not find template: {component}");
|
||||
Some("".to_string())
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user