Use Config struct in the elements generators

master
Araozu 2023-06-25 20:06:09 -05:00
parent 284e547acd
commit 4f185abd87
12 changed files with 48 additions and 36 deletions

View File

@ -2,10 +2,12 @@ use markdown::mdast::Code;
// use super::highlighter::highlight; // use super::highlighter::highlight;
use crate::config::Config;
use super::Printable; use super::Printable;
impl Printable for Code { impl Printable for Code {
fn to_html(&self) -> String { fn to_html(&self, _: &Config) -> String {
let code = &self.value; // highlight(&self.value); let code = &self.value; // highlight(&self.value);
if let Some(lang) = &self.lang { if let Some(lang) = &self.lang {

View File

@ -1,12 +1,12 @@
use markdown::mdast::Emphasis; use markdown::mdast::Emphasis;
use crate::utils; use crate::{utils, config::Config};
use super::Printable; use super::Printable;
impl Printable for Emphasis { impl Printable for Emphasis {
fn to_html(&self) -> String { fn to_html(&self, config: &Config) -> String {
let html = utils::collect_children_html(&self.children); let html = utils::collect_children_html(config, &self.children);
format!("<em>{}</em>", html) format!("<em>{}</em>", html)
} }

View File

@ -1,15 +1,15 @@
use markdown::mdast::Heading; use markdown::mdast::Heading;
use crate::utils; use crate::{utils, config::Config};
use super::Printable; use super::Printable;
impl Printable for Heading { impl Printable for Heading {
fn to_html(&self) -> String { fn to_html(&self, config: &Config) -> String {
let mut result = Vec::<String>::new(); let mut result = Vec::<String>::new();
for node in &self.children { for node in &self.children {
result.push(node.to_html()) result.push(node.to_html(config))
} }
let text: String = result.into_iter().collect(); let text: String = result.into_iter().collect();

View File

@ -1,10 +1,12 @@
use markdown::mdast::InlineCode; use markdown::mdast::InlineCode;
use crate::config::Config;
// use super::highlighter::highlight; // use super::highlighter::highlight;
use super::Printable; use super::Printable;
impl Printable for InlineCode { impl Printable for InlineCode {
fn to_html(&self) -> String { fn to_html(&self, _: &Config) -> String {
/* /*
let tokens = misti::tokenize(&self.value); let tokens = misti::tokenize(&self.value);
println!("INLINE CODE ==== tokens ====\n\n{:?}\n\n==== code ====\n\n{}\n\n", tokens, self.value); println!("INLINE CODE ==== tokens ====\n\n{:?}\n\n==== code ====\n\n{}\n\n", tokens, self.value);

View File

@ -1,15 +1,15 @@
use markdown::mdast::{List, ListItem, Node}; use markdown::mdast::{List, ListItem, Node};
use crate::utils; use crate::{utils, config::Config};
use super::Printable; use super::Printable;
impl Printable for List { impl Printable for List {
fn to_html(&self) -> String { fn to_html(&self, config: &Config) -> String {
let mut result = Vec::<String>::new(); let mut result = Vec::<String>::new();
for node in &self.children { for node in &self.children {
result.push(format!("<li class=\"py-2\">{}</li>", node.to_html())) result.push(format!("<li class=\"py-2\">{}</li>", node.to_html(config)))
} }
let str: String = result.into_iter().collect(); let str: String = result.into_iter().collect();
@ -27,12 +27,12 @@ impl Printable for List {
} }
impl Printable for ListItem { impl Printable for ListItem {
fn to_html(&self) -> String { fn to_html(&self, config: &Config) -> String {
let mut result = Vec::<String>::new(); let mut result = Vec::<String>::new();
for node in &self.children { for node in &self.children {
let s = match node { let s = match node {
Node::Paragraph(p) => utils::collect_children_html(&p.children), Node::Paragraph(p) => utils::collect_children_html(config, &p.children),
_ => panic!("A thing other than Paragraph inside ListItem (?)"), _ => panic!("A thing other than Paragraph inside ListItem (?)"),
}; };
result.push(format!("{}", s)) result.push(format!("{}", s))

View File

@ -1,5 +1,7 @@
use markdown::mdast::Node; use markdown::mdast::Node;
use crate::config::Config;
mod code; mod code;
mod emphasis; mod emphasis;
mod heading; mod heading;
@ -13,25 +15,25 @@ mod text;
// mod highlighter; // mod highlighter;
pub trait Printable { pub trait Printable {
fn to_html(&self) -> String; fn to_html(&self, c: &Config) -> String;
fn get_text(&self) -> String; fn get_text(&self) -> String;
} }
impl Printable for Node { impl Printable for Node {
fn to_html(&self) -> String { fn to_html(&self, config: &Config) -> String {
match self { match self {
Node::Root(root) => root.to_html(), Node::Root(root) => root.to_html(config),
Node::Heading(heading) => heading.to_html(), Node::Heading(heading) => heading.to_html(config),
Node::Text(text) => text.to_html(), Node::Text(text) => text.to_html(config),
Node::Paragraph(p) => p.to_html(), Node::Paragraph(p) => p.to_html(config),
Node::ThematicBreak(_) => String::from("<hr />"), Node::ThematicBreak(_) => String::from("<hr />"),
Node::InlineCode(i) => i.to_html(), Node::InlineCode(i) => i.to_html(config),
Node::Code(c) => c.to_html(), Node::Code(c) => c.to_html(config),
Node::Html(h) => h.value.clone(), Node::Html(h) => h.value.clone(),
Node::Strong(s) => s.to_html(), Node::Strong(s) => s.to_html(config),
Node::Emphasis(e) => e.to_html(), Node::Emphasis(e) => e.to_html(config),
Node::List(l) => l.to_html(), Node::List(l) => l.to_html(config),
Node::ListItem(l) => l.to_html(), Node::ListItem(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

View File

@ -1,13 +1,15 @@
use markdown::mdast::Paragraph; use markdown::mdast::Paragraph;
use crate::config::Config;
use super::Printable; use super::Printable;
impl Printable for Paragraph { impl Printable for Paragraph {
fn to_html(&self) -> String { fn to_html(&self, config: &Config) -> String {
let mut result = Vec::<String>::new(); let mut result = Vec::<String>::new();
for node in &self.children { for node in &self.children {
result.push(node.to_html()) result.push(node.to_html(config))
} }
let text: String = result.into_iter().collect(); let text: String = result.into_iter().collect();

View File

@ -1,13 +1,15 @@
use markdown::mdast; use markdown::mdast;
use crate::config::Config;
use super::Printable; use super::Printable;
impl Printable for mdast::Root { impl Printable for mdast::Root {
fn to_html(&self) -> String { fn to_html(&self, config: &Config) -> String {
let mut result = Vec::<String>::new(); let mut result = Vec::<String>::new();
for node in &self.children { for node in &self.children {
result.push(node.to_html()) result.push(node.to_html(config))
} }
result.into_iter().collect() result.into_iter().collect()

View File

@ -1,12 +1,12 @@
use markdown::mdast::Strong; use markdown::mdast::Strong;
use crate::utils; use crate::{utils, config::Config};
use super::Printable; use super::Printable;
impl Printable for Strong { impl Printable for Strong {
fn to_html(&self) -> String { fn to_html(&self, config: &Config) -> String {
let text = utils::collect_children_html(&self.children); let text = utils::collect_children_html(config, &self.children);
format!("<b>{}</b>", text) format!("<b>{}</b>", text)
} }

View File

@ -1,9 +1,11 @@
use markdown::mdast::Text; use markdown::mdast::Text;
use crate::config::{Config};
use super::Printable; use super::Printable;
impl Printable for Text { impl Printable for Text {
fn to_html(&self) -> String { fn to_html(&self, _: &Config) -> String {
self.value.clone() self.value.clone()
} }

View File

@ -43,7 +43,7 @@ pub fn compile(config: &Config, file: &PathBuf, file_tree_html: &String) {
// Compile MD // Compile MD
// //
let md_ast = markdown::to_mdast(&markdown_text, &markdown::ParseOptions::gfm()).unwrap(); let md_ast = markdown::to_mdast(&markdown_text, &markdown::ParseOptions::gfm()).unwrap();
let html_text = md_ast.to_html(); let html_text = md_ast.to_html(config);
let sidebar_html = md_ast.generate_sidebar(); let sidebar_html = md_ast.generate_sidebar();
// Read template.html // Read template.html

View File

@ -8,11 +8,11 @@ pub fn to_html_fragment(text: &String) -> String {
text.clone().replace(" ", "-") text.clone().replace(" ", "-")
} }
pub fn collect_children_html(vec: &Vec<Node>) -> String { pub fn collect_children_html(config: &Config, vec: &Vec<Node>) -> String {
let mut result = Vec::<String>::new(); let mut result = Vec::<String>::new();
for node in vec { for node in vec {
result.push(node.to_html()) result.push(node.to_html(config))
} }
result.into_iter().collect() result.into_iter().collect()