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

View file

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