html: extract Writer::render_{event, epilogue}

This commit is contained in:
Noah Hellman 2023-03-19 18:35:41 +01:00
parent 3d1b5f2115
commit 8eafdf073b

View file

@ -74,14 +74,21 @@ impl Default for Writer {
}
impl Writer {
fn write<'s>(
&mut self,
events: impl Iterator<Item = Event<'s>>,
mut out: impl std::fmt::Write,
) -> std::fmt::Result {
for e in events {
fn write<'s, I, W>(&mut self, mut events: I, mut out: W) -> std::fmt::Result
where
I: Iterator<Item = Event<'s>>,
W: std::fmt::Write,
{
events.try_for_each(|e| self.render_event(&e, &mut out))?;
self.render_epilogue(&mut out)
}
fn render_event<'s, W>(&mut self, e: &Event<'s>, mut out: W) -> std::fmt::Result
where
W: std::fmt::Write,
{
if matches!(&e, Event::Blankline | Event::Escape) {
continue;
return Ok(());
}
let close_para = self.close_para;
@ -99,7 +106,7 @@ impl Writer {
out.write_char('\n')?;
}
if self.img_alt_text > 0 && !matches!(c, Container::Image(..)) {
continue;
return Ok(());
}
match &c {
Container::Blockquote => out.write_str("<blockquote")?,
@ -139,7 +146,7 @@ impl Writer {
out.write_str("<section role=\"doc-endnotes\">\n<hr>\n<ol>\n")?;
}
write!(out, "<li id=\"fn{}\">", number)?;
continue;
return Ok(());
}
Container::Table => out.write_str("<table")?,
Container::TableRow { .. } => out.write_str("<tr")?,
@ -147,7 +154,7 @@ impl Writer {
Container::Div { .. } => out.write_str("<div")?,
Container::Paragraph => {
if matches!(self.list_tightness.last(), Some(true)) {
continue;
return Ok(());
}
out.write_str("<p")?;
}
@ -175,7 +182,7 @@ impl Writer {
if self.img_alt_text == 1 {
out.write_str("<img")?;
} else {
continue;
return Ok(());
}
}
Container::Verbatim => out.write_str("<code")?,
@ -185,7 +192,7 @@ impl Writer {
} else {
Raw::Other
};
continue;
return Ok(());
}
Container::Subscript => out.write_str("<sub")?,
Container::Superscript => out.write_str("<sup")?,
@ -293,7 +300,7 @@ impl Writer {
}
}
Container::Math { display } => {
out.write_str(if display { r#">\["# } else { r#">\("# })?;
out.write_str(if *display { r#">\["# } else { r#">\("# })?;
}
_ => out.write_char('>')?,
}
@ -303,7 +310,7 @@ impl Writer {
out.write_char('\n')?;
}
if self.img_alt_text > 0 && !matches!(c, Container::Image(..)) {
continue;
return Ok(());
}
match c {
Container::Blockquote => out.write_str("</blockquote>")?,
@ -342,7 +349,7 @@ impl Writer {
Container::Div { .. } => out.write_str("</div>")?,
Container::Paragraph => {
if matches!(self.list_tightness.last(), Some(true)) {
continue;
return Ok(());
}
if self.footnote_number.is_none() {
out.write_str("</p>")?;
@ -362,7 +369,7 @@ impl Writer {
if self.img_alt_text == 1 {
if !src.is_empty() {
out.write_str(r#"" src=""#)?;
write_attr(&src, &mut out)?;
write_attr(src, &mut out)?;
}
out.write_str(r#"">"#)?;
}
@ -370,7 +377,7 @@ impl Writer {
}
Container::Verbatim => out.write_str("</code>")?,
Container::Math { display } => {
out.write_str(if display {
out.write_str(if *display {
r#"\]</span>"#
} else {
r#"\)</span>"#
@ -389,9 +396,9 @@ impl Writer {
}
}
Event::Str(s) => match self.raw {
Raw::None if self.img_alt_text > 0 => write_attr(&s, &mut out)?,
Raw::None => write_text(&s, &mut out)?,
Raw::Html => out.write_str(&s)?,
Raw::None if self.img_alt_text > 0 => write_attr(s, &mut out)?,
Raw::None => write_text(s, &mut out)?,
Raw::Html => out.write_str(s)?,
Raw::Other => {}
},
Event::FootnoteReference(_tag, number) => {
@ -426,11 +433,19 @@ impl Writer {
}
}
self.first_line = false;
Ok(())
}
fn render_epilogue<W>(&mut self, mut out: W) -> std::fmt::Result
where
W: std::fmt::Write,
{
if self.encountered_footnote {
out.write_str("\n</ol>\n</section>")?;
}
out.write_char('\n')?;
Ok(())
}
}