From 132286c7798c79a900e88e250a68d5da62fd8fb8 Mon Sep 17 00:00:00 2001 From: Qrius Date: Thu, 26 Sep 2024 00:11:05 +0200 Subject: Move metadata_parser into it's own file --- src/macro_processor/macro_processor.rs | 1 - src/skaldpress/main.rs | 72 +--------------------------------- src/skaldpress/metadata_parser.rs | 71 +++++++++++++++++++++++++++++++++ src/skaldpress/mod.rs | 2 +- 4 files changed, 73 insertions(+), 73 deletions(-) create mode 100644 src/skaldpress/metadata_parser.rs (limited to 'src') diff --git a/src/macro_processor/macro_processor.rs b/src/macro_processor/macro_processor.rs index 4a46a62..0f8b217 100644 --- a/src/macro_processor/macro_processor.rs +++ b/src/macro_processor/macro_processor.rs @@ -181,7 +181,6 @@ pub struct MacroProcessor { } impl MacroProcessor { - pub fn new() -> Self { let mut smp = Self { macros: HashMap::new(), diff --git a/src/skaldpress/main.rs b/src/skaldpress/main.rs index 56abf70..5be6e13 100644 --- a/src/skaldpress/main.rs +++ b/src/skaldpress/main.rs @@ -1,83 +1,13 @@ -use std::fmt; use std::fs; use std::path::Path; use skaldpress::macro_processor::MacroProcessor; -use std::collections::HashMap; +use skaldpress::skaldpress::metadata_parser::extract_parse_yaml_metadata; const TEMPLATES_DIR: &str = "templates/"; const CONTENT_DIR: &str = "content/"; const BUILD_DIR: &str = "build/"; -#[derive(Debug)] -enum YamlValue { - Scalar(String), - List(Vec), -} - -impl fmt::Display for YamlValue { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - match self { - YamlValue::Scalar(x) => write!(f, "{}", x), - YamlValue::List(_l) => write!(f, "List"), - } - } -} - -/// Currently, all files MUST have a metadata block, or this will fail completely -fn extract_parse_yaml_metadata<'a>(markdown: &'a str) -> (HashMap, &'a str) { - let mut yaml_map = HashMap::new(); - let lines = markdown.lines(); - let mut yaml_started = false; - let mut yaml_ended = false; - let mut end_index = 0; - let mut current_key: Option = None; - let mut current_list: Vec = Vec::new(); - - for (i, line) in lines.enumerate() { - if line.trim() == "---" { - if yaml_started { - yaml_ended = true; - end_index = markdown.lines().take(i + 1).map(|l| l.len() + 1).sum(); - break; - } else { - yaml_started = true; - } - } else if yaml_started && !yaml_ended { - if line.trim().starts_with('-') && current_key.is_some() { - current_list.push(line.trim().trim_start_matches('-').trim().to_string()); - } else if let Some((key, value)) = line.split_once(':') { - if let Some(key) = current_key.take() { - if !current_list.is_empty() { - yaml_map.insert(key, YamlValue::List(current_list.clone())); - current_list.clear(); - } - } - 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()), - ); - current_key = None; - } - } - } - } - - if let Some(key) = current_key.take() { - if !current_list.is_empty() { - yaml_map.insert(key, YamlValue::List(current_list)); - } - } - - if !yaml_ended { - end_index = markdown.len(); - } - - (yaml_map, &markdown[end_index..]) -} - fn compile_file(file_path: &Path) -> Result> { let extension = file_path.extension().expect("SP14"); diff --git a/src/skaldpress/metadata_parser.rs b/src/skaldpress/metadata_parser.rs new file mode 100644 index 0000000..7a91b74 --- /dev/null +++ b/src/skaldpress/metadata_parser.rs @@ -0,0 +1,71 @@ +use std::collections::HashMap; +use std::fmt; + +#[derive(Debug)] +pub enum YamlValue { + Scalar(String), + List(Vec), +} + +impl fmt::Display for YamlValue { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + match self { + YamlValue::Scalar(x) => write!(f, "{}", x), + YamlValue::List(_l) => write!(f, "List"), + } + } +} + +/// Currently, all files MUST have a metadata block, or this will fail completely +pub fn extract_parse_yaml_metadata<'a>(markdown: &'a str) -> (HashMap, &'a str) { + let mut yaml_map = HashMap::new(); + let lines = markdown.lines(); + let mut yaml_started = false; + let mut yaml_ended = false; + let mut end_index = 0; + let mut current_key: Option = None; + let mut current_list: Vec = Vec::new(); + + for (i, line) in lines.enumerate() { + if line.trim() == "---" { + if yaml_started { + yaml_ended = true; + end_index = markdown.lines().take(i + 1).map(|l| l.len() + 1).sum(); + break; + } else { + yaml_started = true; + } + } else if yaml_started && !yaml_ended { + if line.trim().starts_with('-') && current_key.is_some() { + current_list.push(line.trim().trim_start_matches('-').trim().to_string()); + } else if let Some((key, value)) = line.split_once(':') { + if let Some(key) = current_key.take() { + if !current_list.is_empty() { + yaml_map.insert(key, YamlValue::List(current_list.clone())); + current_list.clear(); + } + } + 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()), + ); + current_key = None; + } + } + } + } + + if let Some(key) = current_key.take() { + if !current_list.is_empty() { + yaml_map.insert(key, YamlValue::List(current_list)); + } + } + + if !yaml_ended { + end_index = markdown.len(); + } + + (yaml_map, &markdown[end_index..]) +} diff --git a/src/skaldpress/mod.rs b/src/skaldpress/mod.rs index 8b13789..1a14898 100644 --- a/src/skaldpress/mod.rs +++ b/src/skaldpress/mod.rs @@ -1 +1 @@ - +pub mod metadata_parser; -- cgit v1.2.3