From 1da85c07f53a311a0f83625ee9939119eb18e800 Mon Sep 17 00:00:00 2001 From: Araozu Date: Mon, 26 Jun 2023 21:50:07 -0500 Subject: [PATCH] Use classes for headings from config file --- README.md | 2 +- src/config/mod.rs | 53 +++++++++++++++++++++++++++++++++++++++ src/generator/emphasis.rs | 2 +- src/generator/heading.rs | 15 ++++++++--- src/generator/list.rs | 2 +- src/generator/strong.rs | 2 +- src/generator/text.rs | 2 +- 7 files changed, 70 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 8968794..874c4ff 100644 --- a/README.md +++ b/README.md @@ -11,7 +11,7 @@ Values: String input # Path to the input folder String output # Path to the output folder - ?content: { + headings: { String? h1 # Classes to add to h1 elements String? h2 # Classes to add to h2 elements String? h3 # Classes to add to h3 elements diff --git a/src/config/mod.rs b/src/config/mod.rs index b01858e..13b3ede 100644 --- a/src/config/mod.rs +++ b/src/config/mod.rs @@ -11,6 +11,15 @@ pub struct Config { pub input: String, pub output: String, pub template: String, + pub headings: Headings, +} + +pub struct Headings { + pub h1: Option, + pub h2: Option, + pub h3: Option, + pub h4: Option, + pub h5: Option, } /// Creates a `YAML::String` from a `&str` @@ -116,9 +125,53 @@ pub fn parse(yaml_str: &String) -> Result { )); } + // Heading config + let headings_yaml = parse_headings(config_yaml.get(ystr!("headings"))); + Ok(Config { input: input_folder.clone(), output: output_folder.clone(), template: template_file.clone(), + headings: headings_yaml, }) } + +fn parse_headings(y: Option<&Yaml>) -> Headings { + let Some(Yaml::Hash(map)) = y + else { + return Headings { + h1: None, + h2: None, + h3: None, + h4: None, + h5: None, + } + }; + + let h1 = match map.get(ystr!("h1")) { + Some(Yaml::String(s)) => Some(s.clone()), + _ => None, + }; + + let h2 = match map.get(ystr!("h2")) { + Some(Yaml::String(s)) => Some(s.clone()), + _ => None, + }; + + let h3 = match map.get(ystr!("h3")) { + Some(Yaml::String(s)) => Some(s.clone()), + _ => None, + }; + + let h4 = match map.get(ystr!("h4")) { + Some(Yaml::String(s)) => Some(s.clone()), + _ => None, + }; + + let h5 = match map.get(ystr!("h5")) { + Some(Yaml::String(s)) => Some(s.clone()), + _ => None, + }; + + Headings { h1, h2, h3, h4, h5 } +} diff --git a/src/generator/emphasis.rs b/src/generator/emphasis.rs index 0b76af5..be5a487 100644 --- a/src/generator/emphasis.rs +++ b/src/generator/emphasis.rs @@ -1,6 +1,6 @@ use markdown::mdast::Emphasis; -use crate::{utils, config::Config}; +use crate::{config::Config, utils}; use super::Printable; diff --git a/src/generator/heading.rs b/src/generator/heading.rs index f5eaadd..022bc2e 100644 --- a/src/generator/heading.rs +++ b/src/generator/heading.rs @@ -1,6 +1,6 @@ use markdown::mdast::Heading; -use crate::{utils, config::Config}; +use crate::{config::Config, utils}; use super::Printable; @@ -14,12 +14,21 @@ impl Printable for Heading { let text: String = result.into_iter().collect(); + let extra_classes = match self.depth { + 1 => config.headings.h1.clone().unwrap_or(format!("")), + 2 => config.headings.h2.clone().unwrap_or(format!("")), + 3 => config.headings.h3.clone().unwrap_or(format!("")), + 4 => config.headings.h4.clone().unwrap_or(format!("")), + 5 => config.headings.h5.clone().unwrap_or(format!("")), + _ => String::from(""), + }; + if self.depth < 4 { let html_fragment_text = utils::to_html_fragment(&self.get_text()); format!( - "{}", - self.depth, html_fragment_text, html_fragment_text, text, self.depth + "{}", + self.depth, extra_classes, html_fragment_text, html_fragment_text, text, self.depth ) } else { format!("{}", self.depth, text, self.depth) diff --git a/src/generator/list.rs b/src/generator/list.rs index eec25c0..48fcf10 100644 --- a/src/generator/list.rs +++ b/src/generator/list.rs @@ -1,6 +1,6 @@ use markdown::mdast::{List, ListItem, Node}; -use crate::{utils, config::Config}; +use crate::{config::Config, utils}; use super::Printable; diff --git a/src/generator/strong.rs b/src/generator/strong.rs index 84f44b0..ea6805c 100644 --- a/src/generator/strong.rs +++ b/src/generator/strong.rs @@ -1,6 +1,6 @@ use markdown::mdast::Strong; -use crate::{utils, config::Config}; +use crate::{config::Config, utils}; use super::Printable; diff --git a/src/generator/text.rs b/src/generator/text.rs index d2b32cf..cdf4682 100644 --- a/src/generator/text.rs +++ b/src/generator/text.rs @@ -1,6 +1,6 @@ use markdown::mdast::Text; -use crate::config::{Config}; +use crate::config::Config; use super::Printable;