From 80f01d70c6667258044af348b5803426e431cd06 Mon Sep 17 00:00:00 2001 From: steveklabnik Date: Fri, 31 Mar 2017 17:06:03 -0400 Subject: [PATCH] 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. --- src/renderer/html_handlebars/hbs_renderer.rs | 28 +++++++++++++++++--- src/theme/book.js | 14 +++++++++- 2 files changed, 38 insertions(+), 4 deletions(-) diff --git a/src/renderer/html_handlebars/hbs_renderer.rs b/src/renderer/html_handlebars/hbs_renderer.rs index 6d49f21d..4fac6869 100644 --- a/src/renderer/html_handlebars/hbs_renderer.rs +++ b/src/renderer/html_handlebars/hbs_renderer.rs @@ -318,11 +318,11 @@ fn add_playpen_pre(html: String) -> String { format!("
{}
", text) } else { // we need to inject our own main + let (attrs, code) = partition_source(code); format!("
# #![allow(unused_variables)]
-# 
-#fn main() {{
+{}#fn main() {{
 {}
-#}}
", classes, code) +#}}", 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) +} \ No newline at end of file diff --git a/src/theme/book.js b/src/theme/book.js index c3cbcfcc..52be9a9d 100644 --- a/src/theme/book.js +++ b/src/theme/book.js @@ -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); }