From 1a27e733f961893b5cb1a92b1b9927e31631c936 Mon Sep 17 00:00:00 2001 From: Qrius Date: Tue, 12 Nov 2024 14:49:14 +0100 Subject: Add explode function, add auto filename metadata --- src/macro_processor/macro_processor.rs | 39 +++++++++++++++++++++++++++++++++- src/skaldpress/main.rs | 10 +++++++++ 2 files changed, 48 insertions(+), 1 deletion(-) (limited to 'src') diff --git a/src/macro_processor/macro_processor.rs b/src/macro_processor/macro_processor.rs index a9585bb..0b714f0 100644 --- a/src/macro_processor/macro_processor.rs +++ b/src/macro_processor/macro_processor.rs @@ -397,7 +397,10 @@ fn smp_builtin_array_each( let mut out = String::new(); for el in array.clone() { let exp = match el { - MacroType::String(s) => smp.expand_macro(&args[1], &mut [s])?, + MacroType::String(s) => { + let n = smp.expand_macro(&args[1], &mut [s])?; + smp.process_input(&n)? + } MacroType::Array(a) => { let mut out = Vec::new(); for _el in a { @@ -440,6 +443,36 @@ fn smp_builtin_array_size( Ok(array.len().to_string()) } +/// Split input into array +fn smp_builtin_explode( + smp: &mut MacroProcessor, + macro_name: &str, + args: &mut [String], +) -> Result { + if args.len() != 3 { + smp.warnings + .push(MacroProcessorWarning::from_macro_invocation( + macro_name, + args, + format!("Wrong number of arguments, expected 3"), + )); + return Ok(macro_name.to_string()); + } + let split = smp.process_input(&args[1])?; + for c in smp.process_input(&args[2])?.split(&split) { + if let Err(e) = smp.array_push(&args[0], MacroType::String(String::from(c))) { + smp.warnings + .push(MacroProcessorWarning::from_macro_invocation( + macro_name, + args, + format!("Error executing array_push ({:?})", e), + )); + } + } + + Ok(String::new()) +} + #[cfg(feature = "time")] fn smp_builtin_format_time( smp: &mut MacroProcessor, @@ -664,6 +697,10 @@ impl MacroProcessor { String::from("array_size"), MacroType::Function(smp_builtin_array_size), ); + self.define_macro( + String::from("explode"), + MacroType::Function(smp_builtin_explode), + ); #[cfg(feature = "time")] self.define_macro( String::from("format_time"), diff --git a/src/skaldpress/main.rs b/src/skaldpress/main.rs index 909f139..25060f7 100644 --- a/src/skaldpress/main.rs +++ b/src/skaldpress/main.rs @@ -313,6 +313,16 @@ fn compile_file(file_path: &Path, opts: &Opts) -> Result (HashMap::new(), file_content.as_str()), }; map.extend(opts.metadata.clone()); + let filename = file_path + .strip_prefix(&opts.content_dir) + .map_err(|e| { + SkaldpressError::PathOperationError(SP_GEN_DEST_STRIP_PREFIX_ERROR, Some(Box::new(e))) + })? + .with_extension("") + .to_str() + .unwrap_or("") + .to_string(); + map.insert(String::from("filename"), YamlValue::Scalar(filename)); let mut skip_smp = false; if let Some(_skip_smp) = &map.get("skip_smp") { -- cgit v1.2.3