8
8
//! ```
9
9
//! #![feature(rustc_private)]
10
10
//!
11
+ //! extern crate syntax;
12
+ //!
13
+ //! use syntax::edition::Edition;
11
14
//! use rustdoc::html::markdown::{IdMap, Markdown, ErrorCodes};
12
15
//! use std::cell::RefCell;
13
16
//!
14
17
//! let s = "My *markdown* _text_";
15
18
//! let mut id_map = IdMap::new();
16
- //! let html = format!("{}", Markdown(s, &[], RefCell::new(&mut id_map), ErrorCodes::Yes));
19
+ //! let html = format!("{}", Markdown(s, &[], RefCell::new(&mut id_map),
20
+ //! ErrorCodes::Yes, Edition::Edition2015));
17
21
//! // ... something using html
18
22
//! ```
19
23
@@ -42,14 +46,21 @@ fn opts() -> Options {
42
46
/// A unit struct which has the `fmt::Display` trait implemented. When
43
47
/// formatted, this struct will emit the HTML corresponding to the rendered
44
48
/// version of the contained markdown string.
45
- /// The second parameter is a list of link replacements
49
+ ///
50
+ /// The second parameter is a list of link replacements.
51
+ ///
52
+ /// The third is the current list of used header IDs.
53
+ ///
54
+ /// The fourth is whether to allow the use of explicit error codes in doctest lang strings.
55
+ ///
56
+ /// The fifth is what default edition to use when parsing doctests (to add a `fn main`).
46
57
pub struct Markdown < ' a > (
47
- pub & ' a str , pub & ' a [ ( String , String ) ] , pub RefCell < & ' a mut IdMap > , pub ErrorCodes ) ;
58
+ pub & ' a str , pub & ' a [ ( String , String ) ] , pub RefCell < & ' a mut IdMap > , pub ErrorCodes , pub Edition ) ;
48
59
/// A unit struct like `Markdown`, that renders the markdown with a
49
60
/// table of contents.
50
- pub struct MarkdownWithToc < ' a > ( pub & ' a str , pub RefCell < & ' a mut IdMap > , pub ErrorCodes ) ;
61
+ pub struct MarkdownWithToc < ' a > ( pub & ' a str , pub RefCell < & ' a mut IdMap > , pub ErrorCodes , pub Edition ) ;
51
62
/// A unit struct like `Markdown`, that renders the markdown escaping HTML tags.
52
- pub struct MarkdownHtml < ' a > ( pub & ' a str , pub RefCell < & ' a mut IdMap > , pub ErrorCodes ) ;
63
+ pub struct MarkdownHtml < ' a > ( pub & ' a str , pub RefCell < & ' a mut IdMap > , pub ErrorCodes , pub Edition ) ;
53
64
/// A unit struct like `Markdown`, that renders only the first paragraph.
54
65
pub struct MarkdownSummaryLine < ' a > ( pub & ' a str , pub & ' a [ ( String , String ) ] ) ;
55
66
@@ -146,13 +157,15 @@ thread_local!(pub static PLAYGROUND: RefCell<Option<(Option<String>, String)>> =
146
157
struct CodeBlocks < ' a , I : Iterator < Item = Event < ' a > > > {
147
158
inner : I ,
148
159
check_error_codes : ErrorCodes ,
160
+ edition : Edition ,
149
161
}
150
162
151
163
impl < ' a , I : Iterator < Item = Event < ' a > > > CodeBlocks < ' a , I > {
152
- fn new ( iter : I , error_codes : ErrorCodes ) -> Self {
164
+ fn new ( iter : I , error_codes : ErrorCodes , edition : Edition ) -> Self {
153
165
CodeBlocks {
154
166
inner : iter,
155
167
check_error_codes : error_codes,
168
+ edition,
156
169
}
157
170
}
158
171
}
@@ -177,6 +190,9 @@ impl<'a, I: Iterator<Item = Event<'a>>> Iterator for CodeBlocks<'a, I> {
177
190
return event;
178
191
}
179
192
193
+ let explicit_edition = edition. is_some ( ) ;
194
+ let edition = edition. unwrap_or ( self . edition ) ;
195
+
180
196
let mut origtext = String :: new ( ) ;
181
197
for event in & mut self . inner {
182
198
match event {
@@ -202,22 +218,14 @@ impl<'a, I: Iterator<Item = Event<'a>>> Iterator for CodeBlocks<'a, I> {
202
218
. collect :: < Vec < Cow < ' _ , str > > > ( ) . join ( "\n " ) ;
203
219
let krate = krate. as_ref ( ) . map ( |s| & * * s) ;
204
220
let ( test, _) = test:: make_test ( & test, krate, false ,
205
- & Default :: default ( ) ) ;
221
+ & Default :: default ( ) , edition ) ;
206
222
let channel = if test. contains ( "#![feature(" ) {
207
223
"&version=nightly"
208
224
} else {
209
225
""
210
226
} ;
211
227
212
- let edition_string = if let Some ( e @ Edition :: Edition2018 ) = edition {
213
- format ! ( "&edition={}{}" , e,
214
- if channel == "&version=nightly" { "" }
215
- else { "&version=nightly" } )
216
- } else if let Some ( e) = edition {
217
- format ! ( "&edition={}" , e)
218
- } else {
219
- "" . to_owned ( )
220
- } ;
228
+ let edition_string = format ! ( "&edition={}" , edition) ;
221
229
222
230
// These characters don't need to be escaped in a URI.
223
231
// FIXME: use a library function for percent encoding.
@@ -247,8 +255,8 @@ impl<'a, I: Iterator<Item = Event<'a>>> Iterator for CodeBlocks<'a, I> {
247
255
Some ( ( "This example is not tested" . to_owned ( ) , "ignore" ) )
248
256
} else if compile_fail {
249
257
Some ( ( "This example deliberately fails to compile" . to_owned ( ) , "compile_fail" ) )
250
- } else if let Some ( e ) = edition {
251
- Some ( ( format ! ( "This code runs with edition {}" , e ) , "edition" ) )
258
+ } else if explicit_edition {
259
+ Some ( ( format ! ( "This code runs with edition {}" , edition ) , "edition" ) )
252
260
} else {
253
261
None
254
262
} ;
@@ -259,7 +267,7 @@ impl<'a, I: Iterator<Item = Event<'a>>> Iterator for CodeBlocks<'a, I> {
259
267
Some ( & format ! ( "rust-example-rendered{}" ,
260
268
if ignore { " ignore" }
261
269
else if compile_fail { " compile_fail" }
262
- else if edition . is_some ( ) { " edition " }
270
+ else if explicit_edition { " edition " }
263
271
else { "" } ) ) ,
264
272
playground_button. as_ref ( ) . map ( String :: as_str) ,
265
273
Some ( ( s1. as_str ( ) , s2) ) ) ) ;
@@ -270,7 +278,7 @@ impl<'a, I: Iterator<Item = Event<'a>>> Iterator for CodeBlocks<'a, I> {
270
278
Some ( & format ! ( "rust-example-rendered{}" ,
271
279
if ignore { " ignore" }
272
280
else if compile_fail { " compile_fail" }
273
- else if edition . is_some ( ) { " edition " }
281
+ else if explicit_edition { " edition " }
274
282
else { "" } ) ) ,
275
283
playground_button. as_ref ( ) . map ( String :: as_str) ,
276
284
None ) ) ;
@@ -659,7 +667,7 @@ impl LangString {
659
667
660
668
impl < ' a > fmt:: Display for Markdown < ' a > {
661
669
fn fmt ( & self , fmt : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
662
- let Markdown ( md, links, ref ids, codes) = * self ;
670
+ let Markdown ( md, links, ref ids, codes, edition ) = * self ;
663
671
let mut ids = ids. borrow_mut ( ) ;
664
672
665
673
// This is actually common enough to special-case
@@ -678,7 +686,7 @@ impl<'a> fmt::Display for Markdown<'a> {
678
686
679
687
let p = HeadingLinks :: new ( p, None , & mut ids) ;
680
688
let p = LinkReplacer :: new ( p, links) ;
681
- let p = CodeBlocks :: new ( p, codes) ;
689
+ let p = CodeBlocks :: new ( p, codes, edition ) ;
682
690
let p = Footnotes :: new ( p) ;
683
691
html:: push_html ( & mut s, p) ;
684
692
@@ -688,7 +696,7 @@ impl<'a> fmt::Display for Markdown<'a> {
688
696
689
697
impl < ' a > fmt:: Display for MarkdownWithToc < ' a > {
690
698
fn fmt ( & self , fmt : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
691
- let MarkdownWithToc ( md, ref ids, codes) = * self ;
699
+ let MarkdownWithToc ( md, ref ids, codes, edition ) = * self ;
692
700
let mut ids = ids. borrow_mut ( ) ;
693
701
694
702
let p = Parser :: new_ext ( md, opts ( ) ) ;
@@ -699,7 +707,7 @@ impl<'a> fmt::Display for MarkdownWithToc<'a> {
699
707
700
708
{
701
709
let p = HeadingLinks :: new ( p, Some ( & mut toc) , & mut ids) ;
702
- let p = CodeBlocks :: new ( p, codes) ;
710
+ let p = CodeBlocks :: new ( p, codes, edition ) ;
703
711
let p = Footnotes :: new ( p) ;
704
712
html:: push_html ( & mut s, p) ;
705
713
}
@@ -712,7 +720,7 @@ impl<'a> fmt::Display for MarkdownWithToc<'a> {
712
720
713
721
impl < ' a > fmt:: Display for MarkdownHtml < ' a > {
714
722
fn fmt ( & self , fmt : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
715
- let MarkdownHtml ( md, ref ids, codes) = * self ;
723
+ let MarkdownHtml ( md, ref ids, codes, edition ) = * self ;
716
724
let mut ids = ids. borrow_mut ( ) ;
717
725
718
726
// This is actually common enough to special-case
@@ -728,7 +736,7 @@ impl<'a> fmt::Display for MarkdownHtml<'a> {
728
736
let mut s = String :: with_capacity ( md. len ( ) * 3 / 2 ) ;
729
737
730
738
let p = HeadingLinks :: new ( p, None , & mut ids) ;
731
- let p = CodeBlocks :: new ( p, codes) ;
739
+ let p = CodeBlocks :: new ( p, codes, edition ) ;
732
740
let p = Footnotes :: new ( p) ;
733
741
html:: push_html ( & mut s, p) ;
734
742
@@ -1046,7 +1054,7 @@ mod tests {
1046
1054
use super :: { ErrorCodes , LangString , Markdown , MarkdownHtml , IdMap } ;
1047
1055
use super :: plain_summary_line;
1048
1056
use std:: cell:: RefCell ;
1049
- use syntax:: edition:: Edition ;
1057
+ use syntax:: edition:: { Edition , DEFAULT_EDITION } ;
1050
1058
1051
1059
#[ test]
1052
1060
fn test_lang_string_parse ( ) {
@@ -1098,7 +1106,8 @@ mod tests {
1098
1106
fn test_header ( ) {
1099
1107
fn t ( input : & str , expect : & str ) {
1100
1108
let mut map = IdMap :: new ( ) ;
1101
- let output = Markdown ( input, & [ ] , RefCell :: new ( & mut map) , ErrorCodes :: Yes ) . to_string ( ) ;
1109
+ let output = Markdown ( input, & [ ] , RefCell :: new ( & mut map) ,
1110
+ ErrorCodes :: Yes , DEFAULT_EDITION ) . to_string ( ) ;
1102
1111
assert_eq ! ( output, expect, "original: {}" , input) ;
1103
1112
}
1104
1113
@@ -1120,7 +1129,8 @@ mod tests {
1120
1129
fn test_header_ids_multiple_blocks ( ) {
1121
1130
let mut map = IdMap :: new ( ) ;
1122
1131
fn t ( map : & mut IdMap , input : & str , expect : & str ) {
1123
- let output = Markdown ( input, & [ ] , RefCell :: new ( map) , ErrorCodes :: Yes ) . to_string ( ) ;
1132
+ let output = Markdown ( input, & [ ] , RefCell :: new ( map) ,
1133
+ ErrorCodes :: Yes , DEFAULT_EDITION ) . to_string ( ) ;
1124
1134
assert_eq ! ( output, expect, "original: {}" , input) ;
1125
1135
}
1126
1136
@@ -1157,7 +1167,8 @@ mod tests {
1157
1167
fn test_markdown_html_escape ( ) {
1158
1168
fn t ( input : & str , expect : & str ) {
1159
1169
let mut idmap = IdMap :: new ( ) ;
1160
- let output = MarkdownHtml ( input, RefCell :: new ( & mut idmap) , ErrorCodes :: Yes ) . to_string ( ) ;
1170
+ let output = MarkdownHtml ( input, RefCell :: new ( & mut idmap) ,
1171
+ ErrorCodes :: Yes , DEFAULT_EDITION ) . to_string ( ) ;
1161
1172
assert_eq ! ( output, expect, "original: {}" , input) ;
1162
1173
}
1163
1174
0 commit comments