88//! ```
99//! #![feature(rustc_private)]
1010//!
11+ //! extern crate syntax;
12+ //!
13+ //! use syntax::edition::Edition;
1114//! use rustdoc::html::markdown::{IdMap, Markdown, ErrorCodes};
1215//! use std::cell::RefCell;
1316//!
1417//! let s = "My *markdown* _text_";
1518//! 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));
1721//! // ... something using html
1822//! ```
1923
@@ -42,14 +46,21 @@ fn opts() -> Options {
4246/// A unit struct which has the `fmt::Display` trait implemented. When
4347/// formatted, this struct will emit the HTML corresponding to the rendered
4448/// 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`).
4657pub 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 ) ;
4859/// A unit struct like `Markdown`, that renders the markdown with a
4960/// 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 ) ;
5162/// 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 ) ;
5364/// A unit struct like `Markdown`, that renders only the first paragraph.
5465pub struct MarkdownSummaryLine < ' a > ( pub & ' a str , pub & ' a [ ( String , String ) ] ) ;
5566
@@ -146,13 +157,15 @@ thread_local!(pub static PLAYGROUND: RefCell<Option<(Option<String>, String)>> =
146157struct CodeBlocks < ' a , I : Iterator < Item = Event < ' a > > > {
147158 inner : I ,
148159 check_error_codes : ErrorCodes ,
160+ edition : Edition ,
149161}
150162
151163impl < ' 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 {
153165 CodeBlocks {
154166 inner : iter,
155167 check_error_codes : error_codes,
168+ edition,
156169 }
157170 }
158171}
@@ -177,6 +190,9 @@ impl<'a, I: Iterator<Item = Event<'a>>> Iterator for CodeBlocks<'a, I> {
177190 return event;
178191 }
179192
193+ let explicit_edition = edition. is_some ( ) ;
194+ let edition = edition. unwrap_or ( self . edition ) ;
195+
180196 let mut origtext = String :: new ( ) ;
181197 for event in & mut self . inner {
182198 match event {
@@ -202,22 +218,14 @@ impl<'a, I: Iterator<Item = Event<'a>>> Iterator for CodeBlocks<'a, I> {
202218 . collect :: < Vec < Cow < ' _ , str > > > ( ) . join ( "\n " ) ;
203219 let krate = krate. as_ref ( ) . map ( |s| & * * s) ;
204220 let ( test, _) = test:: make_test ( & test, krate, false ,
205- & Default :: default ( ) ) ;
221+ & Default :: default ( ) , edition ) ;
206222 let channel = if test. contains ( "#![feature(" ) {
207223 "&version=nightly"
208224 } else {
209225 ""
210226 } ;
211227
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) ;
221229
222230 // These characters don't need to be escaped in a URI.
223231 // FIXME: use a library function for percent encoding.
@@ -247,8 +255,8 @@ impl<'a, I: Iterator<Item = Event<'a>>> Iterator for CodeBlocks<'a, I> {
247255 Some ( ( "This example is not tested" . to_owned ( ) , "ignore" ) )
248256 } else if compile_fail {
249257 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" ) )
252260 } else {
253261 None
254262 } ;
@@ -259,7 +267,7 @@ impl<'a, I: Iterator<Item = Event<'a>>> Iterator for CodeBlocks<'a, I> {
259267 Some ( & format ! ( "rust-example-rendered{}" ,
260268 if ignore { " ignore" }
261269 else if compile_fail { " compile_fail" }
262- else if edition . is_some ( ) { " edition " }
270+ else if explicit_edition { " edition " }
263271 else { "" } ) ) ,
264272 playground_button. as_ref ( ) . map ( String :: as_str) ,
265273 Some ( ( s1. as_str ( ) , s2) ) ) ) ;
@@ -270,7 +278,7 @@ impl<'a, I: Iterator<Item = Event<'a>>> Iterator for CodeBlocks<'a, I> {
270278 Some ( & format ! ( "rust-example-rendered{}" ,
271279 if ignore { " ignore" }
272280 else if compile_fail { " compile_fail" }
273- else if edition . is_some ( ) { " edition " }
281+ else if explicit_edition { " edition " }
274282 else { "" } ) ) ,
275283 playground_button. as_ref ( ) . map ( String :: as_str) ,
276284 None ) ) ;
@@ -659,7 +667,7 @@ impl LangString {
659667
660668impl < ' a > fmt:: Display for Markdown < ' a > {
661669 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 ;
663671 let mut ids = ids. borrow_mut ( ) ;
664672
665673 // This is actually common enough to special-case
@@ -678,7 +686,7 @@ impl<'a> fmt::Display for Markdown<'a> {
678686
679687 let p = HeadingLinks :: new ( p, None , & mut ids) ;
680688 let p = LinkReplacer :: new ( p, links) ;
681- let p = CodeBlocks :: new ( p, codes) ;
689+ let p = CodeBlocks :: new ( p, codes, edition ) ;
682690 let p = Footnotes :: new ( p) ;
683691 html:: push_html ( & mut s, p) ;
684692
@@ -688,7 +696,7 @@ impl<'a> fmt::Display for Markdown<'a> {
688696
689697impl < ' a > fmt:: Display for MarkdownWithToc < ' a > {
690698 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 ;
692700 let mut ids = ids. borrow_mut ( ) ;
693701
694702 let p = Parser :: new_ext ( md, opts ( ) ) ;
@@ -699,7 +707,7 @@ impl<'a> fmt::Display for MarkdownWithToc<'a> {
699707
700708 {
701709 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 ) ;
703711 let p = Footnotes :: new ( p) ;
704712 html:: push_html ( & mut s, p) ;
705713 }
@@ -712,7 +720,7 @@ impl<'a> fmt::Display for MarkdownWithToc<'a> {
712720
713721impl < ' a > fmt:: Display for MarkdownHtml < ' a > {
714722 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 ;
716724 let mut ids = ids. borrow_mut ( ) ;
717725
718726 // This is actually common enough to special-case
@@ -728,7 +736,7 @@ impl<'a> fmt::Display for MarkdownHtml<'a> {
728736 let mut s = String :: with_capacity ( md. len ( ) * 3 / 2 ) ;
729737
730738 let p = HeadingLinks :: new ( p, None , & mut ids) ;
731- let p = CodeBlocks :: new ( p, codes) ;
739+ let p = CodeBlocks :: new ( p, codes, edition ) ;
732740 let p = Footnotes :: new ( p) ;
733741 html:: push_html ( & mut s, p) ;
734742
@@ -1046,7 +1054,7 @@ mod tests {
10461054 use super :: { ErrorCodes , LangString , Markdown , MarkdownHtml , IdMap } ;
10471055 use super :: plain_summary_line;
10481056 use std:: cell:: RefCell ;
1049- use syntax:: edition:: Edition ;
1057+ use syntax:: edition:: { Edition , DEFAULT_EDITION } ;
10501058
10511059 #[ test]
10521060 fn test_lang_string_parse ( ) {
@@ -1098,7 +1106,8 @@ mod tests {
10981106 fn test_header ( ) {
10991107 fn t ( input : & str , expect : & str ) {
11001108 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 ( ) ;
11021111 assert_eq ! ( output, expect, "original: {}" , input) ;
11031112 }
11041113
@@ -1120,7 +1129,8 @@ mod tests {
11201129 fn test_header_ids_multiple_blocks ( ) {
11211130 let mut map = IdMap :: new ( ) ;
11221131 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 ( ) ;
11241134 assert_eq ! ( output, expect, "original: {}" , input) ;
11251135 }
11261136
@@ -1157,7 +1167,8 @@ mod tests {
11571167 fn test_markdown_html_escape ( ) {
11581168 fn t ( input : & str , expect : & str ) {
11591169 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 ( ) ;
11611172 assert_eq ! ( output, expect, "original: {}" , input) ;
11621173 }
11631174
0 commit comments