Skip to content

Commit b01e0e4

Browse files
committed
Add a documentation banner for edition specific code
1 parent ac8d8d7 commit b01e0e4

File tree

1 file changed

+44
-13
lines changed

1 file changed

+44
-13
lines changed

src/librustdoc/html/markdown.rs

Lines changed: 44 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -171,13 +171,15 @@ impl<'a, I: Iterator<Item = Event<'a>>> Iterator for CodeBlocks<'a, I> {
171171
let event = self.inner.next();
172172
let compile_fail;
173173
let ignore;
174+
let edition;
174175
if let Some(Event::Start(Tag::CodeBlock(lang))) = event {
175176
let parse_result = LangString::parse(&lang, self.check_error_codes);
176177
if !parse_result.rust {
177178
return Some(Event::Start(Tag::CodeBlock(lang)));
178179
}
179180
compile_fail = parse_result.compile_fail;
180181
ignore = parse_result.ignore;
182+
edition = parse_result.edition;
181183
} else {
182184
return event;
183185
}
@@ -213,6 +215,17 @@ impl<'a, I: Iterator<Item = Event<'a>>> Iterator for CodeBlocks<'a, I> {
213215
} else {
214216
""
215217
};
218+
219+
let edition_string = if let Some(e @ Edition::Edition2018) = edition {
220+
format!("&amp;edition={}{}", e,
221+
if channel == "&amp;version=nightly" { "" }
222+
else { "&amp;version=nightly" })
223+
} else if let Some(e) = edition {
224+
format!("&amp;edition={}", e)
225+
} else {
226+
"".to_owned()
227+
};
228+
216229
// These characters don't need to be escaped in a URI.
217230
// FIXME: use a library function for percent encoding.
218231
fn dont_escape(c: u8) -> bool {
@@ -232,26 +245,44 @@ impl<'a, I: Iterator<Item = Event<'a>>> Iterator for CodeBlocks<'a, I> {
232245
}
233246
}
234247
Some(format!(
235-
r#"<a class="test-arrow" target="_blank" href="{}?code={}{}">Run</a>"#,
236-
url, test_escaped, channel
248+
r#"<a class="test-arrow" target="_blank" href="{}?code={}{}{}">Run</a>"#,
249+
url, test_escaped, channel, edition_string
237250
))
238251
});
252+
239253
let tooltip = if ignore {
240-
Some(("This example is not tested", "ignore"))
254+
Some(("This example is not tested".to_owned(), "ignore"))
241255
} else if compile_fail {
242-
Some(("This example deliberately fails to compile", "compile_fail"))
256+
Some(("This example deliberately fails to compile".to_owned(), "compile_fail"))
257+
} else if let Some(e) = edition {
258+
Some((format!("This code runs with edition {}", e), "edition"))
243259
} else {
244260
None
245261
};
246-
s.push_str(&highlight::render_with_highlighting(
247-
&text,
248-
Some(&format!("rust-example-rendered{}",
249-
if ignore { " ignore" }
250-
else if compile_fail { " compile_fail" }
251-
else { "" })),
252-
playground_button.as_ref().map(String::as_str),
253-
tooltip));
254-
Some(Event::Html(s.into()))
262+
263+
if let Some((s1, s2)) = tooltip {
264+
s.push_str(&highlight::render_with_highlighting(
265+
&text,
266+
Some(&format!("rust-example-rendered{}",
267+
if ignore { " ignore" }
268+
else if compile_fail { " compile_fail" }
269+
else if edition.is_some() { " edition " }
270+
else { "" })),
271+
playground_button.as_ref().map(String::as_str),
272+
Some((s1.as_str(), s2))));
273+
Some(Event::Html(s.into()))
274+
} else {
275+
s.push_str(&highlight::render_with_highlighting(
276+
&text,
277+
Some(&format!("rust-example-rendered{}",
278+
if ignore { " ignore" }
279+
else if compile_fail { " compile_fail" }
280+
else if edition.is_some() { " edition " }
281+
else { "" })),
282+
playground_button.as_ref().map(String::as_str),
283+
None));
284+
Some(Event::Html(s.into()))
285+
}
255286
})
256287
}
257288
}

0 commit comments

Comments
 (0)