Skip to content

Commit c5f9625

Browse files
committed
inject main
1 parent 79f00ee commit c5f9625

File tree

1 file changed

+11
-2
lines changed

1 file changed

+11
-2
lines changed

src/renderer/html_handlebars/hbs_renderer.rs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -294,14 +294,23 @@ fn fix_code_blocks(html: String) -> String {
294294
}
295295

296296
fn add_playpen_pre(html: String) -> String {
297-
let regex = Regex::new(r##"((?s)<code[^>]?class="([^"]+)".*?>.*?</code>)"##).unwrap();
297+
let regex = Regex::new(r##"((?s)<code[^>]?class="([^"]+)".*?>(.*?)</code>)"##).unwrap();
298298
regex.replace_all(&html, |caps: &Captures| {
299299
let text = &caps[1];
300300
let classes = &caps[2];
301+
let code = &caps[3];
301302

302303
if classes.contains("language-rust") && !classes.contains("ignore") {
303304
// wrap the contents in an external pre block
304-
format!("<pre class=\"playpen\">{}</pre>", text)
305+
306+
if text.contains("fn main") {
307+
format!("<pre class=\"playpen\">{}</pre>", text)
308+
} else {
309+
// we need to inject our own main
310+
format!("<pre class=\"playpen\"><code class=\"{}\">#fn main() {{
311+
{}
312+
#}}</code></pre>", classes, code)
313+
}
305314
} else {
306315
// not language-rust, so no-op
307316
format!("{}", text)

0 commit comments

Comments
 (0)