move atomic events to Event from Atom

An additional Atom enum seems to be more cumbersome and add little
value.

methods could potentially be used to classify events in several ways,
e.g.  block vs inline, atomic vs container
This commit is contained in:
Noah Hellman 2023-02-04 21:59:01 +01:00
parent 2811493c34
commit 5efb700c9b
2 changed files with 83 additions and 93 deletions

View file

@ -1,7 +1,6 @@
//! An HTML renderer that takes an iterator of [`Event`]s and emits HTML. //! An HTML renderer that takes an iterator of [`Event`]s and emits HTML.
use crate::Alignment; use crate::Alignment;
use crate::Atom;
use crate::Container; use crate::Container;
use crate::Event; use crate::Event;
use crate::ListKind; use crate::ListKind;
@ -62,7 +61,7 @@ impl<'s, I: Iterator<Item = Event<'s>>> Iterator for FilteredEvents<I> {
fn next(&mut self) -> Option<Self::Item> { fn next(&mut self) -> Option<Self::Item> {
let mut ev = self.events.next(); let mut ev = self.events.next();
while matches!(ev, Some(Event::Atom(Atom::Blankline | Atom::Escape))) { while matches!(ev, Some(Event::Blankline | Event::Escape)) {
ev = self.events.next(); ev = self.events.next();
} }
ev ev
@ -398,27 +397,25 @@ impl<'s, I: Iterator<Item = Event<'s>>, W: std::fmt::Write> Writer<'s, I, W> {
Raw::Html => self.out.write_str(&s)?, Raw::Html => self.out.write_str(&s)?,
Raw::Other => {} Raw::Other => {}
}, },
Event::Atom(a) => match a { Event::FootnoteReference(_tag, number) => {
Atom::FootnoteReference(_tag, number) => {
write!( write!(
self.out, self.out,
r##"<a id="fnref{}" href="#fn{}" role="doc-noteref"><sup>{}</sup></a>"##, r##"<a id="fnref{}" href="#fn{}" role="doc-noteref"><sup>{}</sup></a>"##,
number, number, number number, number, number
)?; )?;
} }
Atom::Symbol(sym) => write!(self.out, ":{}:", sym)?, Event::Symbol(sym) => write!(self.out, ":{}:", sym)?,
Atom::LeftSingleQuote => self.out.write_str("&lsquo;")?, Event::LeftSingleQuote => self.out.write_str("&lsquo;")?,
Atom::RightSingleQuote => self.out.write_str("&rsquo;")?, Event::RightSingleQuote => self.out.write_str("&rsquo;")?,
Atom::LeftDoubleQuote => self.out.write_str("&ldquo;")?, Event::LeftDoubleQuote => self.out.write_str("&ldquo;")?,
Atom::RightDoubleQuote => self.out.write_str("&rdquo;")?, Event::RightDoubleQuote => self.out.write_str("&rdquo;")?,
Atom::Ellipsis => self.out.write_str("&hellip;")?, Event::Ellipsis => self.out.write_str("&hellip;")?,
Atom::EnDash => self.out.write_str("&ndash;")?, Event::EnDash => self.out.write_str("&ndash;")?,
Atom::EmDash => self.out.write_str("&mdash;")?, Event::EmDash => self.out.write_str("&mdash;")?,
Atom::NonBreakingSpace => self.out.write_str("&nbsp;")?, Event::NonBreakingSpace => self.out.write_str("&nbsp;")?,
Atom::Hardbreak => self.out.write_str("<br>\n")?, Event::Hardbreak => self.out.write_str("<br>\n")?,
Atom::Softbreak => self.out.write_char('\n')?, Event::Softbreak => self.out.write_char('\n')?,
Atom::Escape | Atom::Blankline => unreachable!("filtered out"), Event::Escape | Event::Blankline => unreachable!("filtered out"),
},
Event::ThematicBreak(attrs) => { Event::ThematicBreak(attrs) => {
self.out.write_str("\n<hr")?; self.out.write_str("\n<hr")?;
for (a, v) in attrs.iter() { for (a, v) in attrs.iter() {

View file

@ -24,8 +24,34 @@ pub enum Event<'s> {
End(Container<'s>), End(Container<'s>),
/// A string object, text only. /// A string object, text only.
Str(CowStr<'s>), Str(CowStr<'s>),
/// An atomic element. /// A footnote reference.
Atom(Atom<'s>), FootnoteReference(&'s str, usize),
/// A symbol, by default rendered literally but may be treated specially.
Symbol(CowStr<'s>),
/// Left single quotation mark.
LeftSingleQuote,
/// Right double quotation mark.
RightSingleQuote,
/// Left single quotation mark.
LeftDoubleQuote,
/// Right double quotation mark.
RightDoubleQuote,
/// A horizontal ellipsis, i.e. a set of three periods.
Ellipsis,
/// An en dash.
EnDash,
/// An em dash.
EmDash,
/// A space that must not break a line.
NonBreakingSpace,
/// A newline that may or may not break a line in the output.
Softbreak,
/// A newline that must break a line in the output.
Hardbreak,
/// An escape character, not visible in output.
Escape,
/// A blank line, not visible in output.
Blankline,
/// A thematic break, typically a horizontal rule. /// A thematic break, typically a horizontal rule.
ThematicBreak(Attributes<'s>), ThematicBreak(Attributes<'s>),
} }
@ -232,38 +258,6 @@ pub enum OrderedListStyle {
ParenParen, ParenParen,
} }
#[derive(Debug, PartialEq, Eq)]
pub enum Atom<'s> {
/// A footnote reference.
FootnoteReference(&'s str, usize),
/// A symbol, by default rendered literally but may be treated specially.
Symbol(CowStr<'s>),
/// Left single quotation mark.
LeftSingleQuote,
/// Right double quotation mark.
RightSingleQuote,
/// Left single quotation mark.
LeftDoubleQuote,
/// Right double quotation mark.
RightDoubleQuote,
/// A horizontal ellipsis, i.e. a set of three periods.
Ellipsis,
/// An en dash.
EnDash,
/// An em dash.
EmDash,
/// A space that must not break a line.
NonBreakingSpace,
/// A newline that may or may not break a line in the output.
Softbreak,
/// A newline that must break a line in the output.
Hardbreak,
/// An escape character, not visible in output.
Escape,
/// A blank line, not visible in output.
Blankline,
}
impl OrderedListNumbering { impl OrderedListNumbering {
fn parse_number(self, n: &str) -> u64 { fn parse_number(self, n: &str) -> u64 {
match self { match self {
@ -631,7 +625,7 @@ impl<'s> Parser<'s> {
Event::End(t) Event::End(t)
} }
} }
inline::EventKind::Atom(a) => Event::Atom(match a { inline::EventKind::Atom(a) => match a {
inline::Atom::FootnoteReference => { inline::Atom::FootnoteReference => {
let tag = match self.inlines.src(inline.span) { let tag = match self.inlines.src(inline.span) {
CowStr::Borrowed(s) => s, CowStr::Borrowed(s) => s,
@ -648,7 +642,7 @@ impl<'s> Parser<'s> {
}, },
|i| i + 1, |i| i + 1,
); );
Atom::FootnoteReference( Event::FootnoteReference(
match self.inlines.src(inline.span) { match self.inlines.src(inline.span) {
CowStr::Borrowed(s) => s, CowStr::Borrowed(s) => s,
CowStr::Owned(..) => panic!(), CowStr::Owned(..) => panic!(),
@ -656,21 +650,21 @@ impl<'s> Parser<'s> {
number, number,
) )
} }
inline::Atom::Symbol => Atom::Symbol(self.inlines.src(inline.span)), inline::Atom::Symbol => Event::Symbol(self.inlines.src(inline.span)),
inline::Atom::Quote { ty, left } => match (ty, left) { inline::Atom::Quote { ty, left } => match (ty, left) {
(inline::QuoteType::Single, true) => Atom::LeftSingleQuote, (inline::QuoteType::Single, true) => Event::LeftSingleQuote,
(inline::QuoteType::Single, false) => Atom::RightSingleQuote, (inline::QuoteType::Single, false) => Event::RightSingleQuote,
(inline::QuoteType::Double, true) => Atom::LeftDoubleQuote, (inline::QuoteType::Double, true) => Event::LeftDoubleQuote,
(inline::QuoteType::Double, false) => Atom::RightDoubleQuote, (inline::QuoteType::Double, false) => Event::RightDoubleQuote,
},
inline::Atom::Ellipsis => Event::Ellipsis,
inline::Atom::EnDash => Event::EnDash,
inline::Atom::EmDash => Event::EmDash,
inline::Atom::Nbsp => Event::NonBreakingSpace,
inline::Atom::Softbreak => Event::Softbreak,
inline::Atom::Hardbreak => Event::Hardbreak,
inline::Atom::Escape => Event::Escape,
}, },
inline::Atom::Ellipsis => Atom::Ellipsis,
inline::Atom::EnDash => Atom::EnDash,
inline::Atom::EmDash => Atom::EmDash,
inline::Atom::Nbsp => Atom::NonBreakingSpace,
inline::Atom::Softbreak => Atom::Softbreak,
inline::Atom::Hardbreak => Atom::Hardbreak,
inline::Atom::Escape => Atom::Escape,
}),
inline::EventKind::Str => Event::Str(self.inlines.src(inline.span)), inline::EventKind::Str => Event::Str(self.inlines.src(inline.span)),
inline::EventKind::Whitespace inline::EventKind::Whitespace
| inline::EventKind::Attributes | inline::EventKind::Attributes
@ -686,7 +680,7 @@ impl<'s> Parser<'s> {
let content = ev.span.of(self.src); let content = ev.span.of(self.src);
let event = match ev.kind { let event = match ev.kind {
tree::EventKind::Atom(a) => match a { tree::EventKind::Atom(a) => match a {
block::Atom::Blankline => Event::Atom(Atom::Blankline), block::Atom::Blankline => Event::Blankline,
block::Atom::ThematicBreak => { block::Atom::ThematicBreak => {
Event::ThematicBreak(self.block_attributes.take()) Event::ThematicBreak(self.block_attributes.take())
} }
@ -853,7 +847,6 @@ impl<'s> Iterator for Parser<'s> {
#[cfg(test)] #[cfg(test)]
mod test { mod test {
use super::Atom::*;
use super::Attributes; use super::Attributes;
use super::Container::*; use super::Container::*;
use super::Event::*; use super::Event::*;
@ -949,7 +942,7 @@ mod test {
Attributes::new() Attributes::new()
), ),
Str("abc".into()), Str("abc".into()),
Atom(Softbreak), Softbreak,
Str("def".into()), Str("def".into()),
End(Heading { End(Heading {
level: 1, level: 1,
@ -1013,7 +1006,7 @@ mod test {
test_parse!( test_parse!(
">\n", ">\n",
Start(Blockquote, Attributes::new()), Start(Blockquote, Attributes::new()),
Atom(Blankline), Blankline,
End(Blockquote), End(Blockquote),
); );
} }
@ -1037,7 +1030,7 @@ mod test {
Start(Paragraph, Attributes::new()), Start(Paragraph, Attributes::new()),
Str("para0".into()), Str("para0".into()),
End(Paragraph), End(Paragraph),
Atom(Blankline), Blankline,
Start(Paragraph, Attributes::new()), Start(Paragraph, Attributes::new()),
Str("para1".into()), Str("para1".into()),
End(Paragraph), End(Paragraph),
@ -1097,7 +1090,7 @@ mod test {
"abc :+1: def", "abc :+1: def",
Start(Paragraph, Attributes::new()), Start(Paragraph, Attributes::new()),
Str("abc ".into()), Str("abc ".into()),
Atom(Symbol("+1".into())), Symbol("+1".into()),
Str(" def".into()), Str(" def".into()),
End(Paragraph), End(Paragraph),
); );
@ -1150,7 +1143,7 @@ mod test {
Str("text".into()), Str("text".into()),
End(Link("url".into(), LinkType::Span(SpanLinkType::Reference))), End(Link("url".into(), LinkType::Span(SpanLinkType::Reference))),
End(Paragraph), End(Paragraph),
Atom(Blankline), Blankline,
); );
test_parse!( test_parse!(
concat!( concat!(
@ -1166,7 +1159,7 @@ mod test {
Str("text".into()), Str("text".into()),
End(Image("url".into(), SpanLinkType::Reference)), End(Image("url".into(), SpanLinkType::Reference)),
End(Paragraph), End(Paragraph),
Atom(Blankline), Blankline,
); );
} }
@ -1187,7 +1180,7 @@ mod test {
Str("text".into()), Str("text".into()),
End(Link("url".into(), LinkType::Span(SpanLinkType::Reference))), End(Link("url".into(), LinkType::Span(SpanLinkType::Reference))),
End(Paragraph), End(Paragraph),
Atom(Blankline), Blankline,
); );
test_parse!( test_parse!(
concat!( concat!(
@ -1204,7 +1197,7 @@ mod test {
Str("text".into()), Str("text".into()),
End(Link("url".into(), LinkType::Span(SpanLinkType::Reference))), End(Link("url".into(), LinkType::Span(SpanLinkType::Reference))),
End(Paragraph), End(Paragraph),
Atom(Blankline), Blankline,
); );
} }
@ -1226,7 +1219,7 @@ mod test {
Str("text".into()), Str("text".into()),
End(Link("url".into(), LinkType::Span(SpanLinkType::Reference))), End(Link("url".into(), LinkType::Span(SpanLinkType::Reference))),
End(Paragraph), End(Paragraph),
Atom(Blankline), Blankline,
Start(Paragraph, Attributes::new()), Start(Paragraph, Attributes::new()),
Str("para".into()), Str("para".into()),
End(Paragraph), End(Paragraph),
@ -1238,9 +1231,9 @@ mod test {
test_parse!( test_parse!(
"[^a][^b][^c]", "[^a][^b][^c]",
Start(Paragraph, Attributes::new()), Start(Paragraph, Attributes::new()),
Atom(FootnoteReference("a", 1)), FootnoteReference("a", 1),
Atom(FootnoteReference("b", 2)), FootnoteReference("b", 2),
Atom(FootnoteReference("c", 3)), FootnoteReference("c", 3),
End(Paragraph), End(Paragraph),
Start( Start(
Footnote { Footnote {
@ -1283,9 +1276,9 @@ mod test {
test_parse!( test_parse!(
"[^a]\n\n[^a]: a\n", "[^a]\n\n[^a]: a\n",
Start(Paragraph, Attributes::new()), Start(Paragraph, Attributes::new()),
Atom(FootnoteReference("a", 1)), FootnoteReference("a", 1),
End(Paragraph), End(Paragraph),
Atom(Blankline), Blankline,
Start( Start(
Footnote { Footnote {
tag: "a", tag: "a",
@ -1314,9 +1307,9 @@ mod test {
" def", // " def", //
), ),
Start(Paragraph, Attributes::new()), Start(Paragraph, Attributes::new()),
Atom(FootnoteReference("a", 1)), FootnoteReference("a", 1),
End(Paragraph), End(Paragraph),
Atom(Blankline), Blankline,
Start( Start(
Footnote { Footnote {
tag: "a", tag: "a",
@ -1327,7 +1320,7 @@ mod test {
Start(Paragraph, Attributes::new()), Start(Paragraph, Attributes::new()),
Str("abc".into()), Str("abc".into()),
End(Paragraph), End(Paragraph),
Atom(Blankline), Blankline,
Start(Paragraph, Attributes::new()), Start(Paragraph, Attributes::new()),
Str("def".into()), Str("def".into()),
End(Paragraph), End(Paragraph),
@ -1348,9 +1341,9 @@ mod test {
"para\n", // "para\n", //
), ),
Start(Paragraph, Attributes::new()), Start(Paragraph, Attributes::new()),
Atom(FootnoteReference("a", 1)), FootnoteReference("a", 1),
End(Paragraph), End(Paragraph),
Atom(Blankline), Blankline,
Start(Paragraph, Attributes::new()), Start(Paragraph, Attributes::new()),
Str("para".into()), Str("para".into()),
End(Paragraph), End(Paragraph),