mirror of
https://github.com/rust-lang/rust.git
synced 2025-01-22 12:43:36 +00:00
Turn markdown_split_summary_and_content
into a method of Markdown
This commit is contained in:
parent
abcd094c51
commit
6e0dabd9e2
@ -1345,6 +1345,55 @@ impl<'a> Markdown<'a> {
|
||||
CodeBlocks::new(p, codes, edition, playground)
|
||||
})
|
||||
}
|
||||
|
||||
/// Convert markdown to (summary, remaining) HTML.
|
||||
///
|
||||
/// - The summary is the first top-level Markdown element (usually a paragraph, but potentially
|
||||
/// any block).
|
||||
/// - The remaining docs contain everything after the summary.
|
||||
pub(crate) fn split_summary_and_content(self) -> (Option<String>, Option<String>) {
|
||||
if self.content.is_empty() {
|
||||
return (None, None);
|
||||
}
|
||||
let mut p = self.into_iter();
|
||||
|
||||
let mut event_level = 0;
|
||||
let mut summary_events = Vec::new();
|
||||
let mut get_next_tag = false;
|
||||
|
||||
let mut end_of_summary = false;
|
||||
while let Some(event) = p.next() {
|
||||
match event {
|
||||
Event::Start(_) => event_level += 1,
|
||||
Event::End(kind) => {
|
||||
event_level -= 1;
|
||||
if event_level == 0 {
|
||||
// We're back at the "top" so it means we're done with the summary.
|
||||
end_of_summary = true;
|
||||
// We surround tables with `<div>` HTML tags so this is a special case.
|
||||
get_next_tag = kind == TagEnd::Table;
|
||||
}
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
summary_events.push(event);
|
||||
if end_of_summary {
|
||||
if get_next_tag && let Some(event) = p.next() {
|
||||
summary_events.push(event);
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
let mut summary = String::new();
|
||||
html::push_html(&mut summary, summary_events.into_iter());
|
||||
if summary.is_empty() {
|
||||
return (None, None);
|
||||
}
|
||||
let mut content = String::new();
|
||||
html::push_html(&mut content, p);
|
||||
|
||||
if content.is_empty() { (Some(summary), None) } else { (Some(summary), Some(content)) }
|
||||
}
|
||||
}
|
||||
|
||||
impl MarkdownWithToc<'_> {
|
||||
@ -1418,52 +1467,6 @@ impl MarkdownItemInfo<'_> {
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn markdown_split_summary_and_content(
|
||||
md: Markdown<'_>,
|
||||
) -> (Option<String>, Option<String>) {
|
||||
if md.content.is_empty() {
|
||||
return (None, None);
|
||||
}
|
||||
let mut p = md.into_iter();
|
||||
|
||||
let mut event_level = 0;
|
||||
let mut summary_events = Vec::new();
|
||||
let mut get_next_tag = false;
|
||||
|
||||
let mut end_of_summary = false;
|
||||
while let Some(event) = p.next() {
|
||||
match event {
|
||||
Event::Start(_) => event_level += 1,
|
||||
Event::End(kind) => {
|
||||
event_level -= 1;
|
||||
if event_level == 0 {
|
||||
// We're back at the "top" so it means we're done with the summary.
|
||||
end_of_summary = true;
|
||||
// We surround tables with `<div>` HTML tags so this is a special case.
|
||||
get_next_tag = kind == TagEnd::Table;
|
||||
}
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
summary_events.push(event);
|
||||
if end_of_summary {
|
||||
if get_next_tag && let Some(event) = p.next() {
|
||||
summary_events.push(event);
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
let mut summary = String::new();
|
||||
html::push_html(&mut summary, summary_events.into_iter());
|
||||
if summary.is_empty() {
|
||||
return (None, None);
|
||||
}
|
||||
let mut content = String::new();
|
||||
html::push_html(&mut content, p);
|
||||
|
||||
if content.is_empty() { (Some(summary), None) } else { (Some(summary), Some(content)) }
|
||||
}
|
||||
|
||||
impl MarkdownSummaryLine<'_> {
|
||||
pub(crate) fn into_string_with_has_more_content(self) -> (String, bool) {
|
||||
let MarkdownSummaryLine(md, links) = self;
|
||||
|
@ -74,7 +74,6 @@ use crate::html::format::{
|
||||
};
|
||||
use crate::html::markdown::{
|
||||
HeadingOffset, IdMap, Markdown, MarkdownItemInfo, MarkdownSummaryLine,
|
||||
markdown_split_summary_and_content,
|
||||
};
|
||||
use crate::html::static_files::SCRAPE_EXAMPLES_HELP_MD;
|
||||
use crate::html::{highlight, sources};
|
||||
@ -1941,7 +1940,7 @@ fn render_impl(
|
||||
.impl_item
|
||||
.opt_doc_value()
|
||||
.map(|dox| {
|
||||
markdown_split_summary_and_content(Markdown {
|
||||
Markdown {
|
||||
content: &*dox,
|
||||
links: &i.impl_item.links(cx),
|
||||
ids: &mut cx.id_map.borrow_mut(),
|
||||
@ -1949,7 +1948,8 @@ fn render_impl(
|
||||
edition: cx.shared.edition(),
|
||||
playground: &cx.shared.playground,
|
||||
heading_offset: HeadingOffset::H4,
|
||||
})
|
||||
}
|
||||
.split_summary_and_content()
|
||||
})
|
||||
.unwrap_or((None, None));
|
||||
render_impl_summary(
|
||||
|
Loading…
Reference in New Issue
Block a user