lib: emit LinkDefinition event

resolves #14
This commit is contained in:
Noah Hellman 2023-04-07 15:33:45 +02:00
parent 17b166867f
commit 8e48021f7a
2 changed files with 47 additions and 9 deletions

View file

@ -30,6 +30,7 @@ pub struct Renderer {
footnote_number: Option<std::num::NonZeroUsize>,
not_first_line: bool,
close_para: bool,
ignore: bool,
}
impl Render for Renderer {
@ -41,6 +42,20 @@ impl Render for Renderer {
return Ok(());
}
if matches!(&e, Event::Start(Container::LinkDefinition { .. }, ..)) {
self.ignore = true;
return Ok(());
}
if matches!(&e, Event::End(Container::LinkDefinition { .. })) {
self.ignore = false;
return Ok(());
}
if self.ignore {
return Ok(());
}
let close_para = self.close_para;
if close_para {
self.close_para = false;
@ -151,6 +166,7 @@ impl Render for Renderer {
Container::Strong => out.write_str("<strong")?,
Container::Emphasis => out.write_str("<em")?,
Container::Mark => out.write_str("<mark")?,
Container::LinkDefinition { .. } => return Ok(()),
}
for (a, v) in attrs.iter().filter(|(a, _)| *a != "class") {
@ -343,6 +359,7 @@ impl Render for Renderer {
Container::Strong => out.write_str("</strong>")?,
Container::Emphasis => out.write_str("</em>")?,
Container::Mark => out.write_str("</mark>")?,
Container::LinkDefinition { .. } => unreachable!(),
}
}
Event::Str(s) => match self.raw {

View file

@ -327,6 +327,8 @@ pub enum Container<'s> {
Caption,
/// A term within a description list.
DescriptionTerm,
/// A link definition.
LinkDefinition { label: &'s str },
/// A block with raw markup for a specific output format.
RawBlock { format: &'s str },
/// A block with code in a specific language.
@ -381,6 +383,7 @@ impl<'s> Container<'s> {
| Self::TableCell { .. }
| Self::Caption
| Self::DescriptionTerm
| Self::LinkDefinition { .. }
| Self::RawBlock { .. }
| Self::CodeBlock { .. } => true,
Self::Span
@ -419,6 +422,7 @@ impl<'s> Container<'s> {
| Self::TableCell { .. }
| Self::Caption
| Self::DescriptionTerm
| Self::LinkDefinition { .. }
| Self::RawBlock { .. }
| Self::CodeBlock { .. }
| Self::Span
@ -941,14 +945,6 @@ impl<'s> Parser<'s> {
let cont = match c {
block::Node::Leaf(l) => {
self.inline_parser.reset();
if matches!(l, block::Leaf::LinkDefinition) {
// ignore link definitions
if enter {
self.tree.take_inlines().last();
}
self.block_attributes = Attributes::new();
continue;
}
match l {
block::Leaf::Paragraph => Container::Paragraph,
block::Leaf::Heading { has_section } => Container::Heading {
@ -977,7 +973,9 @@ impl<'s> Parser<'s> {
head: self.table_head_row,
},
block::Leaf::Caption => Container::Caption,
block::Leaf::LinkDefinition => unreachable!(),
block::Leaf::LinkDefinition => {
Container::LinkDefinition { label: content }
}
}
}
block::Node::Container(c) => match c {
@ -1418,6 +1416,9 @@ mod test {
End(Link("url".into(), LinkType::Span(SpanLinkType::Reference))),
End(Paragraph),
Blankline,
Start(LinkDefinition { label: "tag" }, Attributes::new()),
Str("url".into()),
End(LinkDefinition { label: "tag" }),
);
test_parse!(
concat!(
@ -1434,6 +1435,9 @@ mod test {
End(Image("url".into(), SpanLinkType::Reference)),
End(Paragraph),
Blankline,
Start(LinkDefinition { label: "tag" }, Attributes::new()),
Str("url".into()),
End(LinkDefinition { label: "tag" }),
);
}
@ -1483,6 +1487,9 @@ mod test {
End(Paragraph),
End(Blockquote),
Blankline,
Start(LinkDefinition { label: "a b" }, Attributes::new()),
Str("url".into()),
End(LinkDefinition { label: "a b" }),
);
}
@ -1504,6 +1511,11 @@ mod test {
End(Link("url".into(), LinkType::Span(SpanLinkType::Reference))),
End(Paragraph),
Blankline,
Start(LinkDefinition { label: "tag" }, Attributes::new()),
Str("u".into()),
Softbreak,
Str("rl".into()),
End(LinkDefinition { label: "tag" }),
);
test_parse!(
concat!(
@ -1521,6 +1533,9 @@ mod test {
End(Link("url".into(), LinkType::Span(SpanLinkType::Reference))),
End(Paragraph),
Blankline,
Start(LinkDefinition { label: "tag" }, Attributes::new()),
Str("url".into()),
End(LinkDefinition { label: "tag" }),
);
}
@ -1543,6 +1558,12 @@ mod test {
End(Link("url".into(), LinkType::Span(SpanLinkType::Reference))),
End(Paragraph),
Blankline,
Start(
LinkDefinition { label: "tag" },
[("a", "b")].into_iter().collect()
),
Str("url".into()),
End(LinkDefinition { label: "tag" }),
Start(Paragraph, Attributes::new()),
Str("para".into()),
End(Paragraph),