diff options
Diffstat (limited to 'src/librustdoc/html/markdown.rs')
| -rw-r--r-- | src/librustdoc/html/markdown.rs | 31 |
1 files changed, 13 insertions, 18 deletions
diff --git a/src/librustdoc/html/markdown.rs b/src/librustdoc/html/markdown.rs index f626e07b000..e41435de29c 100644 --- a/src/librustdoc/html/markdown.rs +++ b/src/librustdoc/html/markdown.rs @@ -21,13 +21,14 @@ //! playground: &None, //! heading_offset: HeadingOffset::H2, //! }; -//! let html = md.into_string(); +//! let mut html = String::new(); +//! md.write_into(&mut html).unwrap(); //! // ... something using html //! ``` use std::borrow::Cow; use std::collections::VecDeque; -use std::fmt::Write; +use std::fmt::{self, Write}; use std::iter::Peekable; use std::ops::{ControlFlow, Range}; use std::path::PathBuf; @@ -1328,16 +1329,13 @@ impl LangString { } impl<'a> Markdown<'a> { - pub fn into_string(self) -> String { + pub fn write_into(self, f: impl fmt::Write) -> fmt::Result { // This is actually common enough to special-case if self.content.is_empty() { - return String::new(); + return Ok(()); } - let mut s = String::with_capacity(self.content.len() * 3 / 2); - html::push_html(&mut s, self.into_iter()); - - s + html::write_html_fmt(f, self.into_iter()) } fn into_iter(self) -> CodeBlocks<'a, 'a, impl Iterator<Item = Event<'a>>> { @@ -1453,19 +1451,20 @@ impl MarkdownWithToc<'_> { (toc.into_toc(), s) } - pub(crate) fn into_string(self) -> String { + + pub(crate) fn write_into(self, mut f: impl fmt::Write) -> fmt::Result { let (toc, s) = self.into_parts(); - format!("<nav id=\"rustdoc\">{toc}</nav>{s}", toc = toc.print()) + write!(f, "<nav id=\"rustdoc\">{toc}</nav>{s}", toc = toc.print()) } } impl MarkdownItemInfo<'_> { - pub(crate) fn into_string(self) -> String { + pub(crate) fn write_into(self, mut f: impl fmt::Write) -> fmt::Result { let MarkdownItemInfo(md, ids) = self; // This is actually common enough to special-case if md.is_empty() { - return String::new(); + return Ok(()); } let p = Parser::new_ext(md, main_body_opts()).into_offset_iter(); @@ -1475,8 +1474,6 @@ impl MarkdownItemInfo<'_> { _ => event, }); - let mut s = String::with_capacity(md.len() * 3 / 2); - ids.handle_footnotes(|ids, existing_footnotes| { let p = HeadingLinks::new(p, None, ids, HeadingOffset::H1); let p = footnotes::Footnotes::new(p, existing_footnotes); @@ -1484,10 +1481,8 @@ impl MarkdownItemInfo<'_> { let p = p.filter(|event| { !matches!(event, Event::Start(Tag::Paragraph) | Event::End(TagEnd::Paragraph)) }); - html::push_html(&mut s, p); - }); - - s + html::write_html_fmt(&mut f, p) + }) } } |
