Skip to content

Commit

Permalink
lib: emit Attributes event for dangling attrs
Browse files Browse the repository at this point in the history
  • Loading branch information
hellux committed Aug 10, 2024
1 parent 6f60dc3 commit 5a7138a
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 13 deletions.
2 changes: 1 addition & 1 deletion src/html.rs
Original file line number Diff line number Diff line change
Expand Up @@ -406,7 +406,7 @@ impl<'s> Writer<'s> {
Event::NonBreakingSpace => out.write_str("&nbsp;")?,
Event::Hardbreak => out.write_str("<br>\n")?,
Event::Softbreak => out.write_char('\n')?,
Event::Escape | Event::Blankline => {}
Event::Escape | Event::Blankline | Event::Attributes(..) => {}
Event::ThematicBreak(attrs) => {
if self.not_first_line {
out.write_char('\n')?;
Expand Down
93 changes: 81 additions & 12 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,8 @@ pub enum Event<'s> {
Blankline,
/// A thematic break, typically a horizontal rule.
ThematicBreak(Attributes<'s>),
/// Dangling attributes not attached to anything.
Attributes(Attributes<'s>),
}

/// A container that may contain other elements.
Expand Down Expand Up @@ -552,9 +554,9 @@ pub struct Parser<'s> {
/// Contents obtained by the prepass.
pre_pass: PrePass<'s>,

/// Last parsed block attributes, and its starting offset.
/// Last parsed block attributes, and its span.
block_attributes: Attributes<'s>,
block_attributes_pos: Option<usize>,
block_attributes_span: Option<Range<usize>>,

/// Current table row is a head row.
table_head_row: bool,
Expand Down Expand Up @@ -789,7 +791,7 @@ impl<'s> Parser<'s> {
blocks: blocks.into_iter().peekable(),
pre_pass,
block_attributes: Attributes::new(),
block_attributes_pos: None,
block_attributes_span: None,
table_head_row: false,
verbatim: false,
inline_parser,
Expand Down Expand Up @@ -1005,7 +1007,7 @@ impl<'s> Parser<'s> {
},
inline::EventKind::Empty => {
debug_assert!(!attributes.is_empty());
todo!()
Event::Attributes(attributes.take())
}
inline::EventKind::Str => Event::Str(self.src[inline.span.clone()].into()),
inline::EventKind::Attributes { .. } | inline::EventKind::Placeholder => {
Expand All @@ -1031,14 +1033,14 @@ impl<'s> Parser<'s> {
block::EventKind::Atom(a) => match a {
block::Atom::Blankline => Event::Blankline,
block::Atom::ThematicBreak => {
if let Some(pos) = self.block_attributes_pos.take() {
ev.span.start = pos;
if let Some(span) = self.block_attributes_span.take() {
ev.span.start = span.start;
}
Event::ThematicBreak(self.block_attributes.take())
}
block::Atom::Attributes => {
if self.block_attributes_pos.is_none() {
self.block_attributes_pos = Some(ev.span.start);
if self.block_attributes_span.is_none() {
self.block_attributes_span = Some(ev.span.clone());
}
self.block_attributes.parse(&self.src[ev.span.clone()]);
continue;
Expand Down Expand Up @@ -1134,13 +1136,13 @@ impl<'s> Parser<'s> {
},
};
if enter {
if let Some(pos) = self.block_attributes_pos.take() {
ev.span.start = pos;
if let Some(span) = self.block_attributes_span.take() {
ev.span.start = span.start;
}
Event::Start(cont, self.block_attributes.take())
} else {
self.block_attributes = Attributes::new();
self.block_attributes_pos = None;
self.block_attributes_span = None;
Event::End(cont)
}
}
Expand All @@ -1166,7 +1168,11 @@ impl<'s> Parser<'s> {
}

fn next_span(&mut self) -> Option<(Event<'s>, Range<usize>)> {
self.inline().or_else(|| self.block())
self.inline().or_else(|| self.block()).or_else(|| {
self.block_attributes_span
.take()
.map(|span| (Event::Attributes(self.block_attributes.take()), span))
})
}
}

Expand Down Expand Up @@ -2042,6 +2048,35 @@ mod test {
Str("para".into()),
End(Paragraph),
);
test_parse!(
"{.a}\n\n{.b}\n\npara\n",
Blankline,
Blankline,
Start(
Paragraph,
[(AttributeKey::Class, "a"), (AttributeKey::Class, "b")]
.into_iter()
.collect()
),
Str("para".into()),
End(Paragraph),
);
}

#[test]
fn attr_block_dangling() {
test_parse!(
"{.a}",
Attributes([(AttributeKey::Class, "a")].into_iter().collect()),
);
test_parse!(
"para\n\n{.a}",
Start(Paragraph, Attributes::new()),
Str("para".into()),
End(Paragraph),
Blankline,
Attributes([(AttributeKey::Class, "a")].into_iter().collect()),
);
}

#[test]
Expand Down Expand Up @@ -2268,6 +2303,40 @@ mod test {
);
}

#[test]
fn attr_inline_dangling() {
test_parse!(
"a {.b} c",
Start(Paragraph, Attributes::new()),
Str("a ".into()),
Attributes([(AttributeKey::Class, "b")].into_iter().collect()),
Str(" c".into()),
End(Paragraph),
);
test_parse!(
"a {%cmt} c",
Start(Paragraph, Attributes::new()),
Str("a ".into()),
Attributes([(AttributeKey::Comment, "cmt")].into_iter().collect()),
Str(" c".into()),
End(Paragraph),
);
test_parse!(
"a {%cmt}",
Start(Paragraph, Attributes::new()),
Str("a ".into()),
Attributes([(AttributeKey::Comment, "cmt")].into_iter().collect()),
End(Paragraph),
);
test_parse!(
"{%cmt} a",
Start(Paragraph, Attributes::new()),
Attributes([(AttributeKey::Comment, "cmt")].into_iter().collect()),
Str(" a".into()),
End(Paragraph),
);
}

#[test]
fn list_item_unordered() {
test_parse!(
Expand Down

0 comments on commit 5a7138a

Please sign in to comment.