use crate::elements::*;
use htmlescape::encode_minimal;
use minify::html::minify;
macro_rules! combine_with_lb {
($a:expr, $b:expr) => {
if $a.len() > 0 {
format!("{}
{}", $a, $b.to_html())
} else {
$b.to_html()
}
};
}
pub trait ToHtml {
fn to_html(&self) -> String;
}
impl ToHtml for Inline {
fn to_html(&self) -> String {
match self {
Inline::Text(text) => text.to_html(),
Inline::Ruler(ruler) => ruler.to_html(),
}
}
}
impl ToHtml for SubText {
fn to_html(&self) -> String {
match self {
SubText::Url(url) => url.to_html(),
SubText::Monospace(mono) => mono.to_html(),
SubText::Striked(striked) => striked.to_html(),
SubText::Plain(plain) => plain.to_html(),
SubText::Italic(italic) => italic.to_html(),
SubText::Underlined(under) => under.to_html(),
SubText::Bold(bold) => bold.to_html(),
SubText::Image(img) => img.to_html(),
_ => "".to_string(),
}
}
}
impl ToHtml for Block {
fn to_html(&self) -> String {
match self {
Block::Paragraph(para) => para.to_html(),
Block::List(list) => list.to_html(),
Block::Table(table) => table.to_html(),
Block::CodeBlock(code) => code.to_html(),
Block::Quote(quote) => quote.to_html(),
Block::Section(section) => section.to_html(),
Block::Import(import) => import.to_html(),
}
}
}
impl ToHtml for Document {
fn to_html(&self) -> String {
let inner = self
.elements
.iter()
.fold("".to_string(), |a, b| format!("{}{}", a, b.to_html()));
if self.is_root {
let style = minify(std::include_str!("assets/style.css"));
format!(
"\n
{}
", inner) } } impl ToHtml for List { fn to_html(&self) -> String { let inner = self .items .iter() .fold("".to_string(), |a, b| format!("{}{}", a, b.to_html())); if self.ordered { format!("{}
{}
{}
{}
", self.value.to_html())
}
}
impl ToHtml for Url {
fn to_html(&self) -> String {
if let Some(description) = self.description.clone() {
format!(
"{}",
self.url.clone(),
encode_minimal(description.as_str())
)
} else {
format!(
"{}",
self.url.clone(),
encode_minimal(self.url.clone().as_str())
)
}
}
}
impl ToHtml for PlainText {
fn to_html(&self) -> String {
encode_minimal(self.value.clone().as_str())
}
}