From 65b35f11e7242b08e375c80df5908d4275f3fe74 Mon Sep 17 00:00:00 2001 From: Qrius Date: Thu, 26 Sep 2024 00:11:05 +0200 Subject: Read metadata better, also, use METADATA_keep_states for storing states between runs --- src/skaldpress/main.rs | 41 ++++++++++++++++++++++++++--------------- 1 file changed, 26 insertions(+), 15 deletions(-) (limited to 'src') diff --git a/src/skaldpress/main.rs b/src/skaldpress/main.rs index 284ad88..57f3106 100644 --- a/src/skaldpress/main.rs +++ b/src/skaldpress/main.rs @@ -167,7 +167,20 @@ fn macro_processor_initialize( ); macro_processor.define_macro(String::from("template"), MacroType::Function(sp_template)); for (key, value) in metadata { - macro_processor.define_macro_string(format!("METADATA_{}", key), value.to_string()); + let macro_name = format!("METADATA_{}", key); + if !macro_processor.macros.contains_key(¯o_name) { + let value = match value { + YamlValue::List(l) => { + let mut out = Vec::new(); + for el in l { + out.push(MacroType::String(el.to_string())); + } + MacroType::Array(out) + } + value => MacroType::String(value.to_string()), + }; + macro_processor.define_macro(macro_name, value); + } } if let Some(additional_state) = additional_state { for (key, value) in additional_state { @@ -209,6 +222,7 @@ fn wrap_template( None => (HashMap::new(), template.as_str()), }; + macro_processor_initialize(&template_metadata, macro_processor, None); macro_processor.define_macro_string(String::from("CONTENT"), file_content.to_string()); let content = macro_processor .process_input(&template_content) @@ -252,12 +266,17 @@ fn print_warnings(macro_processor: &MacroProcessor) { fn extract_requested_macro_processor_state( macro_processor: &mut MacroProcessor, - metadata: &HashMap, ) -> HashMap { - if let Some(requested_keys) = metadata.get("keep_states") { + if let Some(requested_keys) = macro_processor.macros.get("METADATA_keep_states") { let requested_keys = match requested_keys { - YamlValue::List(l) => l.clone(), - YamlValue::Scalar(s) => vec![s.to_string()], + MacroType::Array(l) => { + let mut out = Vec::new(); + for el in l { + out.push(el.to_string()); + } + out + } + MacroType::String(s) => vec![s.to_string()], _ => { macro_processor .warnings @@ -326,14 +345,6 @@ fn compile_file(file_path: &Path, opts: &Opts) -> Result format!(r#"html_from_markdown(%"{}"%)"#, file_content), content => content.to_string(), @@ -346,7 +357,7 @@ fn compile_file(file_path: &Path, opts: &Opts) -> Result Result