Add support for playground language choice
We add a config option under the `Playground` type to allow setting which language should work with playground. Any language outside of rust doesn't get any preferential treatment in terms of code hiding, but atleast we get support at all. Looks like other languages being able to *use* the playground comes down to creating their own code in `book.js` after that
This commit is contained in:
parent
5921f59817
commit
e2b06cb6e7
|
@ -630,6 +630,9 @@ pub struct Playground {
|
||||||
pub copy_js: bool,
|
pub copy_js: bool,
|
||||||
/// Display line numbers on playground snippets. Default: `false`.
|
/// Display line numbers on playground snippets. Default: `false`.
|
||||||
pub line_numbers: bool,
|
pub line_numbers: bool,
|
||||||
|
/// Set's the language the playground will work with
|
||||||
|
/// TODO: Turn this into an array when there's support for multiple languages
|
||||||
|
pub language: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Default for Playground {
|
impl Default for Playground {
|
||||||
|
@ -639,6 +642,7 @@ impl Default for Playground {
|
||||||
copyable: true,
|
copyable: true,
|
||||||
copy_js: true,
|
copy_js: true,
|
||||||
line_numbers: false,
|
line_numbers: false,
|
||||||
|
language: "rust".to_string(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -748,6 +752,7 @@ mod tests {
|
||||||
[output.html.playground]
|
[output.html.playground]
|
||||||
editable = true
|
editable = true
|
||||||
editor = "ace"
|
editor = "ace"
|
||||||
|
language = "rust"
|
||||||
|
|
||||||
[output.html.redirect]
|
[output.html.redirect]
|
||||||
"index.html" = "overview.html"
|
"index.html" = "overview.html"
|
||||||
|
@ -781,6 +786,7 @@ mod tests {
|
||||||
copyable: true,
|
copyable: true,
|
||||||
copy_js: true,
|
copy_js: true,
|
||||||
line_numbers: false,
|
line_numbers: false,
|
||||||
|
language: "rust".to_string(),
|
||||||
};
|
};
|
||||||
let html_should_be = HtmlConfig {
|
let html_should_be = HtmlConfig {
|
||||||
curly_quotes: true,
|
curly_quotes: true,
|
||||||
|
|
|
@ -824,8 +824,32 @@ fn add_playground_pre(
|
||||||
let text = &caps[1];
|
let text = &caps[1];
|
||||||
let classes = &caps[2];
|
let classes = &caps[2];
|
||||||
let code = &caps[3];
|
let code = &caps[3];
|
||||||
|
let lang_class = format!("language-{}", playground_config.language);
|
||||||
|
|
||||||
if classes.contains("language-rust") {
|
if classes.contains(lang_class.as_str()) {
|
||||||
|
if playground_config.language == "rust" {
|
||||||
|
add_playground_pre_rust(playground_config, edition, classes, text, code)
|
||||||
|
} else {
|
||||||
|
format!(
|
||||||
|
"<pre class=\"playground\"><code class=\"{}\">{}</code></pre>",
|
||||||
|
classes, code
|
||||||
|
)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// Language doens't match playground config, so no-op
|
||||||
|
text.to_owned()
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.into_owned()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn add_playground_pre_rust(
|
||||||
|
playground_config: &Playground,
|
||||||
|
edition: Option<RustEdition>,
|
||||||
|
classes: &str,
|
||||||
|
text: &str,
|
||||||
|
code: &str,
|
||||||
|
) -> String {
|
||||||
if (!classes.contains("ignore")
|
if (!classes.contains("ignore")
|
||||||
&& !classes.contains("noplayground")
|
&& !classes.contains("noplayground")
|
||||||
&& !classes.contains("noplaypen"))
|
&& !classes.contains("noplaypen"))
|
||||||
|
@ -845,12 +869,12 @@ fn add_playground_pre(
|
||||||
None => "",
|
None => "",
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
let all_classes = format!("{}{}", classes, edition_class);
|
||||||
|
|
||||||
// wrap the contents in an external pre block
|
// wrap the contents in an external pre block
|
||||||
format!(
|
format!(
|
||||||
"<pre class=\"playground\"><code class=\"{}{}\">{}</code></pre>",
|
"<pre class=\"playground\"><code class=\"{}\">{}</code></pre>",
|
||||||
classes,
|
all_classes,
|
||||||
edition_class,
|
|
||||||
{
|
{
|
||||||
let content: Cow<'_, str> = if playground_config.editable
|
let content: Cow<'_, str> = if playground_config.editable
|
||||||
&& classes.contains("editable")
|
&& classes.contains("editable")
|
||||||
|
@ -862,11 +886,7 @@ fn add_playground_pre(
|
||||||
// we need to inject our own main
|
// we need to inject our own main
|
||||||
let (attrs, code) = partition_source(code);
|
let (attrs, code) = partition_source(code);
|
||||||
|
|
||||||
format!(
|
format!("\n# #![allow(unused)]\n{}#fn main() {{\n{}#}}", attrs, code).into()
|
||||||
"\n# #![allow(unused)]\n{}#fn main() {{\n{}#}}",
|
|
||||||
attrs, code
|
|
||||||
)
|
|
||||||
.into()
|
|
||||||
};
|
};
|
||||||
hide_lines(&content)
|
hide_lines(&content)
|
||||||
}
|
}
|
||||||
|
@ -874,12 +894,6 @@ fn add_playground_pre(
|
||||||
} else {
|
} else {
|
||||||
format!("<code class=\"{}\">{}</code>", classes, hide_lines(code))
|
format!("<code class=\"{}\">{}</code>", classes, hide_lines(code))
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
// not language-rust, so no-op
|
|
||||||
text.to_owned()
|
|
||||||
}
|
|
||||||
})
|
|
||||||
.into_owned()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
lazy_static! {
|
lazy_static! {
|
||||||
|
|
Loading…
Reference in New Issue