Skip to content

Commit

Permalink
rustdoc: update pulldown-cmark to 0.10
Browse files Browse the repository at this point in the history
  • Loading branch information
stepantubanov committed Mar 24, 2024
1 parent e50ab29 commit c3d3e1d
Show file tree
Hide file tree
Showing 8 changed files with 83 additions and 88 deletions.
15 changes: 2 additions & 13 deletions Cargo.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2378,7 +2378,7 @@ dependencies = [
"memchr",
"once_cell",
"opener",
"pulldown-cmark 0.10.0",
"pulldown-cmark",
"regex",
"serde",
"serde_json",
Expand Down Expand Up @@ -3049,17 +3049,6 @@ dependencies = [
"cc",
]

[[package]]
name = "pulldown-cmark"
version = "0.9.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "57206b407293d2bcd3af849ce869d52068623f19e1b5ff8e8778e3309439682b"
dependencies = [
"bitflags 2.4.2",
"memchr",
"unicase",
]

[[package]]
name = "pulldown-cmark"
version = "0.10.0"
Expand Down Expand Up @@ -4526,7 +4515,7 @@ name = "rustc_resolve"
version = "0.0.0"
dependencies = [
"bitflags 2.4.2",
"pulldown-cmark 0.9.6",
"pulldown-cmark",
"rustc_arena",
"rustc_ast",
"rustc_ast_pretty",
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_resolve/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ edition = "2021"
[dependencies]
# tidy-alphabetical-start
bitflags = "2.4.1"
pulldown-cmark = { version = "0.9.6", default-features = false }
pulldown-cmark = { version = "0.10", default-features = false, features = ["html"] }
rustc_arena = { path = "../rustc_arena" }
rustc_ast = { path = "../rustc_ast" }
rustc_ast_pretty = { path = "../rustc_ast_pretty" }
Expand Down
15 changes: 9 additions & 6 deletions compiler/rustc_resolve/src/rustdoc.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
use pulldown_cmark::{BrokenLink, CowStr, Event, LinkType, Options, Parser, Tag};
use pulldown_cmark::{
BrokenLink, BrokenLinkCallback, CowStr, Event, LinkType, Options, Parser, Tag,
};
use rustc_ast as ast;
use rustc_ast::util::comments::beautify_doc_string;
use rustc_data_structures::fx::FxHashMap;
Expand Down Expand Up @@ -413,7 +415,7 @@ fn parse_links<'md>(doc: &'md str) -> Vec<Box<str>> {

while let Some(event) = event_iter.next() {
match event {
Event::Start(Tag::Link(link_type, dest, _)) if may_be_doc_link(link_type) => {
Event::Start(Tag::Link { link_type, dest_url, .. }) if may_be_doc_link(link_type) => {
if matches!(
link_type,
LinkType::Inline
Expand All @@ -427,7 +429,7 @@ fn parse_links<'md>(doc: &'md str) -> Vec<Box<str>> {
}
}

links.push(preprocess_link(&dest));
links.push(preprocess_link(&dest_url));
}
_ => {}
}
Expand All @@ -437,9 +439,10 @@ fn parse_links<'md>(doc: &'md str) -> Vec<Box<str>> {
}

/// Collects additional data of link.
fn collect_link_data<'input, 'callback>(
event_iter: &mut Parser<'input, 'callback>,
) -> Option<Box<str>> {
fn collect_link_data<'input, F>(event_iter: &mut Parser<'input, F>) -> Option<Box<str>>
where
F: BrokenLinkCallback<'input>,
{
let mut display_text: Option<String> = None;
let mut append_text = |text: CowStr<'_>| {
if let Some(display_text) = &mut display_text {
Expand Down
112 changes: 54 additions & 58 deletions src/librustdoc/html/markdown.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@ use crate::html::render::small_url_encode;
use crate::html::toc::TocBuilder;

use pulldown_cmark::{
html, BrokenLink, CodeBlockKind, CowStr, Event, LinkType, OffsetIter, Options, Parser, Tag,
html, BrokenLink, BrokenLinkCallback, CodeBlockKind, CowStr, Event, LinkType, OffsetIter,
Options, Parser, Tag, TagEnd,
};

#[cfg(test)]
Expand Down Expand Up @@ -242,7 +243,7 @@ impl<'a, I: Iterator<Item = Event<'a>>> Iterator for CodeBlocks<'_, 'a, I> {
let mut original_text = String::new();
for event in &mut self.inner {
match event {
Event::End(Tag::CodeBlock(..)) => break,
Event::End(TagEnd::CodeBlock) => break,
Event::Text(ref s) => {
original_text.push_str(s);
}
Expand Down Expand Up @@ -368,20 +369,20 @@ impl<'a, I: Iterator<Item = Event<'a>>> Iterator for LinkReplacer<'a, I> {
match &mut event {
// This is a shortcut link that was resolved by the broken_link_callback: `[fn@f]`
// Remove any disambiguator.
Some(Event::Start(Tag::Link(
Some(Event::Start(Tag::Link {
// [fn@f] or [fn@f][]
LinkType::ShortcutUnknown | LinkType::CollapsedUnknown,
dest,
link_type: LinkType::ShortcutUnknown | LinkType::CollapsedUnknown,
dest_url,
title,
))) => {
debug!("saw start of shortcut link to {dest} with title {title}");
..
})) => {
debug!("saw start of shortcut link to {dest_url} with title {title}");
// If this is a shortcut link, it was resolved by the broken_link_callback.
// So the URL will already be updated properly.
let link = self.links.iter().find(|&link| *link.href == **dest);
let link = self.links.iter().find(|&link| *link.href == **dest_url);
// Since this is an external iterator, we can't replace the inner text just yet.
// Store that we saw a link so we know to replace it later.
if let Some(link) = link {
trace!("it matched");
assert!(self.shortcut_link.is_none(), "shortcut links cannot be nested");
self.shortcut_link = Some(link);
if title.is_empty() && !link.tooltip.is_empty() {
Expand All @@ -390,21 +391,14 @@ impl<'a, I: Iterator<Item = Event<'a>>> Iterator for LinkReplacer<'a, I> {
}
}
// Now that we're done with the shortcut link, don't replace any more text.
Some(Event::End(Tag::Link(
LinkType::ShortcutUnknown | LinkType::CollapsedUnknown,
dest,
_,
))) => {
debug!("saw end of shortcut link to {dest}");
if self.links.iter().any(|link| *link.href == **dest) {
assert!(self.shortcut_link.is_some(), "saw closing link without opening tag");
self.shortcut_link = None;
Some(Event::End(TagEnd::Link)) => {
if let Some(link) = self.shortcut_link.take() {
debug!("saw end of shortcut link to {}", link.href);
}
}
// Handle backticks in inline code blocks, but only if we're in the middle of a shortcut link.
// [`fn@f`]
Some(Event::Code(text)) => {
trace!("saw code {text}");
if let Some(link) = self.shortcut_link {
// NOTE: this only replaces if the code block is the *entire* text.
// If only part of the link has code highlighting, the disambiguator will not be removed.
Expand All @@ -427,7 +421,6 @@ impl<'a, I: Iterator<Item = Event<'a>>> Iterator for LinkReplacer<'a, I> {
// Replace plain text in links, but only in the middle of a shortcut link.
// [fn@f]
Some(Event::Text(text)) => {
trace!("saw text {text}");
if let Some(link) = self.shortcut_link {
// NOTE: same limitations as `Event::Code`
if let Some(link) = self
Expand All @@ -442,7 +435,8 @@ impl<'a, I: Iterator<Item = Event<'a>>> Iterator for LinkReplacer<'a, I> {
}
// If this is a link, but not a shortcut link,
// replace the URL, since the broken_link_callback was not called.
Some(Event::Start(Tag::Link(_, dest, title))) => {
Some(Event::Start(Tag::Link { dest_url: dest, title, .. })) => {
assert!(self.shortcut_link.is_none(), "links cannot be nested");
if let Some(link) = self.links.iter().find(|&link| *link.original_text == **dest) {
*dest = CowStr::Borrowed(link.href.as_ref());
if title.is_empty() && !link.tooltip.is_empty() {
Expand Down Expand Up @@ -486,9 +480,9 @@ impl<'a, I: Iterator<Item = Event<'a>>> Iterator for TableWrapper<'a, I> {
self.stored_events.push_back(Event::Start(Tag::Table(t)));
Event::Html(CowStr::Borrowed("<div>"))
}
Event::End(Tag::Table(t)) => {
Event::End(TagEnd::Table) => {
self.stored_events.push_back(Event::Html(CowStr::Borrowed("</div>")));
Event::End(Tag::Table(t))
Event::End(TagEnd::Table)
}
e => e,
})
Expand Down Expand Up @@ -528,11 +522,11 @@ impl<'a, 'b, 'ids, I: Iterator<Item = SpannedEvent<'a>>> Iterator
}

let event = self.inner.next();
if let Some((Event::Start(Tag::Heading(level, _, _)), _)) = event {
if let Some((Event::Start(Tag::Heading { level, .. }), _)) = event {
let mut id = String::new();
for event in &mut self.inner {
match &event.0 {
Event::End(Tag::Heading(..)) => break,
Event::End(TagEnd::Heading(..)) => break,
Event::Text(text) | Event::Code(text) => {
id.extend(text.chars().filter_map(slugify));
self.buf.push_back(event);
Expand Down Expand Up @@ -575,27 +569,27 @@ impl<'a, I: Iterator<Item = Event<'a>>> SummaryLine<'a, I> {
}
}

fn check_if_allowed_tag(t: &Tag<'_>) -> bool {
fn check_if_allowed_tag(t: TagEnd) -> bool {
matches!(
t,
Tag::Paragraph
| Tag::Emphasis
| Tag::Strong
| Tag::Strikethrough
| Tag::Link(..)
| Tag::BlockQuote
TagEnd::Paragraph
| TagEnd::Emphasis
| TagEnd::Strong
| TagEnd::Strikethrough
| TagEnd::Link
| TagEnd::BlockQuote
)
}

fn is_forbidden_tag(t: &Tag<'_>) -> bool {
fn is_forbidden_tag(t: TagEnd) -> bool {
matches!(
t,
Tag::CodeBlock(_)
| Tag::Table(_)
| Tag::TableHead
| Tag::TableRow
| Tag::TableCell
| Tag::FootnoteDefinition(_)
TagEnd::CodeBlock
| TagEnd::Table
| TagEnd::TableHead
| TagEnd::TableRow
| TagEnd::TableCell
| TagEnd::FootnoteDefinition
)
}

Expand All @@ -613,14 +607,15 @@ impl<'a, I: Iterator<Item = Event<'a>>> Iterator for SummaryLine<'a, I> {
let mut is_start = true;
let is_allowed_tag = match event {
Event::Start(ref c) => {
if is_forbidden_tag(c) {
let end_tag = c.to_end();
if is_forbidden_tag(end_tag) {
self.skipped_tags += 1;
return None;
}
self.depth += 1;
check_if_allowed_tag(c)
check_if_allowed_tag(end_tag)
}
Event::End(ref c) => {
Event::End(c) => {
if is_forbidden_tag(c) {
self.skipped_tags += 1;
return None;
Expand All @@ -642,7 +637,7 @@ impl<'a, I: Iterator<Item = Event<'a>>> Iterator for SummaryLine<'a, I> {
if is_start {
Some(Event::Start(Tag::Paragraph))
} else {
Some(Event::End(Tag::Paragraph))
Some(Event::End(TagEnd::Paragraph))
}
} else {
Some(event)
Expand Down Expand Up @@ -688,7 +683,7 @@ impl<'a, I: Iterator<Item = SpannedEvent<'a>>> Iterator for Footnotes<'a, I> {
Some((Event::Start(Tag::FootnoteDefinition(def)), _)) => {
let mut content = Vec::new();
for (event, _) in &mut self.inner {
if let Event::End(Tag::FootnoteDefinition(..)) = event {
if let Event::End(TagEnd::FootnoteDefinition) = event {
break;
}
content.push(event);
Expand All @@ -705,7 +700,7 @@ impl<'a, I: Iterator<Item = SpannedEvent<'a>>> Iterator for Footnotes<'a, I> {
for (mut content, id) in v {
write!(ret, "<li id=\"fn{id}\">").unwrap();
let mut is_paragraph = false;
if let Some(&Event::End(Tag::Paragraph)) = content.last() {
if let Some(&Event::End(TagEnd::Paragraph)) = content.last() {
content.pop();
is_paragraph = true;
}
Expand Down Expand Up @@ -804,7 +799,7 @@ pub(crate) fn find_codes<T: doctest::Tester>(
tests.add_test(text, block_info, line);
prev_offset = offset.start;
}
Event::Start(Tag::Heading(level, _, _)) => {
Event::Start(Tag::Heading { level, .. }) => {
register_header = Some(level as u32);
}
Event::Text(ref s) if register_header.is_some() => {
Expand Down Expand Up @@ -1488,7 +1483,7 @@ impl MarkdownItemInfo<'_> {
let p = Footnotes::new(p);
let p = TableWrapper::new(p.map(|(ev, _)| ev));
let p = p.filter(|event| {
!matches!(event, Event::Start(Tag::Paragraph) | Event::End(Tag::Paragraph))
!matches!(event, Event::Start(Tag::Paragraph) | Event::End(TagEnd::Paragraph))
});
html::push_html(&mut s, p);

Expand Down Expand Up @@ -1518,7 +1513,7 @@ impl MarkdownSummaryLine<'_> {
let mut s = String::new();

let without_paragraphs = LinkReplacer::new(&mut summary, links).filter(|event| {
!matches!(event, Event::Start(Tag::Paragraph) | Event::End(Tag::Paragraph))
!matches!(event, Event::Start(Tag::Paragraph) | Event::End(TagEnd::Paragraph))
});

html::push_html(&mut s, without_paragraphs);
Expand Down Expand Up @@ -1590,8 +1585,8 @@ fn markdown_summary_with_limit(
_ => {}
},
Event::End(tag) => match tag {
Tag::Emphasis | Tag::Strong => buf.close_tag(),
Tag::Paragraph | Tag::Heading(..) => return ControlFlow::Break(()),
TagEnd::Emphasis | TagEnd::Strong => buf.close_tag(),
TagEnd::Paragraph | TagEnd::Heading(..) => return ControlFlow::Break(()),
_ => {}
},
Event::HardBreak | Event::SoftBreak => buf.push(" ")?,
Expand Down Expand Up @@ -1651,8 +1646,8 @@ pub(crate) fn plain_text_summary(md: &str, link_names: &[RenderedLink]) -> Strin
}
Event::HardBreak | Event::SoftBreak => s.push(' '),
Event::Start(Tag::CodeBlock(..)) => break,
Event::End(Tag::Paragraph) => break,
Event::End(Tag::Heading(..)) => break,
Event::End(TagEnd::Paragraph) => break,
Event::End(TagEnd::Heading(..)) => break,
_ => (),
}
}
Expand Down Expand Up @@ -1811,7 +1806,7 @@ pub(crate) fn markdown_links<'md, R>(

while let Some((event, span)) = event_iter.next() {
match event {
Event::Start(Tag::Link(link_type, dest, _)) if may_be_doc_link(link_type) => {
Event::Start(Tag::Link { link_type, dest_url, .. }) if may_be_doc_link(link_type) => {
let range = match link_type {
// Link is pulled from the link itself.
LinkType::ReferenceUnknown | LinkType::ShortcutUnknown => {
Expand All @@ -1821,7 +1816,7 @@ pub(crate) fn markdown_links<'md, R>(
LinkType::Inline => span_for_offset_backward(span, b'(', b')'),
// Link is pulled from elsewhere in the document.
LinkType::Reference | LinkType::Collapsed | LinkType::Shortcut => {
span_for_link(&dest, span)
span_for_link(&dest_url, span)
}
LinkType::Autolink | LinkType::Email => unreachable!(),
};
Expand All @@ -1841,7 +1836,7 @@ pub(crate) fn markdown_links<'md, R>(

if let Some(link) = preprocess_link(MarkdownLink {
kind: link_type,
link: dest.into_string(),
link: dest_url.into_string(),
display_text,
range,
}) {
Expand All @@ -1856,9 +1851,10 @@ pub(crate) fn markdown_links<'md, R>(
}

/// Collects additional data of link.
fn collect_link_data<'input, 'callback>(
event_iter: &mut OffsetIter<'input, 'callback>,
) -> Option<String> {
fn collect_link_data<'input, F>(event_iter: &mut OffsetIter<'input, F>) -> Option<String>
where
F: BrokenLinkCallback<'input>,
{
let mut display_text: Option<String> = None;
let mut append_text = |text: CowStr<'_>| {
if let Some(display_text) = &mut display_text {
Expand Down
5 changes: 3 additions & 2 deletions src/librustdoc/passes/lint/bare_urls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,12 @@ pub(super) fn visit_item(cx: &DocContext<'_>, item: &Item) {
match event {
Event::Text(s) => find_raw_urls(cx, &s, range, &report_diag),
// We don't want to check the text inside code blocks or links.
Event::Start(tag @ (Tag::CodeBlock(_) | Tag::Link(..))) => {
Event::Start(tag @ (Tag::CodeBlock(_) | Tag::Link { .. })) => {
let tag_end = tag.to_end();
while let Some((event, _)) = p.next() {
match event {
Event::End(end)
if mem::discriminant(&end) == mem::discriminant(&tag) =>
if mem::discriminant(&end) == mem::discriminant(&tag_end) =>
{
break;
}
Expand Down
Loading

0 comments on commit c3d3e1d

Please sign in to comment.