diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/skaldpress/main.rs | 41 |
1 files changed, 26 insertions, 15 deletions
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<String, YamlValue>, ) -> HashMap<String, MacroType> { - 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<CompiledFile, Skaldpres let mut macro_processor = MacroProcessor::new(); macro_processor_initialize(&map, &mut macro_processor, stored_smp_state); - //let file_content = macro_processor - // .process_input(&file_content) - // .map_err(|e| SkaldpressError::SMPError(SP_COMPILE_FILE_MACRO_PROCESS_ERROR, e))?; - //let file_content = do_content_conversion(&file_content, extension)?; - // This didn't work properly, - // Maybe we should instead make a macro which can convert from markdown, and wrap this in a - // macro like that if it is a markdown-file? - let file_content = match extension { "md" => format!(r#"html_from_markdown(%"{}"%)"#, file_content), content => content.to_string(), @@ -346,7 +357,7 @@ fn compile_file(file_path: &Path, opts: &Opts) -> Result<CompiledFile, Skaldpres print_warnings(¯o_processor); return Ok(CompiledFile { content: file_content, - stored_smp_state: extract_requested_macro_processor_state(&mut macro_processor, &map), + stored_smp_state: extract_requested_macro_processor_state(&mut macro_processor), metadata: map, extension: String::from(extension), source_path: String::from(file_path.to_str().unwrap_or("")), @@ -366,7 +377,7 @@ fn compile_file(file_path: &Path, opts: &Opts) -> Result<CompiledFile, Skaldpres print_warnings(¯o_processor); Ok(CompiledFile { content, - stored_smp_state: extract_requested_macro_processor_state(&mut macro_processor, &map), + stored_smp_state: extract_requested_macro_processor_state(&mut macro_processor), metadata: map, extension: String::from(template_extension), source_path: String::from(file_path.to_str().unwrap_or("")), |