//! An HTML renderer that takes an iterator of [`Event`]s and emits HTML. //! //! The HTML can be written to either a [`std::fmt::Write`] or a [`std::io::Write`] object. //! //! # Examples //! //! Push to a [`String`] (implements [`std::fmt::Write`]): //! //! ``` //! # let events = std::iter::empty(); //! let mut html = String::new(); //! jotdown::html::push(events, &mut html); //! ``` //! //! Write to standard output with buffering ([`std::io::Stdout`] implements [`std::io::Write`]): //! //! ``` //! # let events = std::iter::empty(); //! let mut out = std::io::BufWriter::new(std::io::stdout()); //! jotdown::html::write(events, &mut out).unwrap(); //! ``` use crate::Alignment; use crate::Container; use crate::Event; use crate::ListKind; use crate::OrderedListNumbering::*; /// Generate HTML and push it to a unicode-accepting buffer or stream. pub fn push<'s, I: Iterator>, W: std::fmt::Write>(events: I, out: W) { Writer::new(events, out).write().unwrap(); } /// Generate HTML and write it to a byte sink, encoded as UTF-8. /// /// NOTE: This performs many small writes, so IO writes should be buffered with e.g. /// [`std::io::BufWriter`]. pub fn write<'s, I: Iterator>, W: std::io::Write>( events: I, mut out: W, ) -> std::io::Result<()> { struct Adapter<'a, T: ?Sized + 'a> { inner: &'a mut T, error: std::io::Result<()>, } impl std::fmt::Write for Adapter<'_, T> { fn write_str(&mut self, s: &str) -> std::fmt::Result { match self.inner.write_all(s.as_bytes()) { Ok(()) => Ok(()), Err(e) => { self.error = Err(e); Err(std::fmt::Error) } } } } let mut output = Adapter { inner: &mut out, error: Ok(()), }; Writer::new(events, &mut output) .write() .map_err(|_| output.error.unwrap_err()) } enum Raw { None, Html, Other, } struct FilteredEvents { events: I, } impl<'s, I: Iterator>> Iterator for FilteredEvents { type Item = Event<'s>; fn next(&mut self) -> Option { let mut ev = self.events.next(); while matches!(ev, Some(Event::Blankline | Event::Escape)) { ev = self.events.next(); } ev } } struct Writer<'s, I: Iterator>, W> { events: std::iter::Peekable>, out: W, raw: Raw, text_only: bool, list_tightness: Vec, encountered_footnote: bool, footnote_number: Option, footnote_backlink_written: bool, first_line: bool, } impl<'s, I: Iterator>, W: std::fmt::Write> Writer<'s, I, W> { fn new(events: I, out: W) -> Self { Self { events: FilteredEvents { events }.peekable(), out, raw: Raw::None, text_only: false, list_tightness: Vec::new(), encountered_footnote: false, footnote_number: None, footnote_backlink_written: false, first_line: true, } } fn write(&mut self) -> std::fmt::Result { while let Some(e) = self.events.next() { match e { Event::Start(c, attrs) => { if c.is_block() && !self.first_line { self.out.write_char('\n')?; } if self.text_only && !matches!(c, Container::Image(..)) { continue; } match &c { Container::Blockquote => self.out.write_str(" { self.list_tightness.push(*tight); match kind { ListKind::Unordered | ListKind::Task => { self.out.write_str(" { self.out.write_str(" 1 { write!(self.out, r#" start="{}""#, start)?; } if let Some(ty) = match numbering { Decimal => None, AlphaLower => Some('a'), AlphaUpper => Some('A'), RomanLower => Some('i'), RomanUpper => Some('I'), } { write!(self.out, r#" type="{}""#, ty)?; } } } } Container::ListItem | Container::TaskListItem { .. } => { self.out.write_str(" self.out.write_str(" self.out.write_str(" { assert!(self.footnote_number.is_none()); self.footnote_number = Some((*number).try_into().unwrap()); if !self.encountered_footnote { self.encountered_footnote = true; self.out .write_str("
\n
\n
    \n")?; } write!(self.out, "
  1. ", number)?; self.footnote_backlink_written = false; continue; } Container::Table => self.out.write_str(" self.out.write_str(" self.out.write_str(" self.out.write_str(" { if matches!(self.list_tightness.last(), Some(true)) { continue; } self.out.write_str(" write!(self.out, " self.out.write_str(" self.out.write_str(" self.out.write_str(" self.out.write_str(" self.out.write_str(" self.out.write_str(" { if dst.is_empty() { self.out.write_str(" { self.text_only = true; self.out.write_str(" self.out.write_str(" { self.raw = if format == &"html" { Raw::Html } else { Raw::Other }; continue; } Container::Subscript => self.out.write_str(" self.out.write_str(" self.out.write_str(" self.out.write_str(" self.out.write_str(" self.out.write_str(" self.out.write_str(" Some("task-list"), Container::TaskListItem { checked: false } => Some("unchecked"), Container::TaskListItem { checked: true } => Some("checked"), Container::Math { display: false } => Some("math inline"), Container::Math { display: true } => Some("math display"), _ => None, } { first_written = true; self.out.write_str(cls)?; } for cls in attrs .iter() .filter(|(a, _)| a == &"class") .map(|(_, cls)| cls) { if first_written { self.out.write_char(' ')?; } first_written = true; self.out.write_str(cls)?; } // div class goes after classes from attrs if let Container::Div { class: Some(cls) } = c { if first_written { self.out.write_char(' ')?; } self.out.write_str(cls)?; } self.out.write_char('"')?; } match c { Container::TableCell { alignment, .. } if !matches!(alignment, Alignment::Unspecified) => { let a = match alignment { Alignment::Unspecified => unreachable!(), Alignment::Left => "left", Alignment::Center => "center", Alignment::Right => "right", }; write!(self.out, r#" style="text-align: {};">"#, a)?; } Container::CodeBlock { lang } => { if let Some(l) = lang { self.out.write_str(r#">"#)?; } else { self.out.write_str(">")?; } } Container::Image(..) => { self.out.write_str(r#" alt=""#)?; } Container::Math { display } => { self.out .write_str(if display { r#">\["# } else { r#">\("# })?; } _ => self.out.write_char('>')?, } } Event::End(c) => { if c.is_block_container() && !matches!(c, Container::Footnote { .. }) { self.out.write_char('\n')?; } if self.text_only && !matches!(c, Container::Image(..)) { continue; } match c { Container::Blockquote => self.out.write_str("")?, Container::List { kind: ListKind::Unordered | ListKind::Task, .. } => { self.list_tightness.pop(); self.out.write_str("")?; } Container::List { kind: ListKind::Ordered { .. }, .. } => self.out.write_str("
")?, Container::ListItem | Container::TaskListItem { .. } => { self.out.write_str("")?; } Container::DescriptionList => self.out.write_str("")?, Container::DescriptionDetails => self.out.write_str("")?, Container::Footnote { number, .. } => { if !self.footnote_backlink_written { write!( self.out, "\n

↩︎︎

", number, )?; } self.out.write_str("\n")?; self.footnote_number = None; } Container::Table => self.out.write_str("")?, Container::TableRow { .. } => self.out.write_str("")?, Container::Section { .. } => self.out.write_str("
")?, Container::Div { .. } => self.out.write_str("")?, Container::Paragraph => { if matches!(self.list_tightness.last(), Some(true)) { continue; } if let Some(num) = self.footnote_number { if matches!( self.events.peek(), Some(Event::End(Container::Footnote { .. })) ) { write!( self.out, r##"↩︎︎"##, num )?; self.footnote_backlink_written = true; } } self.out.write_str("

")?; } Container::Heading { level, .. } => write!(self.out, "", level)?, Container::TableCell { head: false, .. } => self.out.write_str("")?, Container::TableCell { head: true, .. } => self.out.write_str("")?, Container::Caption => self.out.write_str("")?, Container::DescriptionTerm => self.out.write_str("")?, Container::CodeBlock { .. } => self.out.write_str("
")?, Container::Span => self.out.write_str("")?, Container::Link(..) => self.out.write_str("")?, Container::Image(src, ..) => { self.text_only = false; if src.is_empty() { self.out.write_str(r#"">"#)?; } else { write!(self.out, r#"" src="{}">"#, src)?; } } Container::Verbatim => self.out.write_str("
")?, Container::Math { display } => { self.out.write_str(if display { r#"\]"# } else { r#"\)"# })?; } Container::RawBlock { .. } | Container::RawInline { .. } => { self.raw = Raw::None; } Container::Subscript => self.out.write_str("")?, Container::Superscript => self.out.write_str("")?, Container::Insert => self.out.write_str("")?, Container::Delete => self.out.write_str("")?, Container::Strong => self.out.write_str("")?, Container::Emphasis => self.out.write_str("")?, Container::Mark => self.out.write_str("")?, } } Event::Str(s) => match self.raw { Raw::None => self.write_escape(&s)?, Raw::Html => self.out.write_str(&s)?, Raw::Other => {} }, Event::FootnoteReference(_tag, number) => { write!( self.out, r##"{}"##, number, number, number )?; } Event::Symbol(sym) => write!(self.out, ":{}:", sym)?, Event::LeftSingleQuote => self.out.write_str("‘")?, Event::RightSingleQuote => self.out.write_str("’")?, Event::LeftDoubleQuote => self.out.write_str("“")?, Event::RightDoubleQuote => self.out.write_str("”")?, Event::Ellipsis => self.out.write_str("…")?, Event::EnDash => self.out.write_str("–")?, Event::EmDash => self.out.write_str("—")?, Event::NonBreakingSpace => self.out.write_str(" ")?, Event::Hardbreak => self.out.write_str("
\n")?, Event::Softbreak => self.out.write_char('\n')?, Event::Escape | Event::Blankline => unreachable!("filtered out"), Event::ThematicBreak(attrs) => { self.out.write_str("\n")?; } } self.first_line = false; } if self.encountered_footnote { self.out.write_str("\n\n")?; } self.out.write_char('\n')?; Ok(()) } fn write_escape(&mut self, mut s: &str) -> std::fmt::Result { let mut ent = ""; while let Some(i) = s.find(|c| { match c { '<' => Some("<"), '>' => Some(">"), '&' => Some("&"), _ => None, } .map_or(false, |s| { ent = s; true }) }) { self.out.write_str(&s[..i])?; self.out.write_str(ent)?; s = &s[i + 1..]; } self.out.write_str(s) } }