mod footnote_backrefs;
mod footnote_defs;
mod heading_anchors;
mod highlight;
mod link_decorations;

use pulldown_cmark::{html, Event, Options, Parser};
use std::io::Write;

pub fn render(s: &str, writer: impl Write) {
    html::write_html(writer, parse(s)).unwrap();
}

pub fn parse<'a>(s: &'a str) -> impl Iterator<Item = Event<'a>> {
    let mut options = Options::empty();
    options.insert(Options::ENABLE_TABLES);
    options.insert(Options::ENABLE_FOOTNOTES);
    options.insert(Options::ENABLE_STRIKETHROUGH);
    options.insert(Options::ENABLE_SMART_PUNCTUATION);
    let parser = Parser::new_ext(s, options);
    let heading_anchors = heading_anchors::new(parser);
    let link_decorations = link_decorations::new(heading_anchors);
    // note backrefs need to come before defs, because the defs stage replaces the
    // Tag::FootnoteDefinition events that the backrefs stage relies on with plain html
    let footnote_backrefs = footnote_backrefs::new(link_decorations);
    let footnote_defs = footnote_defs::new(footnote_backrefs);
    let highlight = highlight::new(footnote_defs);
    highlight
}

// #[cfg(test)]
// mod tests {
//     fn render(s: &str) -> String {
//         let mut out = String::new();
//         pulldown_cmark::html::push_html(&mut out, super::parse(s));
//         out
//     }

//     #[test]
//     fn test_multiple_footnotes() {
//         let s = "foo[^1] bar[^2]\n\n[^1]: foo\n\n[^2]: bar";
//         dbg!(super::parse(s).collect::<Vec<_>>());
//         println!("{}", render(s));
//         panic!();
//     }
// }
