Skip to content

Commit 6e0dabd

Browse files
Turn markdown_split_summary_and_content into a method of Markdown
1 parent abcd094 commit 6e0dabd

File tree

2 files changed

+52
-49
lines changed

2 files changed

+52
-49
lines changed

src/librustdoc/html/markdown.rs

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

13501399
impl MarkdownWithToc<'_> {
@@ -1418,52 +1467,6 @@ impl MarkdownItemInfo<'_> {
14181467
}
14191468
}
14201469

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

src/librustdoc/html/render/mod.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,6 @@ use crate::html::format::{
7474
};
7575
use crate::html::markdown::{
7676
HeadingOffset, IdMap, Markdown, MarkdownItemInfo, MarkdownSummaryLine,
77-
markdown_split_summary_and_content,
7877
};
7978
use crate::html::static_files::SCRAPE_EXAMPLES_HELP_MD;
8079
use crate::html::{highlight, sources};
@@ -1941,15 +1940,16 @@ fn render_impl(
19411940
.impl_item
19421941
.opt_doc_value()
19431942
.map(|dox| {
1944-
markdown_split_summary_and_content(Markdown {
1943+
Markdown {
19451944
content: &*dox,
19461945
links: &i.impl_item.links(cx),
19471946
ids: &mut cx.id_map.borrow_mut(),
19481947
error_codes: cx.shared.codes,
19491948
edition: cx.shared.edition(),
19501949
playground: &cx.shared.playground,
19511950
heading_offset: HeadingOffset::H4,
1952-
})
1951+
}
1952+
.split_summary_and_content()
19531953
})
19541954
.unwrap_or((None, None));
19551955
render_impl_summary(

0 commit comments

Comments
 (0)