summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorQrius <[email protected]>2024-09-26 00:11:05 +0200
committerQrius <[email protected]>2024-09-26 00:11:05 +0200
commit132286c7798c79a900e88e250a68d5da62fd8fb8 (patch)
tree9e233c2455028264e75aea99221a288eca6c7ab4
parent99205d6a5635dc8cb1a9d7429e6b6d0880518cb9 (diff)
downloadskaldpress-132286c7798c79a900e88e250a68d5da62fd8fb8.tar.gz
skaldpress-132286c7798c79a900e88e250a68d5da62fd8fb8.zip
Move metadata_parser into it's own file
-rw-r--r--src/macro_processor/macro_processor.rs1
-rw-r--r--src/skaldpress/main.rs72
-rw-r--r--src/skaldpress/metadata_parser.rs71
-rw-r--r--src/skaldpress/mod.rs2
4 files changed, 73 insertions, 73 deletions
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<String>),
-}
-
-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<String>"),
- }
- }
-}
-
-/// Currently, all files MUST have a metadata block, or this will fail completely
-fn extract_parse_yaml_metadata<'a>(markdown: &'a str) -> (HashMap<String, YamlValue>, &'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<String> = None;
- let mut current_list: Vec<String> = 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<String, Box<dyn std::error::Error>> {
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<String>),
+}
+
+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<String>"),
+ }
+ }
+}
+
+/// Currently, all files MUST have a metadata block, or this will fail completely
+pub fn extract_parse_yaml_metadata<'a>(markdown: &'a str) -> (HashMap<String, YamlValue>, &'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<String> = None;
+ let mut current_list: Vec<String> = 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;