From ce34180296441fd4a58a442cefa146006f7b9ad8 Mon Sep 17 00:00:00 2001 From: Araozu Date: Sat, 9 Sep 2023 10:55:28 -0500 Subject: [PATCH] Generate images & links --- README.md | 2 ++ src/config/mod.rs | 9 +++++++++ src/generator/image.rs | 15 +++++++++++++++ src/generator/link.rs | 24 ++++++++++++++++++++++++ src/generator/mod.rs | 4 ++++ 5 files changed, 54 insertions(+) create mode 100644 src/generator/image.rs create mode 100644 src/generator/link.rs diff --git a/README.md b/README.md index 7773b03..9654e08 100644 --- a/README.md +++ b/README.md @@ -19,4 +19,6 @@ headings: { String? h4 # Classes to add to h4 elements String? h5 # Classes to add to h5 elements } + +String link_classes # Classes to add to links () ``` diff --git a/src/config/mod.rs b/src/config/mod.rs index 6d1ad88..a06e163 100644 --- a/src/config/mod.rs +++ b/src/config/mod.rs @@ -13,6 +13,7 @@ pub struct Config { pub template: String, pub extension: String, pub headings: Headings, + pub links_classes: String, } pub struct Headings { @@ -134,12 +135,20 @@ pub fn parse(yaml_str: &String) -> Result { // Heading config let headings_yaml = parse_headings(config_yaml.get(ystr!("headings"))); + + let link_classes = match config_yaml.get(ystr!("link_classes")) { + Some(Yaml::String(input)) => input.clone(), + _ => String::from(""), + }; + + Ok(Config { input: input_folder.clone(), output: output_folder.clone(), template: template_file.clone(), extension: file_extension, headings: headings_yaml, + links_classes: link_classes, }) } diff --git a/src/generator/image.rs b/src/generator/image.rs new file mode 100644 index 0000000..cb79653 --- /dev/null +++ b/src/generator/image.rs @@ -0,0 +1,15 @@ +use markdown::mdast::Image; + +use crate::config::Config; + +use super::Printable; + +impl Printable for Image { + fn to_html(&self, _: &Config) -> String { + format!("\"{}\"", self.url, self.alt, self.title.clone().unwrap_or("".into())) + } + + fn get_text(&self) -> String { + panic!("An image cannot return its raw text") + } +} diff --git a/src/generator/link.rs b/src/generator/link.rs new file mode 100644 index 0000000..d1fbb57 --- /dev/null +++ b/src/generator/link.rs @@ -0,0 +1,24 @@ +use markdown::mdast::Link; + +use crate::config::Config; + +use super::Printable; + +impl Printable for Link { + fn to_html(&self, c: &Config) -> String { + let mut result = Vec::::new(); + + for node in &self.children { + result.push(node.to_html(c)) + } + + let text: String = result.into_iter().collect(); + + format!("{}", c.links_classes, self.url, text) + } + + fn get_text(&self) -> String { + let res = &self.children.iter().map(|x| x.get_text()).collect::>().join(" "); + (*res).clone() + } +} \ No newline at end of file diff --git a/src/generator/mod.rs b/src/generator/mod.rs index 1406652..47516f1 100644 --- a/src/generator/mod.rs +++ b/src/generator/mod.rs @@ -11,6 +11,8 @@ mod paragraph; mod root; mod strong; mod text; +mod image; +mod link; // mod highlighter; @@ -34,6 +36,8 @@ impl Printable for Node { Node::Emphasis(e) => e.to_html(config), Node::List(l) => l.to_html(config), Node::ListItem(l) => l.to_html(config), + Node::Image(i) => i.to_html(config), + Node::Link(l) => l.to_html(config), _ => format!( "
Not implemented
{:?}
", self