html: filter out blanklines, escape

These are not visible, but the blankline breaks the peek for
End(Footnote) to detect whether to append the last paragraph with a
backreference link. A peek will now find the end event instead of an
invisible blankline.
This commit is contained in:
Noah Hellman 2023-01-19 22:14:06 +01:00
parent 8ccfb4c603
commit 0e171c2cf2

View file

@ -48,8 +48,24 @@ enum Raw {
Other, Other,
} }
struct Writer<I: Iterator, W> { struct FilteredEvents<I> {
events: std::iter::Peekable<I>, events: I,
}
impl<'s, I: Iterator<Item = Event<'s>>> Iterator for FilteredEvents<I> {
type Item = Event<'s>;
fn next(&mut self) -> Option<Self::Item> {
let mut ev = self.events.next();
while matches!(ev, Some(Event::Atom(Atom::Blankline | Atom::Escape))) {
ev = self.events.next();
}
ev
}
}
struct Writer<'s, I: Iterator<Item = Event<'s>>, W> {
events: std::iter::Peekable<FilteredEvents<I>>,
out: W, out: W,
raw: Raw, raw: Raw,
text_only: bool, text_only: bool,
@ -58,10 +74,10 @@ struct Writer<I: Iterator, W> {
footnote_backlink_written: bool, footnote_backlink_written: bool,
} }
impl<'s, I: Iterator<Item = Event<'s>>, W: std::fmt::Write> Writer<I, W> { impl<'s, I: Iterator<Item = Event<'s>>, W: std::fmt::Write> Writer<'s, I, W> {
fn new(events: I, out: W) -> Self { fn new(events: I, out: W) -> Self {
Self { Self {
events: events.peekable(), events: FilteredEvents { events }.peekable(),
out, out,
raw: Raw::None, raw: Raw::None,
text_only: false, text_only: false,
@ -324,7 +340,7 @@ impl<'s, I: Iterator<Item = Event<'s>>, W: std::fmt::Write> Writer<I, W> {
Atom::NonBreakingSpace => self.out.write_str("&nbsp;")?, Atom::NonBreakingSpace => self.out.write_str("&nbsp;")?,
Atom::Hardbreak => self.out.write_str("<br>\n")?, Atom::Hardbreak => self.out.write_str("<br>\n")?,
Atom::Softbreak => self.out.write_char('\n')?, Atom::Softbreak => self.out.write_char('\n')?,
Atom::Blankline | Atom::Escape => {} Atom::Escape | Atom::Blankline => unreachable!("filtered out"),
}, },
} }
} }