From 0fd74dd4495ff0007f341df3ab78fa84942b85a7 Mon Sep 17 00:00:00 2001 From: Mylloon Date: Thu, 20 Apr 2023 14:41:36 +0200 Subject: [PATCH] Add date support! --- Cargo.lock | 196 ++++++++++++++++++++++++++++++++++++++++++++- Cargo.toml | 1 + src/routes/blog.rs | 9 ++- src/template.rs | 32 +++++++- 4 files changed, 228 insertions(+), 10 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 3ed4cf5..7c2c6eb 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -190,7 +190,7 @@ dependencies = [ "serde_urlencoded", "smallvec", "socket2", - "time", + "time 0.3.20", "url", ] @@ -259,6 +259,15 @@ dependencies = [ "alloc-no-stdlib", ] +[[package]] +name = "android_system_properties" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "819e7219dbd41043ac279b19830f2efc897156490d7fd6ea916720117ee66311" +dependencies = [ + "libc", +] + [[package]] name = "arrayvec" version = "0.7.2" @@ -429,6 +438,32 @@ version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" +[[package]] +name = "chrono" +version = "0.4.24" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4e3c5919066adf22df73762e50cffcde3a758f2a848b113b586d1f86728b673b" +dependencies = [ + "iana-time-zone", + "js-sys", + "num-integer", + "num-traits", + "serde", + "time 0.1.45", + "wasm-bindgen", + "winapi", +] + +[[package]] +name = "codespan-reporting" +version = "0.11.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3538270d33cc669650c4b093848450d380def10c331d38c768e34cac80576e6e" +dependencies = [ + "termcolor", + "unicode-width", +] + [[package]] name = "convert_case" version = "0.4.0" @@ -442,7 +477,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e859cd57d0710d9e06c381b550c06e76992472a8c6d527aecd2fc673dcc231fb" dependencies = [ "percent-encoding", - "time", + "time 0.3.20", "version_check", ] @@ -501,6 +536,50 @@ dependencies = [ "nom", ] +[[package]] +name = "cxx" +version = "1.0.94" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f61f1b6389c3fe1c316bf8a4dccc90a38208354b330925bce1f74a6c4756eb93" +dependencies = [ + "cc", + "cxxbridge-flags", + "cxxbridge-macro", + "link-cplusplus", +] + +[[package]] +name = "cxx-build" +version = "1.0.94" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "12cee708e8962df2aeb38f594aae5d827c022b6460ac71a7a3e2c3c2aae5a07b" +dependencies = [ + "cc", + "codespan-reporting", + "once_cell", + "proc-macro2", + "quote", + "scratch", + "syn 2.0.13", +] + +[[package]] +name = "cxxbridge-flags" +version = "1.0.94" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7944172ae7e4068c533afbb984114a56c46e9ccddda550499caa222902c7f7bb" + +[[package]] +name = "cxxbridge-macro" +version = "1.0.94" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2345488264226bf682893e25de0769f3360aac9957980ec49361b083ddaa5bc5" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.13", +] + [[package]] name = "darling" version = "0.14.4" @@ -596,6 +675,7 @@ dependencies = [ "actix-files", "actix-web", "cached", + "chrono", "glob", "markdown", "minify-html", @@ -734,7 +814,7 @@ checksum = "c85e1d9ab2eadba7e5040d4e09cbd6d072b76a557ad64e797c2cb9d4da21d7e4" dependencies = [ "cfg-if", "libc", - "wasi", + "wasi 0.11.0+wasi-snapshot-preview1", ] [[package]] @@ -875,6 +955,30 @@ dependencies = [ "tokio-native-tls", ] +[[package]] +name = "iana-time-zone" +version = "0.1.56" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0722cd7114b7de04316e7ea5456a0bbb20e4adb46fd27a3697adb812cff0f37c" +dependencies = [ + "android_system_properties", + "core-foundation-sys", + "iana-time-zone-haiku", + "js-sys", + "wasm-bindgen", + "windows", +] + +[[package]] +name = "iana-time-zone-haiku" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0703ae284fc167426161c2e3f1da3ea71d94b21bedbcc9494e92b28e334e3dca" +dependencies = [ + "cxx", + "cxx-build", +] + [[package]] name = "ident_case" version = "1.0.1" @@ -969,6 +1073,15 @@ version = "0.2.141" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3304a64d199bb964be99741b7a14d26972741915b3649639149b2479bb46f4b5" +[[package]] +name = "link-cplusplus" +version = "1.0.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ecd207c9c713c34f95a097a5b029ac2ce6010530c7b49d7fea24d977dede04f5" +dependencies = [ + "cc", +] + [[package]] name = "linux-raw-sys" version = "0.3.1" @@ -1113,7 +1226,7 @@ checksum = "5b9d9a46eff5b4ff64b45a9e316a6d1e0bc719ef429cbec4dc630684212bfdf9" dependencies = [ "libc", "log", - "wasi", + "wasi 0.11.0+wasi-snapshot-preview1", "windows-sys 0.45.0", ] @@ -1145,6 +1258,25 @@ dependencies = [ "minimal-lexical", ] +[[package]] +name = "num-integer" +version = "0.1.45" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "225d3389fb3509a24c93f5c29eb6bde2586b98d9f016636dff58d7c6f7569cd9" +dependencies = [ + "autocfg", + "num-traits", +] + +[[package]] +name = "num-traits" +version = "0.2.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "578ede34cf02f8924ab9447f50c28075b4d3e5b269972345e7e0372b38c6cdcd" +dependencies = [ + "autocfg", +] + [[package]] name = "num_cpus" version = "1.15.0" @@ -1507,6 +1639,12 @@ version = "1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d29ab0c6d3fc0ee92fe66e2d99f700eab17a8d57d1c1d3b748380fb20baa78cd" +[[package]] +name = "scratch" +version = "1.0.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1792db035ce95be60c3f8853017b3999209281c24e2ba5bc8e59bf97a0c590c1" + [[package]] name = "security-framework" version = "2.8.2" @@ -1687,6 +1825,15 @@ dependencies = [ "windows-sys 0.45.0", ] +[[package]] +name = "termcolor" +version = "1.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "be55cf8942feac5c765c2c993422806843c9a9a45d4d5c407ad6dd2ea95eb9b6" +dependencies = [ + "winapi-util", +] + [[package]] name = "thiserror" version = "1.0.40" @@ -1707,6 +1854,17 @@ dependencies = [ "syn 2.0.13", ] +[[package]] +name = "time" +version = "0.1.45" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1b797afad3f312d1c66a56d11d0316f916356d11bd158fbc6ca6389ff6bf805a" +dependencies = [ + "libc", + "wasi 0.10.0+wasi-snapshot-preview1", + "winapi", +] + [[package]] name = "time" version = "0.3.20" @@ -1917,6 +2075,12 @@ version = "1.10.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1dd624098567895118886609431a7c3b8f516e41d30e0643f03d94592a147e36" +[[package]] +name = "unicode-width" +version = "0.1.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c0edd1e5b14653f783770bce4a4dabb4a5108a5370a5f5d8cfe8710c361f6c8b" + [[package]] name = "unsafe-libyaml" version = "0.2.8" @@ -1956,6 +2120,12 @@ dependencies = [ "try-lock", ] +[[package]] +name = "wasi" +version = "0.10.0+wasi-snapshot-preview1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1a143597ca7c7793eff794def352d41792a93c481eb1042423ff7ff72ba2c31f" + [[package]] name = "wasi" version = "0.11.0+wasi-snapshot-preview1" @@ -2054,12 +2224,30 @@ version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" +[[package]] +name = "winapi-util" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "70ec6ce85bb158151cae5e5c87f95a8e97d2c0c4b001223f33a334e3ce5de178" +dependencies = [ + "winapi", +] + [[package]] name = "winapi-x86_64-pc-windows-gnu" version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" +[[package]] +name = "windows" +version = "0.48.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e686886bc078bc1b0b600cac0147aadb815089b6e4da64016cbd754b6342700f" +dependencies = [ + "windows-targets 0.48.0", +] + [[package]] name = "windows-sys" version = "0.42.0" diff --git a/Cargo.toml b/Cargo.toml index e9612eb..3a5d47d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -21,3 +21,4 @@ minify-html = "0.10.8" glob = "0.3.1" markdown = "1.0.0-alpha.7" reqwest = { version = "0.11", features = ["json"] } +chrono = { version = "0.4", features = ["serde"] } diff --git a/src/routes/blog.rs b/src/routes/blog.rs index 2c3f53f..c193fa6 100644 --- a/src/routes/blog.rs +++ b/src/routes/blog.rs @@ -4,7 +4,7 @@ use ramhorns::Content; use crate::{ config::Config, - template::{get_md_asm, get_md_metadata, read_md_file, File, FileMetadata, Infos}, + template::{get_md_asm, get_md_metadata, read_md_file, Date, File, FileMetadata, Infos}, }; #[get("/blog")] @@ -17,10 +17,10 @@ struct BlogIndexTemplate { posts: Option>, } -#[derive(Content, Debug)] +#[derive(Content)] struct Post { title: String, - date: String, + date: Date, url: String, } @@ -34,6 +34,7 @@ pub fn get_index(config: Config) -> String { .filter(|f| f.path().extension().unwrap() == "md") .collect::>(); + // Sort by latest modification/ date of creation entries.sort_by_cached_key(|f| { f.metadata() .unwrap() @@ -77,6 +78,8 @@ pub fn get_index(config: Config) -> String { }) .collect::>(); + // TODO sort vec by post date metadata, removing the sort above + config.tmpl.render( "blog/index.html", BlogIndexTemplate { diff --git a/src/template.rs b/src/template.rs index a7b4998..d62965c 100644 --- a/src/template.rs +++ b/src/template.rs @@ -1,6 +1,7 @@ +use chrono::{Datelike, NaiveDate}; use markdown::mdast::Node; use ramhorns::{Content, Ramhorns}; -use serde::Deserialize; +use serde::{Deserialize, Deserializer}; use std::fs; #[derive(Clone)] @@ -58,12 +59,37 @@ impl FrontMatter<'_> { } } } +#[derive(Content, Default, Debug)] +pub struct Date { + day: u32, + month: u32, + year: i32, +} -#[derive(Default, Deserialize, Content)] +impl<'de> Deserialize<'de> for Date { + fn deserialize(deserializer: D) -> Result + where + D: Deserializer<'de>, + { + match <&str>::deserialize(deserializer) { + Ok(s) => match NaiveDate::parse_from_str(s, "%d-%m-%Y") { + Ok(date) => Ok(Self { + day: date.day(), + month: date.month(), + year: date.year(), + }), + Err(e) => Err(serde::de::Error::custom(e)), + }, + Err(e) => Err(e), + } + } +} + +#[derive(Default, Deserialize, Content, Debug)] pub struct FileMetadata { pub title: Option, pub link: Option, - pub date: Option, + pub date: Option, } #[derive(Content)]