1
1
//! Markdown formatting for rustdoc.
2
2
//!
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.
7
4
//!
8
5
//! ```
9
6
//! #![feature(rustc_private)]
16
13
//!
17
14
//! let s = "My *markdown* _text_";
18
15
//! 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();
21
19
//! // ... something using html
22
20
//! ```
23
21
@@ -27,7 +25,7 @@ use rustc_data_structures::fx::FxHashMap;
27
25
use std:: cell:: RefCell ;
28
26
use std:: collections:: VecDeque ;
29
27
use std:: default:: Default ;
30
- use std:: fmt:: { self , Write } ;
28
+ use std:: fmt:: Write ;
31
29
use std:: borrow:: Cow ;
32
30
use std:: ops:: Range ;
33
31
use std:: str;
@@ -46,9 +44,8 @@ fn opts() -> Options {
46
44
Options :: ENABLE_TABLES | Options :: ENABLE_FOOTNOTES
47
45
}
48
46
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.
52
49
pub struct Markdown < ' a > (
53
50
pub & ' a str ,
54
51
/// A list of link replacements.
@@ -691,13 +688,13 @@ impl LangString {
691
688
}
692
689
}
693
690
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 ;
697
694
let mut ids = ids. borrow_mut ( ) ;
698
695
699
696
// This is actually common enough to special-case
700
- if md. is_empty ( ) { return Ok ( ( ) ) }
697
+ if md. is_empty ( ) { return String :: new ( ) ; }
701
698
let replacer = |_: & str , s : & str | {
702
699
if let Some ( & ( _, ref replace) ) = links. into_iter ( ) . find ( |link| & * link. 0 == s) {
703
700
Some ( ( replace. clone ( ) , s. to_owned ( ) ) )
@@ -716,13 +713,13 @@ impl<'a> fmt::Display for Markdown<'a> {
716
713
let p = Footnotes :: new ( p) ;
717
714
html:: push_html ( & mut s, p) ;
718
715
719
- fmt . write_str ( & s )
716
+ s
720
717
}
721
718
}
722
719
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 ;
726
723
let mut ids = ids. borrow_mut ( ) ;
727
724
728
725
let p = Parser :: new_ext ( md, opts ( ) ) ;
@@ -738,19 +735,17 @@ impl<'a> fmt::Display for MarkdownWithToc<'a> {
738
735
html:: push_html ( & mut s, p) ;
739
736
}
740
737
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)
744
739
}
745
740
}
746
741
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 ;
750
745
let mut ids = ids. borrow_mut ( ) ;
751
746
752
747
// This is actually common enough to special-case
753
- if md. is_empty ( ) { return Ok ( ( ) ) }
748
+ if md. is_empty ( ) { return String :: new ( ) ; }
754
749
let p = Parser :: new_ext ( md, opts ( ) ) ;
755
750
756
751
// Treat inline HTML as plain text.
@@ -766,15 +761,15 @@ impl<'a> fmt::Display for MarkdownHtml<'a> {
766
761
let p = Footnotes :: new ( p) ;
767
762
html:: push_html ( & mut s, p) ;
768
763
769
- fmt . write_str ( & s )
764
+ s
770
765
}
771
766
}
772
767
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 ;
776
771
// This is actually common enough to special-case
777
- if md. is_empty ( ) { return Ok ( ( ) ) }
772
+ if md. is_empty ( ) { return String :: new ( ) ; }
778
773
779
774
let replacer = |_: & str , s : & str | {
780
775
if let Some ( & ( _, ref replace) ) = links. into_iter ( ) . find ( |link| & * link. 0 == s) {
@@ -790,7 +785,7 @@ impl<'a> fmt::Display for MarkdownSummaryLine<'a> {
790
785
791
786
html:: push_html ( & mut s, LinkReplacer :: new ( SummaryLine :: new ( p) , links) ) ;
792
787
793
- fmt . write_str ( & s )
788
+ s
794
789
}
795
790
}
796
791
0 commit comments