Skip to content

Commit 323d550

Browse files
Turn markdown_split_summary_and_content into a method of Markdown
1 parent 26a21f3 commit 323d550

File tree

2 files changed

+52
-49
lines changed

2 files changed

+52
-49
lines changed

src/librustdoc/html/markdown.rs

Lines changed: 49 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1348,6 +1348,55 @@ impl<'a> Markdown<'a> {
13481348
CodeBlocks::new(p, codes, edition, playground)
13491349
})
13501350
}
1351+
1352+
/// Convert markdown to (summary, remaining) HTML.
1353+
///
1354+
/// - The summary is the first top-level Markdown element (usually a paragraph, but potentially
1355+
/// any block).
1356+
/// - The remaining docs contain everything after the summary.
1357+
pub(crate) fn split_summary_and_content(self) -> (Option<String>, Option<String>) {
1358+
if self.content.is_empty() {
1359+
return (None, None);
1360+
}
1361+
let mut p = self.into_iter();
1362+
1363+
let mut event_level = 0;
1364+
let mut summary_events = Vec::new();
1365+
let mut get_next_tag = false;
1366+
1367+
let mut end_of_summary = false;
1368+
while let Some(event) = p.next() {
1369+
match event {
1370+
Event::Start(_) => event_level += 1,
1371+
Event::End(kind) => {
1372+
event_level -= 1;
1373+
if event_level == 0 {
1374+
// We're back at the "top" so it means we're done with the summary.
1375+
end_of_summary = true;
1376+
// We surround tables with `<div>` HTML tags so this is a special case.
1377+
get_next_tag = kind == TagEnd::Table;
1378+
}
1379+
}
1380+
_ => {}
1381+
}
1382+
summary_events.push(event);
1383+
if end_of_summary {
1384+
if get_next_tag && let Some(event) = p.next() {
1385+
summary_events.push(event);
1386+
}
1387+
break;
1388+
}
1389+
}
1390+
let mut summary = String::new();
1391+
html::push_html(&mut summary, summary_events.into_iter());
1392+
if summary.is_empty() {
1393+
return (None, None);
1394+
}
1395+
let mut content = String::new();
1396+
html::push_html(&mut content, p);
1397+
1398+
if content.is_empty() { (Some(summary), None) } else { (Some(summary), Some(content)) }
1399+
}
13511400
}
13521401

13531402
impl MarkdownWithToc<'_> {
@@ -1421,52 +1470,6 @@ impl MarkdownItemInfo<'_> {
14211470
}
14221471
}
14231472

1424-
pub(crate) fn markdown_split_summary_and_content(
1425-
md: Markdown<'_>,
1426-
) -> (Option<String>, Option<String>) {
1427-
if md.content.is_empty() {
1428-
return (None, None);
1429-
}
1430-
let mut p = md.into_iter();
1431-
1432-
let mut event_level = 0;
1433-
let mut summary_events = Vec::new();
1434-
let mut get_next_tag = false;
1435-
1436-
let mut end_of_summary = false;
1437-
while let Some(event) = p.next() {
1438-
match event {
1439-
Event::Start(_) => event_level += 1,
1440-
Event::End(kind) => {
1441-
event_level -= 1;
1442-
if event_level == 0 {
1443-
// We're back at the "top" so it means we're done with the summary.
1444-
end_of_summary = true;
1445-
// We surround tables with `<div>` HTML tags so this is a special case.
1446-
get_next_tag = kind == TagEnd::Table;
1447-
}
1448-
}
1449-
_ => {}
1450-
}
1451-
summary_events.push(event);
1452-
if end_of_summary {
1453-
if get_next_tag && let Some(event) = p.next() {
1454-
summary_events.push(event);
1455-
}
1456-
break;
1457-
}
1458-
}
1459-
let mut summary = String::new();
1460-
html::push_html(&mut summary, summary_events.into_iter());
1461-
if summary.is_empty() {
1462-
return (None, None);
1463-
}
1464-
let mut content = String::new();
1465-
html::push_html(&mut content, p);
1466-
1467-
if content.is_empty() { (Some(summary), None) } else { (Some(summary), Some(content)) }
1468-
}
1469-
14701473
impl MarkdownSummaryLine<'_> {
14711474
pub(crate) fn into_string_with_has_more_content(self) -> (String, bool) {
14721475
let MarkdownSummaryLine(md, links) = self;

src/librustdoc/html/render/mod.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,6 @@ use crate::html::format::{
7575
};
7676
use crate::html::markdown::{
7777
HeadingOffset, IdMap, Markdown, MarkdownItemInfo, MarkdownSummaryLine,
78-
markdown_split_summary_and_content,
7978
};
8079
use crate::html::static_files::SCRAPE_EXAMPLES_HELP_MD;
8180
use crate::html::{highlight, sources};
@@ -1944,15 +1943,16 @@ fn render_impl(
19441943
.impl_item
19451944
.opt_doc_value()
19461945
.map(|dox| {
1947-
markdown_split_summary_and_content(Markdown {
1946+
Markdown {
19481947
content: &*dox,
19491948
links: &i.impl_item.links(cx),
19501949
ids: &mut cx.id_map,
19511950
error_codes: cx.shared.codes,
19521951
edition: cx.shared.edition(),
19531952
playground: &cx.shared.playground,
19541953
heading_offset: HeadingOffset::H4,
1955-
})
1954+
}
1955+
.split_summary_and_content()
19561956
})
19571957
.unwrap_or((None, None));
19581958
render_impl_summary(

0 commit comments

Comments
 (0)