@@ -42,14 +42,21 @@ fn opts() -> Options {
42
42
/// A unit struct which has the `fmt::Display` trait implemented. When
43
43
/// formatted, this struct will emit the HTML corresponding to the rendered
44
44
/// version of the contained markdown string.
45
- /// The second parameter is a list of link replacements
45
+ ///
46
+ /// The second parameter is a list of link replacements.
47
+ ///
48
+ /// The third is the current list of used header IDs.
49
+ ///
50
+ /// The fourth is whether to allow the use of explicit error codes in doctest lang strings.
51
+ ///
52
+ /// The fifth is what default edition to use when parsing doctests (to add a `fn main`).
46
53
pub struct Markdown < ' a > (
47
- pub & ' a str , pub & ' a [ ( String , String ) ] , pub RefCell < & ' a mut IdMap > , pub ErrorCodes ) ;
54
+ pub & ' a str , pub & ' a [ ( String , String ) ] , pub RefCell < & ' a mut IdMap > , pub ErrorCodes , pub Edition ) ;
48
55
/// A unit struct like `Markdown`, that renders the markdown with a
49
56
/// table of contents.
50
- pub struct MarkdownWithToc < ' a > ( pub & ' a str , pub RefCell < & ' a mut IdMap > , pub ErrorCodes ) ;
57
+ pub struct MarkdownWithToc < ' a > ( pub & ' a str , pub RefCell < & ' a mut IdMap > , pub ErrorCodes , pub Edition ) ;
51
58
/// 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 ) ;
59
+ pub struct MarkdownHtml < ' a > ( pub & ' a str , pub RefCell < & ' a mut IdMap > , pub ErrorCodes , pub Edition ) ;
53
60
/// A unit struct like `Markdown`, that renders only the first paragraph.
54
61
pub struct MarkdownSummaryLine < ' a > ( pub & ' a str , pub & ' a [ ( String , String ) ] ) ;
55
62
@@ -146,13 +153,15 @@ thread_local!(pub static PLAYGROUND: RefCell<Option<(Option<String>, String)>> =
146
153
struct CodeBlocks < ' a , I : Iterator < Item = Event < ' a > > > {
147
154
inner : I ,
148
155
check_error_codes : ErrorCodes ,
156
+ edition : Edition ,
149
157
}
150
158
151
159
impl < ' a , I : Iterator < Item = Event < ' a > > > CodeBlocks < ' a , I > {
152
- fn new ( iter : I , error_codes : ErrorCodes ) -> Self {
160
+ fn new ( iter : I , error_codes : ErrorCodes , edition : Edition ) -> Self {
153
161
CodeBlocks {
154
162
inner : iter,
155
163
check_error_codes : error_codes,
164
+ edition,
156
165
}
157
166
}
158
167
}
@@ -177,6 +186,9 @@ impl<'a, I: Iterator<Item = Event<'a>>> Iterator for CodeBlocks<'a, I> {
177
186
return event;
178
187
}
179
188
189
+ let explicit_edition = edition. is_some ( ) ;
190
+ let edition = edition. unwrap_or ( self . edition ) ;
191
+
180
192
let mut origtext = String :: new ( ) ;
181
193
for event in & mut self . inner {
182
194
match event {
@@ -202,22 +214,14 @@ impl<'a, I: Iterator<Item = Event<'a>>> Iterator for CodeBlocks<'a, I> {
202
214
. collect :: < Vec < Cow < ' _ , str > > > ( ) . join ( "\n " ) ;
203
215
let krate = krate. as_ref ( ) . map ( |s| & * * s) ;
204
216
let ( test, _) = test:: make_test ( & test, krate, false ,
205
- & Default :: default ( ) ) ;
217
+ & Default :: default ( ) , edition ) ;
206
218
let channel = if test. contains ( "#![feature(" ) {
207
219
"&version=nightly"
208
220
} else {
209
221
""
210
222
} ;
211
223
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
- } ;
224
+ let edition_string = format ! ( "&edition={}" , edition) ;
221
225
222
226
// These characters don't need to be escaped in a URI.
223
227
// FIXME: use a library function for percent encoding.
@@ -247,8 +251,8 @@ impl<'a, I: Iterator<Item = Event<'a>>> Iterator for CodeBlocks<'a, I> {
247
251
Some ( ( "This example is not tested" . to_owned ( ) , "ignore" ) )
248
252
} else if compile_fail {
249
253
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" ) )
254
+ } else if explicit_edition {
255
+ Some ( ( format ! ( "This code runs with edition {}" , edition ) , "edition" ) )
252
256
} else {
253
257
None
254
258
} ;
@@ -259,7 +263,7 @@ impl<'a, I: Iterator<Item = Event<'a>>> Iterator for CodeBlocks<'a, I> {
259
263
Some ( & format ! ( "rust-example-rendered{}" ,
260
264
if ignore { " ignore" }
261
265
else if compile_fail { " compile_fail" }
262
- else if edition . is_some ( ) { " edition " }
266
+ else if explicit_edition { " edition " }
263
267
else { "" } ) ) ,
264
268
playground_button. as_ref ( ) . map ( String :: as_str) ,
265
269
Some ( ( s1. as_str ( ) , s2) ) ) ) ;
@@ -270,7 +274,7 @@ impl<'a, I: Iterator<Item = Event<'a>>> Iterator for CodeBlocks<'a, I> {
270
274
Some ( & format ! ( "rust-example-rendered{}" ,
271
275
if ignore { " ignore" }
272
276
else if compile_fail { " compile_fail" }
273
- else if edition . is_some ( ) { " edition " }
277
+ else if explicit_edition { " edition " }
274
278
else { "" } ) ) ,
275
279
playground_button. as_ref ( ) . map ( String :: as_str) ,
276
280
None ) ) ;
@@ -659,7 +663,7 @@ impl LangString {
659
663
660
664
impl < ' a > fmt:: Display for Markdown < ' a > {
661
665
fn fmt ( & self , fmt : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
662
- let Markdown ( md, links, ref ids, codes) = * self ;
666
+ let Markdown ( md, links, ref ids, codes, edition ) = * self ;
663
667
let mut ids = ids. borrow_mut ( ) ;
664
668
665
669
// This is actually common enough to special-case
@@ -678,7 +682,7 @@ impl<'a> fmt::Display for Markdown<'a> {
678
682
679
683
let p = HeadingLinks :: new ( p, None , & mut ids) ;
680
684
let p = LinkReplacer :: new ( p, links) ;
681
- let p = CodeBlocks :: new ( p, codes) ;
685
+ let p = CodeBlocks :: new ( p, codes, edition ) ;
682
686
let p = Footnotes :: new ( p) ;
683
687
html:: push_html ( & mut s, p) ;
684
688
@@ -688,7 +692,7 @@ impl<'a> fmt::Display for Markdown<'a> {
688
692
689
693
impl < ' a > fmt:: Display for MarkdownWithToc < ' a > {
690
694
fn fmt ( & self , fmt : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
691
- let MarkdownWithToc ( md, ref ids, codes) = * self ;
695
+ let MarkdownWithToc ( md, ref ids, codes, edition ) = * self ;
692
696
let mut ids = ids. borrow_mut ( ) ;
693
697
694
698
let p = Parser :: new_ext ( md, opts ( ) ) ;
@@ -699,7 +703,7 @@ impl<'a> fmt::Display for MarkdownWithToc<'a> {
699
703
700
704
{
701
705
let p = HeadingLinks :: new ( p, Some ( & mut toc) , & mut ids) ;
702
- let p = CodeBlocks :: new ( p, codes) ;
706
+ let p = CodeBlocks :: new ( p, codes, edition ) ;
703
707
let p = Footnotes :: new ( p) ;
704
708
html:: push_html ( & mut s, p) ;
705
709
}
@@ -712,7 +716,7 @@ impl<'a> fmt::Display for MarkdownWithToc<'a> {
712
716
713
717
impl < ' a > fmt:: Display for MarkdownHtml < ' a > {
714
718
fn fmt ( & self , fmt : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
715
- let MarkdownHtml ( md, ref ids, codes) = * self ;
719
+ let MarkdownHtml ( md, ref ids, codes, edition ) = * self ;
716
720
let mut ids = ids. borrow_mut ( ) ;
717
721
718
722
// This is actually common enough to special-case
@@ -728,7 +732,7 @@ impl<'a> fmt::Display for MarkdownHtml<'a> {
728
732
let mut s = String :: with_capacity ( md. len ( ) * 3 / 2 ) ;
729
733
730
734
let p = HeadingLinks :: new ( p, None , & mut ids) ;
731
- let p = CodeBlocks :: new ( p, codes) ;
735
+ let p = CodeBlocks :: new ( p, codes, edition ) ;
732
736
let p = Footnotes :: new ( p) ;
733
737
html:: push_html ( & mut s, p) ;
734
738
0 commit comments