use crate::syntax_highlight::syntax_highlight; use super::parser::{Block, Span}; pub fn span_to_html(ss: Vec) -> String { let mut out = String::new(); for s in ss { out += match s { Span::Break => format!("
"), Span::Text(t) => escape(&t), Span::Code(c) => format!("{}", escape(&c)), Span::Link(text, url) => { format!("{}", escape(&url), escape(&text)) } Span::Image(_, _) => todo!(), Span::Emphasis(c) => format!("{}", span_to_html(c)), Span::Strong(c) => format!("{}", span_to_html(c)), Span::Latex(s) => fix_katex( &katex::render_with_opts(&s, &katex::OptsBuilder::default().build().unwrap()) .unwrap(), ), } .as_str() } out } pub fn blocks_to_html(blocks: Vec) -> String { let mut out = String::new(); for e in blocks { out += match e { Block::Header(level, text) => { format!("{}", span_to_html(text)) } Block::Paragraph(p) => format!("

{}

", span_to_html(p)), Block::Blockquote(q) => format!("{}", blocks_to_html(q)), Block::CodeBlock(syntax, content) => { if let Some(s) = &syntax { format!("
{}
", syntax_highlight(s, &content)) } else { format!("
{}
", escape(&content)) } } Block::OrderedList(els) => format!( "
    {}
", els.into_iter() .map(|e| format!("
  • {}
  • ", blocks_to_html(e))) .collect::>() .join("") ), Block::UnorderedList(els) => { format!( "
      {}
    ", els.into_iter() .map(|e| format!("
  • {}
  • ", blocks_to_html(e))) .collect::>() .join("") ) } Block::Raw(r) => r, Block::Hr => format!("
    "), Block::LatexBlock(s) => fix_katex( &katex::render_with_opts( &s, &katex::OptsBuilder::default() .display_mode(true) .build() .unwrap(), ) .unwrap(), ), } .as_str(); } out } pub fn escape(text: &str) -> String { text.replace("&", "&") .replace("<", "<") .replace(">", ">") .replace("'", "’") .replace("\"", """) } // TODO this is *really* bad fix fn fix_katex<'a>(s: &str) -> String { let e = s.find("", "", ) }