18
18
//! ```
19
19
//! #![feature(rustc_private)]
20
20
//!
21
- //! use rustdoc::html::markdown::Markdown;
21
+ //! use rustdoc::html::markdown::{ Markdown, ErrorCodes} ;
22
22
//!
23
23
//! let s = "My *markdown* _text_";
24
- //! let html = format!("{}", Markdown(s, &[]));
24
+ //! let html = format!("{}", Markdown(s, &[], ErrorCodes::Yes ));
25
25
//! // ... something using html
26
26
//! ```
27
27
@@ -35,7 +35,6 @@ use std::borrow::Cow;
35
35
use std:: ops:: Range ;
36
36
use std:: str;
37
37
38
- use syntax:: feature_gate:: UnstableFeatures ;
39
38
use html:: render:: derive_id;
40
39
use html:: toc:: TocBuilder ;
41
40
use html:: highlight;
@@ -48,15 +47,37 @@ use pulldown_cmark::{Options, OPTION_ENABLE_FOOTNOTES, OPTION_ENABLE_TABLES};
48
47
/// formatted, this struct will emit the HTML corresponding to the rendered
49
48
/// version of the contained markdown string.
50
49
/// The second parameter is a list of link replacements
51
- pub struct Markdown < ' a > ( pub & ' a str , pub & ' a [ ( String , String ) ] ) ;
50
+ pub struct Markdown < ' a > ( pub & ' a str , pub & ' a [ ( String , String ) ] , pub ErrorCodes ) ;
52
51
/// A unit struct like `Markdown`, that renders the markdown with a
53
52
/// table of contents.
54
- pub struct MarkdownWithToc < ' a > ( pub & ' a str ) ;
53
+ pub struct MarkdownWithToc < ' a > ( pub & ' a str , pub ErrorCodes ) ;
55
54
/// A unit struct like `Markdown`, that renders the markdown escaping HTML tags.
56
- pub struct MarkdownHtml < ' a > ( pub & ' a str ) ;
55
+ pub struct MarkdownHtml < ' a > ( pub & ' a str , pub ErrorCodes ) ;
57
56
/// A unit struct like `Markdown`, that renders only the first paragraph.
58
57
pub struct MarkdownSummaryLine < ' a > ( pub & ' a str , pub & ' a [ ( String , String ) ] ) ;
59
58
59
+ #[ derive( Copy , Clone , PartialEq , Debug ) ]
60
+ pub enum ErrorCodes {
61
+ Yes ,
62
+ No ,
63
+ }
64
+
65
+ impl ErrorCodes {
66
+ pub fn from ( b : bool ) -> Self {
67
+ match b {
68
+ true => ErrorCodes :: Yes ,
69
+ false => ErrorCodes :: No ,
70
+ }
71
+ }
72
+
73
+ pub fn as_bool ( self ) -> bool {
74
+ match self {
75
+ ErrorCodes :: Yes => true ,
76
+ ErrorCodes :: No => false ,
77
+ }
78
+ }
79
+ }
80
+
60
81
/// Controls whether a line will be hidden or shown in HTML output.
61
82
///
62
83
/// All lines are used in documentation tests.
@@ -127,14 +148,14 @@ thread_local!(pub static PLAYGROUND: RefCell<Option<(Option<String>, String)>> =
127
148
/// Adds syntax highlighting and playground Run buttons to rust code blocks.
128
149
struct CodeBlocks < ' a , I : Iterator < Item = Event < ' a > > > {
129
150
inner : I ,
130
- check_error_codes : bool ,
151
+ check_error_codes : ErrorCodes ,
131
152
}
132
153
133
154
impl < ' a , I : Iterator < Item = Event < ' a > > > CodeBlocks < ' a , I > {
134
- fn new ( iter : I ) -> Self {
155
+ fn new ( iter : I , error_codes : ErrorCodes ) -> Self {
135
156
CodeBlocks {
136
157
inner : iter,
137
- check_error_codes : UnstableFeatures :: from_environment ( ) . is_nightly_build ( ) ,
158
+ check_error_codes : error_codes ,
138
159
}
139
160
}
140
161
}
@@ -476,9 +497,8 @@ impl fmt::Display for TestableCodeError {
476
497
}
477
498
478
499
pub fn find_testable_code (
479
- doc : & str , tests : & mut test:: Collector
500
+ doc : & str , tests : & mut test:: Collector , error_codes : ErrorCodes ,
480
501
) -> Result < ( ) , TestableCodeError > {
481
- let is_nightly = UnstableFeatures :: from_environment ( ) . is_nightly_build ( ) ;
482
502
let mut parser = Parser :: new ( doc) ;
483
503
let mut prev_offset = 0 ;
484
504
let mut nb_lines = 0 ;
@@ -489,7 +509,7 @@ pub fn find_testable_code(
489
509
let block_info = if s. is_empty ( ) {
490
510
LangString :: all_false ( )
491
511
} else {
492
- LangString :: parse ( & * s, is_nightly )
512
+ LangString :: parse ( & * s, error_codes )
493
513
} ;
494
514
if !block_info. rust {
495
515
continue
@@ -570,7 +590,8 @@ impl LangString {
570
590
}
571
591
}
572
592
573
- fn parse ( string : & str , allow_error_code_check : bool ) -> LangString {
593
+ fn parse ( string : & str , allow_error_code_check : ErrorCodes ) -> LangString {
594
+ let allow_error_code_check = allow_error_code_check. as_bool ( ) ;
574
595
let mut seen_rust_tags = false ;
575
596
let mut seen_other_tags = false ;
576
597
let mut data = LangString :: all_false ( ) ;
@@ -620,7 +641,7 @@ impl LangString {
620
641
621
642
impl < ' a > fmt:: Display for Markdown < ' a > {
622
643
fn fmt ( & self , fmt : & mut fmt:: Formatter ) -> fmt:: Result {
623
- let Markdown ( md, links) = * self ;
644
+ let Markdown ( md, links, codes ) = * self ;
624
645
625
646
// This is actually common enough to special-case
626
647
if md. is_empty ( ) { return Ok ( ( ) ) }
@@ -645,15 +666,15 @@ impl<'a> fmt::Display for Markdown<'a> {
645
666
CodeBlocks :: new (
646
667
LinkReplacer :: new (
647
668
HeadingLinks :: new ( p, None ) ,
648
- links) ) ) ) ;
669
+ links) , codes ) ) ) ;
649
670
650
671
fmt. write_str ( & s)
651
672
}
652
673
}
653
674
654
675
impl < ' a > fmt:: Display for MarkdownWithToc < ' a > {
655
676
fn fmt ( & self , fmt : & mut fmt:: Formatter ) -> fmt:: Result {
656
- let MarkdownWithToc ( md) = * self ;
677
+ let MarkdownWithToc ( md, codes ) = * self ;
657
678
658
679
let mut opts = Options :: empty ( ) ;
659
680
opts. insert ( OPTION_ENABLE_TABLES ) ;
@@ -665,8 +686,12 @@ impl<'a> fmt::Display for MarkdownWithToc<'a> {
665
686
666
687
let mut toc = TocBuilder :: new ( ) ;
667
688
668
- html:: push_html ( & mut s,
669
- Footnotes :: new ( CodeBlocks :: new ( HeadingLinks :: new ( p, Some ( & mut toc) ) ) ) ) ;
689
+ {
690
+ let p = HeadingLinks :: new ( p, Some ( & mut toc) ) ;
691
+ let p = CodeBlocks :: new ( p, codes) ;
692
+ let p = Footnotes :: new ( p) ;
693
+ html:: push_html ( & mut s, p) ;
694
+ }
670
695
671
696
write ! ( fmt, "<nav id=\" TOC\" >{}</nav>" , toc. into_toc( ) ) ?;
672
697
@@ -676,7 +701,7 @@ impl<'a> fmt::Display for MarkdownWithToc<'a> {
676
701
677
702
impl < ' a > fmt:: Display for MarkdownHtml < ' a > {
678
703
fn fmt ( & self , fmt : & mut fmt:: Formatter ) -> fmt:: Result {
679
- let MarkdownHtml ( md) = * self ;
704
+ let MarkdownHtml ( md, codes ) = * self ;
680
705
681
706
// This is actually common enough to special-case
682
707
if md. is_empty ( ) { return Ok ( ( ) ) }
@@ -694,8 +719,10 @@ impl<'a> fmt::Display for MarkdownHtml<'a> {
694
719
695
720
let mut s = String :: with_capacity ( md. len ( ) * 3 / 2 ) ;
696
721
697
- html:: push_html ( & mut s,
698
- Footnotes :: new ( CodeBlocks :: new ( HeadingLinks :: new ( p, None ) ) ) ) ;
722
+ let p = HeadingLinks :: new ( p, None ) ;
723
+ let p = CodeBlocks :: new ( p, codes) ;
724
+ let p = Footnotes :: new ( p) ;
725
+ html:: push_html ( & mut s, p) ;
699
726
700
727
fmt. write_str ( & s)
701
728
}
@@ -830,7 +857,7 @@ pub fn markdown_links(md: &str) -> Vec<(String, Option<Range<usize>>)> {
830
857
831
858
#[ cfg( test) ]
832
859
mod tests {
833
- use super :: { LangString , Markdown , MarkdownHtml } ;
860
+ use super :: { ErrorCodes , LangString , Markdown , MarkdownHtml } ;
834
861
use super :: plain_summary_line;
835
862
use html:: render:: reset_ids;
836
863
@@ -839,7 +866,7 @@ mod tests {
839
866
fn t ( s : & str ,
840
867
should_panic : bool , no_run : bool , ignore : bool , rust : bool , test_harness : bool ,
841
868
compile_fail : bool , allow_fail : bool , error_codes : Vec < String > ) {
842
- assert_eq ! ( LangString :: parse( s, true ) , LangString {
869
+ assert_eq ! ( LangString :: parse( s, ErrorCodes :: Yes ) , LangString {
843
870
should_panic,
844
871
no_run,
845
872
ignore,
@@ -878,14 +905,14 @@ mod tests {
878
905
#[ test]
879
906
fn issue_17736 ( ) {
880
907
let markdown = "# title" ;
881
- Markdown ( markdown, & [ ] ) . to_string ( ) ;
908
+ Markdown ( markdown, & [ ] , ErrorCodes :: Yes ) . to_string ( ) ;
882
909
reset_ids ( true ) ;
883
910
}
884
911
885
912
#[ test]
886
913
fn test_header ( ) {
887
914
fn t ( input : & str , expect : & str ) {
888
- let output = Markdown ( input, & [ ] ) . to_string ( ) ;
915
+ let output = Markdown ( input, & [ ] , ErrorCodes :: Yes ) . to_string ( ) ;
889
916
assert_eq ! ( output, expect, "original: {}" , input) ;
890
917
reset_ids ( true ) ;
891
918
}
@@ -907,7 +934,7 @@ mod tests {
907
934
#[ test]
908
935
fn test_header_ids_multiple_blocks ( ) {
909
936
fn t ( input : & str , expect : & str ) {
910
- let output = Markdown ( input, & [ ] ) . to_string ( ) ;
937
+ let output = Markdown ( input, & [ ] , ErrorCodes :: Yes ) . to_string ( ) ;
911
938
assert_eq ! ( output, expect, "original: {}" , input) ;
912
939
}
913
940
@@ -948,7 +975,7 @@ mod tests {
948
975
#[ test]
949
976
fn test_markdown_html_escape ( ) {
950
977
fn t ( input : & str , expect : & str ) {
951
- let output = MarkdownHtml ( input) . to_string ( ) ;
978
+ let output = MarkdownHtml ( input, ErrorCodes :: Yes ) . to_string ( ) ;
952
979
assert_eq ! ( output, expect, "original: {}" , input) ;
953
980
}
954
981
0 commit comments