Skip to content

Commit 11de52a

Browse files
authored
Rollup merge of #105780 - GuillaumeGomez:read-more-links, r=notriddle
rustdoc: Don't add "Read more" link if there is no extra content Fixes #105677.
2 parents 81c1189 + 80059e1 commit 11de52a

File tree

4 files changed

+58
-10
lines changed

4 files changed

+58
-10
lines changed

src/librustdoc/html/markdown.rs

+21-6
Original file line numberDiff line numberDiff line change
@@ -567,11 +567,12 @@ struct SummaryLine<'a, I: Iterator<Item = Event<'a>>> {
567567
inner: I,
568568
started: bool,
569569
depth: u32,
570+
skipped_tags: u32,
570571
}
571572

572573
impl<'a, I: Iterator<Item = Event<'a>>> SummaryLine<'a, I> {
573574
fn new(iter: I) -> Self {
574-
SummaryLine { inner: iter, started: false, depth: 0 }
575+
SummaryLine { inner: iter, started: false, depth: 0, skipped_tags: 0 }
575576
}
576577
}
577578

@@ -601,13 +602,15 @@ impl<'a, I: Iterator<Item = Event<'a>>> Iterator for SummaryLine<'a, I> {
601602
let is_allowed_tag = match event {
602603
Event::Start(ref c) => {
603604
if is_forbidden_tag(c) {
605+
self.skipped_tags += 1;
604606
return None;
605607
}
606608
self.depth += 1;
607609
check_if_allowed_tag(c)
608610
}
609611
Event::End(ref c) => {
610612
if is_forbidden_tag(c) {
613+
self.skipped_tags += 1;
611614
return None;
612615
}
613616
self.depth -= 1;
@@ -616,6 +619,9 @@ impl<'a, I: Iterator<Item = Event<'a>>> Iterator for SummaryLine<'a, I> {
616619
}
617620
_ => true,
618621
};
622+
if !is_allowed_tag {
623+
self.skipped_tags += 1;
624+
}
619625
return if !is_allowed_tag {
620626
if is_start {
621627
Some(Event::Start(Tag::Paragraph))
@@ -1096,11 +1102,11 @@ impl MarkdownItemInfo<'_> {
10961102
}
10971103

10981104
impl MarkdownSummaryLine<'_> {
1099-
pub(crate) fn into_string(self) -> String {
1105+
pub(crate) fn into_string_with_has_more_content(self) -> (String, bool) {
11001106
let MarkdownSummaryLine(md, links) = self;
11011107
// This is actually common enough to special-case
11021108
if md.is_empty() {
1103-
return String::new();
1109+
return (String::new(), false);
11041110
}
11051111

11061112
let mut replacer = |broken_link: BrokenLink<'_>| {
@@ -1110,17 +1116,26 @@ impl MarkdownSummaryLine<'_> {
11101116
.map(|link| (link.href.as_str().into(), link.new_text.as_str().into()))
11111117
};
11121118

1113-
let p = Parser::new_with_broken_link_callback(md, summary_opts(), Some(&mut replacer));
1119+
let p = Parser::new_with_broken_link_callback(md, summary_opts(), Some(&mut replacer))
1120+
.peekable();
1121+
let mut summary = SummaryLine::new(p);
11141122

11151123
let mut s = String::new();
11161124

1117-
let without_paragraphs = LinkReplacer::new(SummaryLine::new(p), links).filter(|event| {
1125+
let without_paragraphs = LinkReplacer::new(&mut summary, links).filter(|event| {
11181126
!matches!(event, Event::Start(Tag::Paragraph) | Event::End(Tag::Paragraph))
11191127
});
11201128

11211129
html::push_html(&mut s, without_paragraphs);
11221130

1123-
s
1131+
let has_more_content =
1132+
matches!(summary.inner.peek(), Some(Event::Start(_))) || summary.skipped_tags > 0;
1133+
1134+
(s, has_more_content)
1135+
}
1136+
1137+
pub(crate) fn into_string(self) -> String {
1138+
self.into_string_with_has_more_content().0
11241139
}
11251140
}
11261141

src/librustdoc/html/render/mod.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -467,9 +467,10 @@ fn document_short(
467467
return;
468468
}
469469
if let Some(s) = item.doc_value() {
470-
let mut summary_html = MarkdownSummaryLine(&s, &item.links(cx)).into_string();
470+
let (mut summary_html, has_more_content) =
471+
MarkdownSummaryLine(&s, &item.links(cx)).into_string_with_has_more_content();
471472

472-
if s.contains('\n') {
473+
if has_more_content {
473474
let link = format!(r#" <a{}>Read more</a>"#, assoc_href_attr(item, link, cx));
474475

475476
if let Some(idx) = summary_html.rfind("</p>") {
+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// Regression test for https://github.com/rust-lang/rust/issues/105677.
2+
// This test ensures that the "Read more" link is only generated when
3+
// there is actually more documentation to read after the short summary.
4+
5+
#![crate_name = "foo"]
6+
7+
pub trait MyFrom {
8+
/// # Hello
9+
/// ## Yolo
10+
/// more!
11+
fn try_from1();
12+
/// a
13+
/// b
14+
/// c
15+
fn try_from2();
16+
/// a
17+
///
18+
/// b
19+
///
20+
/// c
21+
fn try_from3();
22+
}
23+
24+
pub struct NonZero;
25+
26+
// @has 'foo/struct.NonZero.html'
27+
impl MyFrom for NonZero {
28+
// @matches - '//*[@class="docblock"]' '^Hello Read more$'
29+
fn try_from1() {}
30+
// @matches - '//*[@class="docblock"]' '^a\sb\sc$'
31+
fn try_from2() {}
32+
// @matches - '//*[@class="docblock"]' '^a Read more$'
33+
fn try_from3() {}
34+
}

src/test/rustdoc/trait-impl.rs

-2
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,6 @@ impl Trait for Struct {
3030
// @has - '//*[@id="method.b"]/../../div[@class="docblock"]' 'These docs contain'
3131
// @has - '//*[@id="method.b"]/../../div[@class="docblock"]/a' 'reference link'
3232
// @has - '//*[@id="method.b"]/../../div[@class="docblock"]/a/@href' 'https://example.com'
33-
// @has - '//*[@id="method.b"]/../../div[@class="docblock"]/a' 'Read more'
34-
// @has - '//*[@id="method.b"]/../../div[@class="docblock"]/a/@href' 'trait.Trait.html#tymethod.b'
3533
fn b() {}
3634

3735
// @!has - '//*[@id="method.c"]/../../div[@class="docblock"]' 'code block'

0 commit comments

Comments
 (0)