diff options
author | Qrius <[email protected]> | 2024-11-12 14:49:14 +0100 |
---|---|---|
committer | Qrius <[email protected]> | 2024-11-12 14:49:14 +0100 |
commit | 1a27e733f961893b5cb1a92b1b9927e31631c936 (patch) | |
tree | d016cbd1e6a6577642aafcfc13328cd621f8c28b | |
parent | 482290c40763796f530ebe0812bacf905af194fc (diff) | |
download | skaldpress-1a27e733f961893b5cb1a92b1b9927e31631c936.tar.gz skaldpress-1a27e733f961893b5cb1a92b1b9927e31631c936.zip |
Add explode function, add auto filename metadata
-rw-r--r-- | smp.1 | 3 | ||||
-rw-r--r-- | src/macro_processor/macro_processor.rs | 39 | ||||
-rw-r--r-- | src/skaldpress/main.rs | 10 |
3 files changed, 51 insertions, 1 deletions
@@ -50,6 +50,9 @@ Not the best way to do this, it is not sensibly recursive. .IP "\fBarray_size(<macro_name>)\fR" Will return number of elements in a macro array. +.IP "\fBexplode(<array_name>, <delimiter>, <input>)\fR" +Explode a input into individual array elements. + .IP "\fBformat_time(<format>, <time>)\fR" Format a RFC3339-timestamp to the specified format. Format is similar to strftime. Only available if the \fBtime\fR-feature was enabled during compilation. 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<String, SMPError> { + 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<CompiledFile, Skaldpres None => (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") { |