Accept nightly examples.

This also brings us to parity with rustdoc regarding attributes in
general; while this PR was focused on enabling nightly, that was a
happy accident.
This commit is contained in:
steveklabnik 2017-03-31 17:06:03 -04:00
parent 35e2807138
commit 80f01d70c6
2 changed files with 38 additions and 4 deletions

View File

@ -318,11 +318,11 @@ fn add_playpen_pre(html: String) -> String {
format!("<pre class=\"playpen\">{}</pre>", text)
} else {
// we need to inject our own main
let (attrs, code) = partition_source(code);
format!("<pre class=\"playpen\"><code class=\"{}\"># #![allow(unused_variables)]
#
#fn main() {{
{}#fn main() {{
{}
#}}</code></pre>", classes, code)
#}}</code></pre>", classes, attrs, code)
}
} else {
// not language-rust, so no-op
@ -330,3 +330,25 @@ fn add_playpen_pre(html: String) -> String {
}
}).into_owned()
}
fn partition_source(s: &str) -> (String, String) {
let mut after_header = false;
let mut before = String::new();
let mut after = String::new();
for line in s.lines() {
let trimline = line.trim();
let header = trimline.chars().all(|c| c.is_whitespace()) ||
trimline.starts_with("#![");
if !header || after_header {
after_header = true;
after.push_str(line);
after.push_str("\n");
} else {
before.push_str(line);
before.push_str("\n");
}
}
(before, after)
}

View File

@ -208,6 +208,18 @@ function run_rust_code(code_block) {
result_block = code_block.find(".result");
}
let text = code_block.find(".language-rust").text();
let params = {
version: "stable",
optimize: "0",
code: text,
};
if(text.includes("#![feature")) {
params.version = "nightly";
}
result_block.text("Running...");
$.ajax({
@ -216,7 +228,7 @@ function run_rust_code(code_block) {
crossDomain: true,
dataType: "json",
contentType: "application/json",
data: JSON.stringify({version: "stable", optimize: "0", code: code_block.find(".language-rust").text() }),
data: JSON.stringify(params),
success: function(response){
result_block.text(response.result);
}