use pest::{ iterators::{Pair, Pairs}, Parser, }; use pest_derive::Parser; use crate::html::escape; #[derive(Parser)] #[grammar = "grammar/markdown.pest"] struct Grammar; pub fn render(s: &str) -> String { match Grammar::parse(Rule::file, s) { Ok(pairs) => { eprintln!("{pairs:#?}"); render_pairs(pairs) } Err(e) => panic!("{e}"), } } pub fn render_pairs(p: Pairs) -> String { p.map(|p| render_ast(p)).collect::>().join("") } pub fn render_ast(p: Pair) -> String { match p.as_rule() { Rule::block => render_pairs(p.into_inner()), Rule::header => format!("

{}

", render_pairs(p.into_inner())), Rule::paragraph => format!("

{}

", render_pairs(p.into_inner())), Rule::list => format!("
    {}
", render_pairs(p.into_inner())), Rule::list_item => format!("
  • {}
  • ", render_pairs(p.into_inner())), Rule::span => render_pairs(p.into_inner()), Rule::text => escape(p.as_str()), Rule::EOI => "".to_string(), _ => todo!("{:?}", p.as_rule()), } }