Merge pull request #243 from steveklabnik/gh241

Accept nightly examples.
This commit is contained in:
Steve Klabnik 2017-04-14 15:19:07 -04:00 committed by GitHub
commit 9cb232058b
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) format!("<pre class=\"playpen\">{}</pre>", text)
} else { } else {
// we need to inject our own main // we need to inject our own main
let (attrs, code) = partition_source(code);
format!("<pre class=\"playpen\"><code class=\"{}\"># #![allow(unused_variables)] format!("<pre class=\"playpen\"><code class=\"{}\"># #![allow(unused_variables)]
# {}#fn main() {{
#fn main() {{
{} {}
#}}</code></pre>", classes, code) #}}</code></pre>", classes, attrs, code)
} }
} else { } else {
// not language-rust, so no-op // not language-rust, so no-op
@ -330,3 +330,25 @@ fn add_playpen_pre(html: String) -> String {
} }
}).into_owned() }).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"); 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..."); result_block.text("Running...");
$.ajax({ $.ajax({
@ -216,7 +228,7 @@ function run_rust_code(code_block) {
crossDomain: true, crossDomain: true,
dataType: "json", dataType: "json",
contentType: "application/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){ success: function(response){
result_block.text(response.result); result_block.text(response.result);
} }