summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorQrius <[email protected]>2024-09-26 00:11:05 +0200
committerQrius <[email protected]>2024-09-26 00:11:05 +0200
commitbaaa4f147e36f8f667557ee1c64c7240da624001 (patch)
treea9729129c86f11b47169b576dcd9367c417972d5
parent0db0021655f45ce9b1a8dc64e5ca3e1cfd7535e1 (diff)
downloadskaldpress-baaa4f147e36f8f667557ee1c64c7240da624001.tar.gz
skaldpress-baaa4f147e36f8f667557ee1c64c7240da624001.zip
Add sorting function to YamlValue, make it possible to sort in special macros, add special parsing for dates and numbers
-rw-r--r--Cargo.lock291
-rw-r--r--Cargo.toml7
-rw-r--r--src/skaldpress/main.rs34
-rw-r--r--src/skaldpress/metadata_parser.rs56
4 files changed, 381 insertions, 7 deletions
diff --git a/Cargo.lock b/Cargo.lock
index 7d54bd4..9a8bb87 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -3,6 +3,112 @@
version = 3
[[package]]
+name = "android-tzdata"
+version = "0.1.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "e999941b234f3131b00bc13c22d06e8c5ff726d1b6318ac7eb276997bbb4fef0"
+
+[[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 = "autocfg"
+version = "1.3.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "0c4b4d0bd25bd0b74681c0ad21497610ce1b7c91b1022cd21c80c6fbdd9476b0"
+
+[[package]]
+name = "bumpalo"
+version = "3.16.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "79296716171880943b8470b5f8d03aa55eb2e645a4874bdbb28adb49162e012c"
+
+[[package]]
+name = "cc"
+version = "1.1.18"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "b62ac837cdb5cb22e10a256099b4fc502b1dfe560cb282963a974d7abd80e476"
+dependencies = [
+ "shlex",
+]
+
+[[package]]
+name = "cfg-if"
+version = "1.0.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
+
+[[package]]
+name = "chrono"
+version = "0.4.38"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "a21f936df1771bf62b77f047b726c4625ff2e8aa607c01ec06e5a05bd8463401"
+dependencies = [
+ "android-tzdata",
+ "iana-time-zone",
+ "js-sys",
+ "num-traits",
+ "wasm-bindgen",
+ "windows-targets",
+]
+
+[[package]]
+name = "core-foundation-sys"
+version = "0.8.7"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "773648b94d0e5d620f64f280777445740e61fe701025087ec8b57f45c791888b"
+
+[[package]]
+name = "iana-time-zone"
+version = "0.1.60"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "e7ffbb5a1b541ea2561f8c41c087286cc091e21e556a4f09a8f6cbf17b69b141"
+dependencies = [
+ "android_system_properties",
+ "core-foundation-sys",
+ "iana-time-zone-haiku",
+ "js-sys",
+ "wasm-bindgen",
+ "windows-core",
+]
+
+[[package]]
+name = "iana-time-zone-haiku"
+version = "0.1.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "f31827a206f56af32e590ba56d5d2d085f558508192593743f16b2306495269f"
+dependencies = [
+ "cc",
+]
+
+[[package]]
+name = "js-sys"
+version = "0.3.70"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "1868808506b929d7b0cfa8f75951347aa71bb21144b7791bae35d9bccfcfe37a"
+dependencies = [
+ "wasm-bindgen",
+]
+
+[[package]]
+name = "libc"
+version = "0.2.158"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "d8adc4bb1803a324070e64a98ae98f38934d91957a99cfb3a43dcbc01bc56439"
+
+[[package]]
+name = "log"
+version = "0.4.22"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "a7a70ba024b9dc04c27ea2f0c0548feb474ec5c54bba33a7f72f873a39d07b24"
+
+[[package]]
name = "markdown"
version = "1.0.0-alpha.20"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -12,14 +118,199 @@ dependencies = [
]
[[package]]
+name = "num-traits"
+version = "0.2.19"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "071dfc062690e90b734c0b2273ce72ad0ffa95f0c74596bc250dcfd960262841"
+dependencies = [
+ "autocfg",
+]
+
+[[package]]
+name = "once_cell"
+version = "1.19.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "3fdb12b2476b595f9358c5161aa467c2438859caa136dec86c26fdd2efe17b92"
+
+[[package]]
+name = "proc-macro2"
+version = "1.0.86"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "5e719e8df665df0d1c8fbfd238015744736151d4445ec0836b8e628aae103b77"
+dependencies = [
+ "unicode-ident",
+]
+
+[[package]]
+name = "quote"
+version = "1.0.37"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "b5b9d34b8991d19d98081b46eacdd8eb58c6f2b201139f7c5f643cc155a633af"
+dependencies = [
+ "proc-macro2",
+]
+
+[[package]]
+name = "shlex"
+version = "1.3.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "0fda2ff0d084019ba4d7c6f371c95d8fd75ce3524c3cb8fb653a3023f6323e64"
+
+[[package]]
name = "skaldpress"
version = "0.1.0"
dependencies = [
+ "chrono",
"markdown",
]
[[package]]
+name = "syn"
+version = "2.0.77"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "9f35bcdf61fd8e7be6caf75f429fdca8beb3ed76584befb503b1569faee373ed"
+dependencies = [
+ "proc-macro2",
+ "quote",
+ "unicode-ident",
+]
+
+[[package]]
name = "unicode-id"
version = "0.3.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b1b6def86329695390197b82c1e244a54a131ceb66c996f2088a3876e2ae083f"
+
+[[package]]
+name = "unicode-ident"
+version = "1.0.13"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "e91b56cd4cadaeb79bbf1a5645f6b4f8dc5bde8834ad5894a8db35fda9efa1fe"
+
+[[package]]
+name = "wasm-bindgen"
+version = "0.2.93"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "a82edfc16a6c469f5f44dc7b571814045d60404b55a0ee849f9bcfa2e63dd9b5"
+dependencies = [
+ "cfg-if",
+ "once_cell",
+ "wasm-bindgen-macro",
+]
+
+[[package]]
+name = "wasm-bindgen-backend"
+version = "0.2.93"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "9de396da306523044d3302746f1208fa71d7532227f15e347e2d93e4145dd77b"
+dependencies = [
+ "bumpalo",
+ "log",
+ "once_cell",
+ "proc-macro2",
+ "quote",
+ "syn",
+ "wasm-bindgen-shared",
+]
+
+[[package]]
+name = "wasm-bindgen-macro"
+version = "0.2.93"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "585c4c91a46b072c92e908d99cb1dcdf95c5218eeb6f3bf1efa991ee7a68cccf"
+dependencies = [
+ "quote",
+ "wasm-bindgen-macro-support",
+]
+
+[[package]]
+name = "wasm-bindgen-macro-support"
+version = "0.2.93"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "afc340c74d9005395cf9dd098506f7f44e38f2b4a21c6aaacf9a105ea5e1e836"
+dependencies = [
+ "proc-macro2",
+ "quote",
+ "syn",
+ "wasm-bindgen-backend",
+ "wasm-bindgen-shared",
+]
+
+[[package]]
+name = "wasm-bindgen-shared"
+version = "0.2.93"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "c62a0a307cb4a311d3a07867860911ca130c3494e8c2719593806c08bc5d0484"
+
+[[package]]
+name = "windows-core"
+version = "0.52.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "33ab640c8d7e35bf8ba19b884ba838ceb4fba93a4e8c65a9059d08afcfc683d9"
+dependencies = [
+ "windows-targets",
+]
+
+[[package]]
+name = "windows-targets"
+version = "0.52.6"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "9b724f72796e036ab90c1021d4780d4d3d648aca59e491e6b98e725b84e99973"
+dependencies = [
+ "windows_aarch64_gnullvm",
+ "windows_aarch64_msvc",
+ "windows_i686_gnu",
+ "windows_i686_gnullvm",
+ "windows_i686_msvc",
+ "windows_x86_64_gnu",
+ "windows_x86_64_gnullvm",
+ "windows_x86_64_msvc",
+]
+
+[[package]]
+name = "windows_aarch64_gnullvm"
+version = "0.52.6"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "32a4622180e7a0ec044bb555404c800bc9fd9ec262ec147edd5989ccd0c02cd3"
+
+[[package]]
+name = "windows_aarch64_msvc"
+version = "0.52.6"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "09ec2a7bb152e2252b53fa7803150007879548bc709c039df7627cabbd05d469"
+
+[[package]]
+name = "windows_i686_gnu"
+version = "0.52.6"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "8e9b5ad5ab802e97eb8e295ac6720e509ee4c243f69d781394014ebfe8bbfa0b"
+
+[[package]]
+name = "windows_i686_gnullvm"
+version = "0.52.6"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "0eee52d38c090b3caa76c563b86c3a4bd71ef1a819287c19d586d7334ae8ed66"
+
+[[package]]
+name = "windows_i686_msvc"
+version = "0.52.6"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "240948bc05c5e7c6dabba28bf89d89ffce3e303022809e73deaefe4f6ec56c66"
+
+[[package]]
+name = "windows_x86_64_gnu"
+version = "0.52.6"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "147a5c80aabfbf0c7d901cb5895d1de30ef2907eb21fbbab29ca94c5b08b1a78"
+
+[[package]]
+name = "windows_x86_64_gnullvm"
+version = "0.52.6"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "24d5b23dc417412679681396f2b49f3de8c1473deb516bd34410872eff51ed0d"
+
+[[package]]
+name = "windows_x86_64_msvc"
+version = "0.52.6"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "589f6da84c646204747d1270a2a5661ea66ed1cced2631d546fdfb155959f9ec"
diff --git a/Cargo.toml b/Cargo.toml
index ebaf72c..4334410 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -4,8 +4,13 @@ version = "0.1.0"
edition = "2021"
default-run = "skaldpress"
+[features]
+default = ["time"]
+time = ["dep:chrono"]
+
[dependencies]
-markdown = "1.0.0-alpha.20"
+markdown = { version = "1.0.0-alpha.20" }
+chrono = { version = "0.4.38", optional = true }
[[bin]]
name = "skaldpress"
diff --git a/src/skaldpress/main.rs b/src/skaldpress/main.rs
index 84a4285..45d0a78 100644
--- a/src/skaldpress/main.rs
+++ b/src/skaldpress/main.rs
@@ -33,6 +33,13 @@ struct CompiledFile {
source_path: String,
}
+/// SMP Macro for processing input with a template
+///
+/// This will keep state of the macro_processor from the document which invokes the macro.
+/// No additional metadata will be added.
+///
+/// Usage in files:
+/// template(<template>, <content>)
fn sp_template(
smp: &mut MacroProcessor,
macro_name: &str,
@@ -50,6 +57,10 @@ fn sp_template(
smp.process_input(&template)
}
+/// SMP Macro for getting all files with specific tag, this is only _really_ effective the second run
+///
+/// Usage in files:
+/// all_tagged_by(<tag name>, <template> [, <field to sort by>] [, reversed])
fn sp_all_tagged_by(
smp: &mut MacroProcessor,
macro_name: &str,
@@ -72,8 +83,29 @@ fn sp_all_tagged_by(
let mut out = String::new();
+ let mut tagged_files = tagged_files.clone();
+ if args.len() > 2 {
+ if args.len() > 3 && args[3] == "reversed" {
+ tagged_files.sort_by(|a, b| {
+ *(&compiled_files[*b]
+ .metadata
+ .get(&args[2])
+ .unwrap()
+ .cmp(&compiled_files[*a].metadata.get(&args[2]).unwrap()))
+ });
+ } else {
+ tagged_files.sort_by(|a, b| {
+ *(&compiled_files[*a]
+ .metadata
+ .get(&args[2])
+ .unwrap()
+ .cmp(&compiled_files[*b].metadata.get(&args[2]).unwrap()))
+ });
+ }
+ }
+
for doc_i in tagged_files {
- let file = &compiled_files[*doc_i];
+ let file = &compiled_files[doc_i];
let mut smp_local = macro_processor(&file.metadata, Some(smp));
out.push_str(&sp_template(
&mut smp_local,
diff --git a/src/skaldpress/metadata_parser.rs b/src/skaldpress/metadata_parser.rs
index 4d6e352..a2abcc2 100644
--- a/src/skaldpress/metadata_parser.rs
+++ b/src/skaldpress/metadata_parser.rs
@@ -1,9 +1,14 @@
+#[cfg(feature = "time")]
+use chrono::DateTime;
+use std::cmp::Ordering;
use std::collections::HashMap;
use std::fmt;
-#[derive(Debug)]
+#[derive(Eq, PartialEq, Debug)]
pub enum YamlValue {
Scalar(String),
+ Integer(i64),
+ Date(String, i64),
List(Vec<String>),
}
@@ -11,6 +16,8 @@ impl fmt::Display for YamlValue {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
YamlValue::Scalar(x) => write!(f, "{}", x),
+ YamlValue::Integer(x) => write!(f, "{}", x),
+ YamlValue::Date(x, _) => write!(f, "{}", x),
YamlValue::List(_l) => write!(f, "List<String>"),
}
}
@@ -22,11 +29,53 @@ impl TryFrom<&YamlValue> for String {
fn try_from(s: &YamlValue) -> Result<String, String> {
match s {
YamlValue::Scalar(s) => Ok(String::from(s)),
+ YamlValue::Integer(s) => Ok(s.to_string()),
+ YamlValue::Date(s, _) => Ok(s.to_string()),
YamlValue::List(_) => Err(String::from("List value cannot be turned into string")),
}
}
}
+impl PartialOrd for YamlValue {
+ fn partial_cmp(&self, other: &YamlValue) -> Option<Ordering> {
+ Some(self.cmp(other))
+ }
+}
+
+impl Ord for YamlValue {
+ fn cmp(&self, other: &YamlValue) -> Ordering {
+ if let YamlValue::Integer(a_int) = self {
+ if let YamlValue::Integer(b_int) = other {
+ return a_int.cmp(b_int);
+ }
+ }
+ if let YamlValue::Scalar(a_str) = self {
+ if let YamlValue::Scalar(b_str) = other {
+ return a_str.cmp(b_str);
+ }
+ }
+ if let YamlValue::Date(_, a_timestamp) = self {
+ if let YamlValue::Date(_, b_timestamp) = other {
+ return a_timestamp.cmp(b_timestamp);
+ }
+ }
+ self.to_string().cmp(&other.to_string())
+ }
+}
+
+fn str_to_yaml_value(in_str: &str) -> YamlValue {
+ let in_str = in_str.trim();
+ if let Ok(int) = in_str.parse::<i64>() {
+ return YamlValue::Integer(int);
+ }
+ #[cfg(feature = "time")]
+ if let Ok(dt) = DateTime::parse_from_rfc3339(in_str) {
+ return YamlValue::Date(in_str.to_string(), dt.timestamp());
+ }
+
+ YamlValue::Scalar(in_str.to_string())
+}
+
/// Extract a beginning YAML block of a input-string into a HashMap
pub fn extract_parse_yaml_metadata<'a>(
markdown: &'a str,
@@ -64,10 +113,7 @@ pub fn extract_parse_yaml_metadata<'a>(
}
current_key = Some(key.trim().to_string());
if !value.trim().is_empty() {
- yaml_map.insert(
- current_key.clone().unwrap(),
- YamlValue::Scalar(value.trim().to_string()),
- );
+ yaml_map.insert(current_key.clone().unwrap(), str_to_yaml_value(value));
current_key = None;
}
}