@@ -42,14 +42,21 @@ fn opts() -> Options {
4242/// A unit struct which has the `fmt::Display` trait implemented. When
4343/// formatted, this struct will emit the HTML corresponding to the rendered
4444/// 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`).
4653pub 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 ) ;
4855/// A unit struct like `Markdown`, that renders the markdown with a
4956/// 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 ) ;
5158/// 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 ) ;
5360/// A unit struct like `Markdown`, that renders only the first paragraph.
5461pub struct MarkdownSummaryLine < ' a > ( pub & ' a str , pub & ' a [ ( String , String ) ] ) ;
5562
@@ -146,13 +153,15 @@ thread_local!(pub static PLAYGROUND: RefCell<Option<(Option<String>, String)>> =
146153struct CodeBlocks < ' a , I : Iterator < Item = Event < ' a > > > {
147154 inner : I ,
148155 check_error_codes : ErrorCodes ,
156+ edition : Edition ,
149157}
150158
151159impl < ' 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 {
153161 CodeBlocks {
154162 inner : iter,
155163 check_error_codes : error_codes,
164+ edition,
156165 }
157166 }
158167}
@@ -177,6 +186,9 @@ impl<'a, I: Iterator<Item = Event<'a>>> Iterator for CodeBlocks<'a, I> {
177186 return event;
178187 }
179188
189+ let explicit_edition = edition. is_some ( ) ;
190+ let edition = edition. unwrap_or ( self . edition ) ;
191+
180192 let mut origtext = String :: new ( ) ;
181193 for event in & mut self . inner {
182194 match event {
@@ -202,22 +214,14 @@ impl<'a, I: Iterator<Item = Event<'a>>> Iterator for CodeBlocks<'a, I> {
202214 . collect :: < Vec < Cow < ' _ , str > > > ( ) . join ( "\n " ) ;
203215 let krate = krate. as_ref ( ) . map ( |s| & * * s) ;
204216 let ( test, _) = test:: make_test ( & test, krate, false ,
205- & Default :: default ( ) ) ;
217+ & Default :: default ( ) , edition ) ;
206218 let channel = if test. contains ( "#![feature(" ) {
207219 "&version=nightly"
208220 } else {
209221 ""
210222 } ;
211223
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) ;
221225
222226 // These characters don't need to be escaped in a URI.
223227 // FIXME: use a library function for percent encoding.
@@ -247,8 +251,8 @@ impl<'a, I: Iterator<Item = Event<'a>>> Iterator for CodeBlocks<'a, I> {
247251 Some ( ( "This example is not tested" . to_owned ( ) , "ignore" ) )
248252 } else if compile_fail {
249253 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" ) )
252256 } else {
253257 None
254258 } ;
@@ -259,7 +263,7 @@ impl<'a, I: Iterator<Item = Event<'a>>> Iterator for CodeBlocks<'a, I> {
259263 Some ( & format ! ( "rust-example-rendered{}" ,
260264 if ignore { " ignore" }
261265 else if compile_fail { " compile_fail" }
262- else if edition . is_some ( ) { " edition " }
266+ else if explicit_edition { " edition " }
263267 else { "" } ) ) ,
264268 playground_button. as_ref ( ) . map ( String :: as_str) ,
265269 Some ( ( s1. as_str ( ) , s2) ) ) ) ;
@@ -270,7 +274,7 @@ impl<'a, I: Iterator<Item = Event<'a>>> Iterator for CodeBlocks<'a, I> {
270274 Some ( & format ! ( "rust-example-rendered{}" ,
271275 if ignore { " ignore" }
272276 else if compile_fail { " compile_fail" }
273- else if edition . is_some ( ) { " edition " }
277+ else if explicit_edition { " edition " }
274278 else { "" } ) ) ,
275279 playground_button. as_ref ( ) . map ( String :: as_str) ,
276280 None ) ) ;
@@ -659,7 +663,7 @@ impl LangString {
659663
660664impl < ' a > fmt:: Display for Markdown < ' a > {
661665 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 ;
663667 let mut ids = ids. borrow_mut ( ) ;
664668
665669 // This is actually common enough to special-case
@@ -678,7 +682,7 @@ impl<'a> fmt::Display for Markdown<'a> {
678682
679683 let p = HeadingLinks :: new ( p, None , & mut ids) ;
680684 let p = LinkReplacer :: new ( p, links) ;
681- let p = CodeBlocks :: new ( p, codes) ;
685+ let p = CodeBlocks :: new ( p, codes, edition ) ;
682686 let p = Footnotes :: new ( p) ;
683687 html:: push_html ( & mut s, p) ;
684688
@@ -688,7 +692,7 @@ impl<'a> fmt::Display for Markdown<'a> {
688692
689693impl < ' a > fmt:: Display for MarkdownWithToc < ' a > {
690694 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 ;
692696 let mut ids = ids. borrow_mut ( ) ;
693697
694698 let p = Parser :: new_ext ( md, opts ( ) ) ;
@@ -699,7 +703,7 @@ impl<'a> fmt::Display for MarkdownWithToc<'a> {
699703
700704 {
701705 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 ) ;
703707 let p = Footnotes :: new ( p) ;
704708 html:: push_html ( & mut s, p) ;
705709 }
@@ -712,7 +716,7 @@ impl<'a> fmt::Display for MarkdownWithToc<'a> {
712716
713717impl < ' a > fmt:: Display for MarkdownHtml < ' a > {
714718 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 ;
716720 let mut ids = ids. borrow_mut ( ) ;
717721
718722 // This is actually common enough to special-case
@@ -728,7 +732,7 @@ impl<'a> fmt::Display for MarkdownHtml<'a> {
728732 let mut s = String :: with_capacity ( md. len ( ) * 3 / 2 ) ;
729733
730734 let p = HeadingLinks :: new ( p, None , & mut ids) ;
731- let p = CodeBlocks :: new ( p, codes) ;
735+ let p = CodeBlocks :: new ( p, codes, edition ) ;
732736 let p = Footnotes :: new ( p) ;
733737 html:: push_html ( & mut s, p) ;
734738
0 commit comments