X-Git-Url: https://git.lizzy.rs/?a=blobdiff_plain;f=src%2Flibrustdoc%2Fhtml%2Fmarkdown.rs;h=aeaee524fd4532b820f130d04ccb7912ceb68411;hb=f74bcfbdb1fa2b2c3904f2f24f0c8ddb3c00b5c6;hp=1e1c657b0bf22f7d71306f57099be4ac8d9c3145;hpb=e81ad69331dd5fafc9f89756c08a3882680b70e7;p=rust.git diff --git a/src/librustdoc/html/markdown.rs b/src/librustdoc/html/markdown.rs index 1e1c657b0bf..aeaee524fd4 100644 --- a/src/librustdoc/html/markdown.rs +++ b/src/librustdoc/html/markdown.rs @@ -236,12 +236,12 @@ fn next(&mut self) -> Option { return event; }; - let mut origtext = String::new(); + let mut original_text = String::new(); for event in &mut self.inner { match event { Event::End(Tag::CodeBlock(..)) => break, Event::Text(ref s) => { - origtext.push_str(s); + original_text.push_str(s); } _ => {} } @@ -258,7 +258,7 @@ fn next(&mut self) -> Option {
{}
\ ", lang, - Escape(&origtext), + Escape(&original_text), ) .into(), )); @@ -268,7 +268,7 @@ fn next(&mut self) -> Option { CodeBlockKind::Indented => Default::default(), }; - let lines = origtext.lines().filter_map(|l| map_line(l).for_html()); + let lines = original_text.lines().filter_map(|l| map_line(l).for_html()); let text = lines.intersperse("\n".into()).collect::(); compile_fail = parse_result.compile_fail; @@ -285,7 +285,7 @@ fn next(&mut self) -> Option { if url.is_empty() { return None; } - let test = origtext + let test = original_text .lines() .map(|l| map_line(l).for_code()) .intersperse("\n".into()) @@ -567,11 +567,12 @@ struct SummaryLine<'a, I: Iterator>> { inner: I, started: bool, depth: u32, + skipped_tags: u32, } impl<'a, I: Iterator>> SummaryLine<'a, I> { fn new(iter: I) -> Self { - SummaryLine { inner: iter, started: false, depth: 0 } + SummaryLine { inner: iter, started: false, depth: 0, skipped_tags: 0 } } } @@ -601,6 +602,7 @@ fn next(&mut self) -> Option { let is_allowed_tag = match event { Event::Start(ref c) => { if is_forbidden_tag(c) { + self.skipped_tags += 1; return None; } self.depth += 1; @@ -608,6 +610,7 @@ fn next(&mut self) -> Option { } Event::End(ref c) => { if is_forbidden_tag(c) { + self.skipped_tags += 1; return None; } self.depth -= 1; @@ -616,6 +619,9 @@ fn next(&mut self) -> Option { } _ => true, }; + if !is_allowed_tag { + self.skipped_tags += 1; + } return if !is_allowed_tag { if is_start { Some(Event::Start(Tag::Paragraph)) @@ -1096,11 +1102,11 @@ pub(crate) fn into_string(self) -> String { } impl MarkdownSummaryLine<'_> { - pub(crate) fn into_string(self) -> String { + pub(crate) fn into_string_with_has_more_content(self) -> (String, bool) { let MarkdownSummaryLine(md, links) = self; // This is actually common enough to special-case if md.is_empty() { - return String::new(); + return (String::new(), false); } let mut replacer = |broken_link: BrokenLink<'_>| { @@ -1110,17 +1116,26 @@ pub(crate) fn into_string(self) -> String { .map(|link| (link.href.as_str().into(), link.new_text.as_str().into())) }; - let p = Parser::new_with_broken_link_callback(md, summary_opts(), Some(&mut replacer)); + let p = Parser::new_with_broken_link_callback(md, summary_opts(), Some(&mut replacer)) + .peekable(); + let mut summary = SummaryLine::new(p); let mut s = String::new(); - let without_paragraphs = LinkReplacer::new(SummaryLine::new(p), links).filter(|event| { + let without_paragraphs = LinkReplacer::new(&mut summary, links).filter(|event| { !matches!(event, Event::Start(Tag::Paragraph) | Event::End(Tag::Paragraph)) }); html::push_html(&mut s, without_paragraphs); - s + let has_more_content = + matches!(summary.inner.peek(), Some(Event::Start(_))) || summary.skipped_tags > 0; + + (s, has_more_content) + } + + pub(crate) fn into_string(self) -> String { + self.into_string_with_has_more_content().0 } }