Generate images & links

master
Araozu 2023-09-09 10:55:28 -05:00
parent 8b4bc594e3
commit ce34180296
5 changed files with 54 additions and 0 deletions

View File

@ -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 (<a>)
```

View File

@ -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<Config, String> {
// 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,
})
}

15
src/generator/image.rs Normal file
View File

@ -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!("<img src=\"{}\" alt=\"{}\" title=\"{}\" />", self.url, self.alt, self.title.clone().unwrap_or("".into()))
}
fn get_text(&self) -> String {
panic!("An image cannot return its raw text")
}
}

24
src/generator/link.rs Normal file
View File

@ -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::<String>::new();
for node in &self.children {
result.push(node.to_html(c))
}
let text: String = result.into_iter().collect();
format!("<a class=\"{}\" href=\"{}\">{}</a>", c.links_classes, self.url, text)
}
fn get_text(&self) -> String {
let res = &self.children.iter().map(|x| x.get_text()).collect::<Vec<String>>().join(" ");
(*res).clone()
}
}

View File

@ -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!(
"<div style=\"background-color: red\">Not implemented<br>{:?}</div>",
self