Use Config struct in the elements generators
This commit is contained in:
parent
284e547acd
commit
4f185abd87
@ -2,10 +2,12 @@ use markdown::mdast::Code;
|
||||
|
||||
// use super::highlighter::highlight;
|
||||
|
||||
use crate::config::Config;
|
||||
|
||||
use super::Printable;
|
||||
|
||||
impl Printable for Code {
|
||||
fn to_html(&self) -> String {
|
||||
fn to_html(&self, _: &Config) -> String {
|
||||
let code = &self.value; // highlight(&self.value);
|
||||
|
||||
if let Some(lang) = &self.lang {
|
||||
|
@ -1,12 +1,12 @@
|
||||
use markdown::mdast::Emphasis;
|
||||
|
||||
use crate::utils;
|
||||
use crate::{utils, config::Config};
|
||||
|
||||
use super::Printable;
|
||||
|
||||
impl Printable for Emphasis {
|
||||
fn to_html(&self) -> String {
|
||||
let html = utils::collect_children_html(&self.children);
|
||||
fn to_html(&self, config: &Config) -> String {
|
||||
let html = utils::collect_children_html(config, &self.children);
|
||||
|
||||
format!("<em>{}</em>", html)
|
||||
}
|
||||
|
@ -1,15 +1,15 @@
|
||||
use markdown::mdast::Heading;
|
||||
|
||||
use crate::utils;
|
||||
use crate::{utils, config::Config};
|
||||
|
||||
use super::Printable;
|
||||
|
||||
impl Printable for Heading {
|
||||
fn to_html(&self) -> String {
|
||||
fn to_html(&self, config: &Config) -> String {
|
||||
let mut result = Vec::<String>::new();
|
||||
|
||||
for node in &self.children {
|
||||
result.push(node.to_html())
|
||||
result.push(node.to_html(config))
|
||||
}
|
||||
|
||||
let text: String = result.into_iter().collect();
|
||||
|
@ -1,10 +1,12 @@
|
||||
use markdown::mdast::InlineCode;
|
||||
|
||||
use crate::config::Config;
|
||||
|
||||
// use super::highlighter::highlight;
|
||||
use super::Printable;
|
||||
|
||||
impl Printable for InlineCode {
|
||||
fn to_html(&self) -> String {
|
||||
fn to_html(&self, _: &Config) -> String {
|
||||
/*
|
||||
let tokens = misti::tokenize(&self.value);
|
||||
println!("INLINE CODE ==== tokens ====\n\n{:?}\n\n==== code ====\n\n{}\n\n", tokens, self.value);
|
||||
|
@ -1,15 +1,15 @@
|
||||
use markdown::mdast::{List, ListItem, Node};
|
||||
|
||||
use crate::utils;
|
||||
use crate::{utils, config::Config};
|
||||
|
||||
use super::Printable;
|
||||
|
||||
impl Printable for List {
|
||||
fn to_html(&self) -> String {
|
||||
fn to_html(&self, config: &Config) -> String {
|
||||
let mut result = Vec::<String>::new();
|
||||
|
||||
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();
|
||||
@ -27,12 +27,12 @@ impl Printable for List {
|
||||
}
|
||||
|
||||
impl Printable for ListItem {
|
||||
fn to_html(&self) -> String {
|
||||
fn to_html(&self, config: &Config) -> String {
|
||||
let mut result = Vec::<String>::new();
|
||||
|
||||
for node in &self.children {
|
||||
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 (?)"),
|
||||
};
|
||||
result.push(format!("{}", s))
|
||||
|
@ -1,5 +1,7 @@
|
||||
use markdown::mdast::Node;
|
||||
|
||||
use crate::config::Config;
|
||||
|
||||
mod code;
|
||||
mod emphasis;
|
||||
mod heading;
|
||||
@ -13,25 +15,25 @@ mod text;
|
||||
// mod highlighter;
|
||||
|
||||
pub trait Printable {
|
||||
fn to_html(&self) -> String;
|
||||
fn to_html(&self, c: &Config) -> String;
|
||||
fn get_text(&self) -> String;
|
||||
}
|
||||
|
||||
impl Printable for Node {
|
||||
fn to_html(&self) -> String {
|
||||
fn to_html(&self, config: &Config) -> String {
|
||||
match self {
|
||||
Node::Root(root) => root.to_html(),
|
||||
Node::Heading(heading) => heading.to_html(),
|
||||
Node::Text(text) => text.to_html(),
|
||||
Node::Paragraph(p) => p.to_html(),
|
||||
Node::Root(root) => root.to_html(config),
|
||||
Node::Heading(heading) => heading.to_html(config),
|
||||
Node::Text(text) => text.to_html(config),
|
||||
Node::Paragraph(p) => p.to_html(config),
|
||||
Node::ThematicBreak(_) => String::from("<hr />"),
|
||||
Node::InlineCode(i) => i.to_html(),
|
||||
Node::Code(c) => c.to_html(),
|
||||
Node::InlineCode(i) => i.to_html(config),
|
||||
Node::Code(c) => c.to_html(config),
|
||||
Node::Html(h) => h.value.clone(),
|
||||
Node::Strong(s) => s.to_html(),
|
||||
Node::Emphasis(e) => e.to_html(),
|
||||
Node::List(l) => l.to_html(),
|
||||
Node::ListItem(l) => l.to_html(),
|
||||
Node::Strong(s) => s.to_html(config),
|
||||
Node::Emphasis(e) => e.to_html(config),
|
||||
Node::List(l) => l.to_html(config),
|
||||
Node::ListItem(l) => l.to_html(config),
|
||||
_ => format!(
|
||||
"<div style=\"background-color: red\">Not implemented<br>{:?}</div>",
|
||||
self
|
||||
|
@ -1,13 +1,15 @@
|
||||
use markdown::mdast::Paragraph;
|
||||
|
||||
use crate::config::Config;
|
||||
|
||||
use super::Printable;
|
||||
|
||||
impl Printable for Paragraph {
|
||||
fn to_html(&self) -> String {
|
||||
fn to_html(&self, config: &Config) -> String {
|
||||
let mut result = Vec::<String>::new();
|
||||
|
||||
for node in &self.children {
|
||||
result.push(node.to_html())
|
||||
result.push(node.to_html(config))
|
||||
}
|
||||
|
||||
let text: String = result.into_iter().collect();
|
||||
|
@ -1,13 +1,15 @@
|
||||
use markdown::mdast;
|
||||
|
||||
use crate::config::Config;
|
||||
|
||||
use super::Printable;
|
||||
|
||||
impl Printable for mdast::Root {
|
||||
fn to_html(&self) -> String {
|
||||
fn to_html(&self, config: &Config) -> String {
|
||||
let mut result = Vec::<String>::new();
|
||||
|
||||
for node in &self.children {
|
||||
result.push(node.to_html())
|
||||
result.push(node.to_html(config))
|
||||
}
|
||||
|
||||
result.into_iter().collect()
|
||||
|
@ -1,12 +1,12 @@
|
||||
use markdown::mdast::Strong;
|
||||
|
||||
use crate::utils;
|
||||
use crate::{utils, config::Config};
|
||||
|
||||
use super::Printable;
|
||||
|
||||
impl Printable for Strong {
|
||||
fn to_html(&self) -> String {
|
||||
let text = utils::collect_children_html(&self.children);
|
||||
fn to_html(&self, config: &Config) -> String {
|
||||
let text = utils::collect_children_html(config, &self.children);
|
||||
|
||||
format!("<b>{}</b>", text)
|
||||
}
|
||||
|
@ -1,9 +1,11 @@
|
||||
use markdown::mdast::Text;
|
||||
|
||||
use crate::config::{Config};
|
||||
|
||||
use super::Printable;
|
||||
|
||||
impl Printable for Text {
|
||||
fn to_html(&self) -> String {
|
||||
fn to_html(&self, _: &Config) -> String {
|
||||
self.value.clone()
|
||||
}
|
||||
|
||||
|
@ -43,7 +43,7 @@ pub fn compile(config: &Config, file: &PathBuf, file_tree_html: &String) {
|
||||
// Compile MD
|
||||
//
|
||||
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();
|
||||
|
||||
// Read template.html
|
||||
|
@ -8,11 +8,11 @@ pub fn to_html_fragment(text: &String) -> String {
|
||||
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();
|
||||
|
||||
for node in vec {
|
||||
result.push(node.to_html())
|
||||
result.push(node.to_html(config))
|
||||
}
|
||||
|
||||
result.into_iter().collect()
|
||||
|
Loading…
Reference in New Issue
Block a user