Generate images & links
This commit is contained in:
parent
8b4bc594e3
commit
ce34180296
@ -19,4 +19,6 @@ headings: {
|
|||||||
String? h4 # Classes to add to h4 elements
|
String? h4 # Classes to add to h4 elements
|
||||||
String? h5 # Classes to add to h5 elements
|
String? h5 # Classes to add to h5 elements
|
||||||
}
|
}
|
||||||
|
|
||||||
|
String link_classes # Classes to add to links (<a>)
|
||||||
```
|
```
|
||||||
|
@ -13,6 +13,7 @@ pub struct Config {
|
|||||||
pub template: String,
|
pub template: String,
|
||||||
pub extension: String,
|
pub extension: String,
|
||||||
pub headings: Headings,
|
pub headings: Headings,
|
||||||
|
pub links_classes: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct Headings {
|
pub struct Headings {
|
||||||
@ -134,12 +135,20 @@ pub fn parse(yaml_str: &String) -> Result<Config, String> {
|
|||||||
// Heading config
|
// Heading config
|
||||||
let headings_yaml = parse_headings(config_yaml.get(ystr!("headings")));
|
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 {
|
Ok(Config {
|
||||||
input: input_folder.clone(),
|
input: input_folder.clone(),
|
||||||
output: output_folder.clone(),
|
output: output_folder.clone(),
|
||||||
template: template_file.clone(),
|
template: template_file.clone(),
|
||||||
extension: file_extension,
|
extension: file_extension,
|
||||||
headings: headings_yaml,
|
headings: headings_yaml,
|
||||||
|
links_classes: link_classes,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
15
src/generator/image.rs
Normal file
15
src/generator/image.rs
Normal 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
24
src/generator/link.rs
Normal 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()
|
||||||
|
}
|
||||||
|
}
|
@ -11,6 +11,8 @@ mod paragraph;
|
|||||||
mod root;
|
mod root;
|
||||||
mod strong;
|
mod strong;
|
||||||
mod text;
|
mod text;
|
||||||
|
mod image;
|
||||||
|
mod link;
|
||||||
|
|
||||||
// mod highlighter;
|
// mod highlighter;
|
||||||
|
|
||||||
@ -34,6 +36,8 @@ impl Printable for Node {
|
|||||||
Node::Emphasis(e) => e.to_html(config),
|
Node::Emphasis(e) => e.to_html(config),
|
||||||
Node::List(l) => l.to_html(config),
|
Node::List(l) => l.to_html(config),
|
||||||
Node::ListItem(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!(
|
_ => format!(
|
||||||
"<div style=\"background-color: red\">Not implemented<br>{:?}</div>",
|
"<div style=\"background-color: red\">Not implemented<br>{:?}</div>",
|
||||||
self
|
self
|
||||||
|
Loading…
Reference in New Issue
Block a user