11//! Markdown formatting for rustdoc.
22//!
3- //! This module implements markdown formatting through the pulldown-cmark
4- //! rust-library. This module exposes all of the
5- //! functionality through a unit struct, `Markdown`, which has an implementation
6- //! of `fmt::Display`. Example usage:
3+ //! This module implements markdown formatting through the pulldown-cmark library.
74//!
85//! ```
96//! #![feature(rustc_private)]
1613//!
1714//! let s = "My *markdown* _text_";
1815//! let mut id_map = IdMap::new();
19- //! let html = format!("{}", Markdown(s, &[], RefCell::new(&mut id_map),
20- //! ErrorCodes::Yes, Edition::Edition2015, None));
16+ //! let md = Markdown(s, &[], RefCell::new(&mut id_map),
17+ //! ErrorCodes::Yes, Edition::Edition2015, None);
18+ //! let html = md.to_string();
2119//! // ... something using html
2220//! ```
2321
@@ -27,7 +25,7 @@ use rustc_data_structures::fx::FxHashMap;
2725use std:: cell:: RefCell ;
2826use std:: collections:: VecDeque ;
2927use std:: default:: Default ;
30- use std:: fmt:: { self , Write } ;
28+ use std:: fmt:: Write ;
3129use std:: borrow:: Cow ;
3230use std:: ops:: Range ;
3331use std:: str;
@@ -46,9 +44,8 @@ fn opts() -> Options {
4644 Options :: ENABLE_TABLES | Options :: ENABLE_FOOTNOTES
4745}
4846
49- /// A tuple struct that has the `fmt::Display` trait implemented.
50- /// When formatted, this struct will emit the HTML corresponding to the rendered
51- /// version of the contained markdown string.
47+ /// When `to_string` is called, this struct will emit the HTML corresponding to
48+ /// the rendered version of the contained markdown string.
5249pub struct Markdown < ' a > (
5350 pub & ' a str ,
5451 /// A list of link replacements.
@@ -691,13 +688,13 @@ impl LangString {
691688 }
692689}
693690
694- impl < ' a > fmt :: Display for Markdown < ' a > {
695- fn fmt ( & self , fmt : & mut fmt :: Formatter < ' _ > ) -> fmt :: Result {
696- let Markdown ( md, links, ref ids, codes, edition, playground) = * self ;
691+ impl Markdown < ' _ > {
692+ pub fn to_string ( self ) -> String {
693+ let Markdown ( md, links, ids, codes, edition, playground) = self ;
697694 let mut ids = ids. borrow_mut ( ) ;
698695
699696 // This is actually common enough to special-case
700- if md. is_empty ( ) { return Ok ( ( ) ) }
697+ if md. is_empty ( ) { return String :: new ( ) ; }
701698 let replacer = |_: & str , s : & str | {
702699 if let Some ( & ( _, ref replace) ) = links. into_iter ( ) . find ( |link| & * link. 0 == s) {
703700 Some ( ( replace. clone ( ) , s. to_owned ( ) ) )
@@ -716,13 +713,13 @@ impl<'a> fmt::Display for Markdown<'a> {
716713 let p = Footnotes :: new ( p) ;
717714 html:: push_html ( & mut s, p) ;
718715
719- fmt . write_str ( & s )
716+ s
720717 }
721718}
722719
723- impl < ' a > fmt :: Display for MarkdownWithToc < ' a > {
724- fn fmt ( & self , fmt : & mut fmt :: Formatter < ' _ > ) -> fmt :: Result {
725- let MarkdownWithToc ( md, ref ids, codes, edition, playground) = * self ;
720+ impl MarkdownWithToc < ' _ > {
721+ pub fn to_string ( self ) -> String {
722+ let MarkdownWithToc ( md, ref ids, codes, edition, playground) = self ;
726723 let mut ids = ids. borrow_mut ( ) ;
727724
728725 let p = Parser :: new_ext ( md, opts ( ) ) ;
@@ -738,19 +735,17 @@ impl<'a> fmt::Display for MarkdownWithToc<'a> {
738735 html:: push_html ( & mut s, p) ;
739736 }
740737
741- write ! ( fmt, "<nav id=\" TOC\" >{}</nav>" , toc. into_toc( ) ) ?;
742-
743- fmt. write_str ( & s)
738+ format ! ( "<nav id=\" TOC\" >{}</nav>{}" , toc. into_toc( ) , s)
744739 }
745740}
746741
747- impl < ' a > fmt :: Display for MarkdownHtml < ' a > {
748- fn fmt ( & self , fmt : & mut fmt :: Formatter < ' _ > ) -> fmt :: Result {
749- let MarkdownHtml ( md, ref ids, codes, edition, playground) = * self ;
742+ impl MarkdownHtml < ' _ > {
743+ pub fn to_string ( self ) -> String {
744+ let MarkdownHtml ( md, ref ids, codes, edition, playground) = self ;
750745 let mut ids = ids. borrow_mut ( ) ;
751746
752747 // This is actually common enough to special-case
753- if md. is_empty ( ) { return Ok ( ( ) ) }
748+ if md. is_empty ( ) { return String :: new ( ) ; }
754749 let p = Parser :: new_ext ( md, opts ( ) ) ;
755750
756751 // Treat inline HTML as plain text.
@@ -766,15 +761,15 @@ impl<'a> fmt::Display for MarkdownHtml<'a> {
766761 let p = Footnotes :: new ( p) ;
767762 html:: push_html ( & mut s, p) ;
768763
769- fmt . write_str ( & s )
764+ s
770765 }
771766}
772767
773- impl < ' a > fmt :: Display for MarkdownSummaryLine < ' a > {
774- fn fmt ( & self , fmt : & mut fmt :: Formatter < ' _ > ) -> fmt :: Result {
775- let MarkdownSummaryLine ( md, links) = * self ;
768+ impl MarkdownSummaryLine < ' _ > {
769+ pub fn to_string ( self ) -> String {
770+ let MarkdownSummaryLine ( md, links) = self ;
776771 // This is actually common enough to special-case
777- if md. is_empty ( ) { return Ok ( ( ) ) }
772+ if md. is_empty ( ) { return String :: new ( ) ; }
778773
779774 let replacer = |_: & str , s : & str | {
780775 if let Some ( & ( _, ref replace) ) = links. into_iter ( ) . find ( |link| & * link. 0 == s) {
@@ -790,7 +785,7 @@ impl<'a> fmt::Display for MarkdownSummaryLine<'a> {
790785
791786 html:: push_html ( & mut s, LinkReplacer :: new ( SummaryLine :: new ( p) , links) ) ;
792787
793- fmt . write_str ( & s )
788+ s
794789 }
795790}
796791
0 commit comments