Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit af1b4fd

Browse files
committedOct 28, 2024
Turn markdown_split_summary_and_content into a method of Markdown
1 parent 890fc0d commit af1b4fd

File tree

2 files changed

+47
-49
lines changed

2 files changed

+47
-49
lines changed
 

‎src/librustdoc/html/markdown.rs

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

13501394
impl MarkdownWithToc<'_> {
@@ -1416,52 +1460,6 @@ impl MarkdownItemInfo<'_> {
14161460
}
14171461
}
14181462

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

‎src/librustdoc/html/render/mod.rs

+3-3
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};
@@ -1943,15 +1942,16 @@ fn render_impl(
19431942
.impl_item
19441943
.opt_doc_value()
19451944
.map(|dox| {
1946-
markdown_split_summary_and_content(Markdown {
1945+
Markdown {
19471946
content: &*dox,
19481947
links: &i.impl_item.links(cx),
19491948
ids: &mut cx.id_map,
19501949
error_codes: cx.shared.codes,
19511950
edition: cx.shared.edition(),
19521951
playground: &cx.shared.playground,
19531952
heading_offset: HeadingOffset::H4,
1954-
})
1953+
}
1954+
.split_summary_and_content()
19551955
})
19561956
.unwrap_or((None, None));
19571957
render_impl_summary(

0 commit comments

Comments
 (0)
Failed to load comments.