feat: frontmatter manipulation command

This commit is contained in:
Gustavo "Guz" L. de Mello
2024-03-27 19:50:03 -03:00
parent acea73cb40
commit 61601670c3
3 changed files with 156 additions and 4 deletions

82
src/frontmatter.rs Normal file
View File

@@ -0,0 +1,82 @@
use serde_yaml as yaml;
use std::collections::HashMap;
#[derive(Debug)]
pub struct Frontmatter<T> {
map: HashMap<String, T>,
}
impl<T> Frontmatter<T>
where
for<'a> T: serde::de::Deserialize<'a> + serde::Serialize,
{
pub fn new(f: &mut String) -> Result<Frontmatter<T>, yaml::Error> {
let f = f.split("---").collect::<Vec<&str>>()[1];
let m = match yaml::from_str::<HashMap<String, T>>(&f) {
Ok(m) => m,
Err(e) => return Err(e),
};
Ok(Frontmatter { map: m })
}
pub fn get(&self, key: &str) -> Option<&T> {
self.map.get(key)
}
pub fn set(&mut self, key: &str, value: T) {
self.map.insert(String::from(key), value);
}
pub fn to_string(&self) -> Result<String, yaml::Error> {
Ok(format!("---\n{}---\n\n", yaml::to_string(&self.map)?))
}
pub fn to_map(&self) -> &HashMap<String, T> {
&self.map
}
}
pub fn to_yaml_value<T>(value: Vec<T>, json_to_string: bool) -> yaml::Value
where
T: ToString + std::fmt::Display,
{
if value.len() >= 2 {
yaml::Value::Sequence(
value
.iter()
// This causes a recursion limit, which I'm not caring on fixing
// for now knowing the scope of this project as a hole.
// .map(|v| to_yaml_value(vec![v; 1], json_to_string))
.map(|v| yaml::Value::String(v.to_string()))
.collect::<Vec<yaml::Value>>(),
);
}
let value = &value[0].to_string();
match value.to_lowercase().as_str() {
"null" | "~" => return yaml::Value::Null,
"true" | "yes" => return yaml::Value::Bool(true),
"false" | "no" => return yaml::Value::Bool(false),
_ => (),
}
if let Ok(v) = value.parse::<u64>() {
return yaml::Value::Number(v.into());
}
if let Ok(v) = value.parse::<i64>() {
return yaml::Value::Number(v.into());
}
if let Ok(v) = value.parse::<f64>() {
return yaml::Value::Number(v.into());
}
match yaml::from_str::<serde_yaml::Value>(value) {
Ok(v) => {
if json_to_string {
return yaml::Value::String(String::from(value));
} else {
return v;
}
}
Err(_) => (),
}
yaml::Value::String(String::from(value))
}

View File

@@ -1,2 +1,3 @@
pub mod frontmatter;
pub mod links;
pub mod utils;

View File

@@ -1,8 +1,13 @@
use clap::{Parser, Subcommand};
use core::panic;
use clap::{ArgAction, Parser, Subcommand};
use clio::*;
use comrak::nodes::NodeValue;
use mdparser::{links, utils};
use mdparser::{
frontmatter::{self, Frontmatter},
links, utils,
};
#[derive(Parser, Debug)]
#[command(version = "0.1", about = "", long_about = None, propagate_version = true)]
@@ -38,9 +43,37 @@ enum Commands {
#[arg(long)]
not_remove_unfound: bool,
},
Frontmatter {
#[command(subcommand)]
command: FrontmatterCommands,
},
Not {},
}
#[derive(Debug, Subcommand)]
enum FrontmatterCommands {
Set {
#[clap()]
property: String,
#[clap(num_args(1..))]
value: Vec<String>,
#[arg(short, long, action = ArgAction::SetTrue)]
json: bool,
},
Get {
#[clap()]
property: String,
#[arg(short, long, action = ArgAction::SetTrue)]
error_on_unfound: bool,
#[arg(short, long, action = ArgAction::SetTrue)]
stderr_on_unfound: bool,
},
}
fn main() {
let mut cli = Cli::parse();
@@ -48,8 +81,6 @@ fn main() {
let arena = comrak::Arena::new();
let ast = comrak::parse_document(&arena, &file, &mdparser::utils::default_options());
// println!("{ast:#?}");
match &cli.command {
Commands::Links {
path_root,
@@ -81,6 +112,44 @@ fn main() {
};
}
}),
Commands::Frontmatter { command } => utils::iter_nodes(&ast, &|node| {
if let NodeValue::FrontMatter(ref mut f) = &mut node.data.borrow_mut().value {
let mut frontmatter: Frontmatter<serde_yaml::Value> = match Frontmatter::new(f) {
Ok(f) => f,
Err(e) => panic!("{e:#?}"),
};
match command {
FrontmatterCommands::Set {
property,
value,
json,
} => {
frontmatter.set(
&property,
frontmatter::to_yaml_value(value.to_vec(), !*json),
);
}
FrontmatterCommands::Get {
property,
error_on_unfound,
stderr_on_unfound,
} => {
let v = frontmatter.get(property);
if let Some(v) = v {
print!("{v:#?}")
} else if *error_on_unfound {
panic!()
} else if *stderr_on_unfound {
eprint!("Not Found")
}
}
};
*f = match frontmatter.to_string() {
Ok(s) => s,
Err(e) => panic!("{e:#?}"),
};
}
}),
_ => (),
};