forked from dlang/dlang.org
-
Notifications
You must be signed in to change notification settings - Fork 0
/
run_examples.js
101 lines (91 loc) · 3.85 KB
/
run_examples.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
/**
* Run all unittest examples
*
* Copyright 2016 by D Language Foundation
*
* License: http://boost.org/LICENSE_1_0.txt, Boost License 1.0
*/
// turns asserts into writeln
function reformatExample(code) {
return code.replace(/(<span class="(?:d_keyword|kwd)">assert<\/span>(?:<span class="pun">)?\((.*)==(.*)\);)+/g, function(match, text, left, right) {
return "writeln(" + left.trim() + "); "
+ "<span class='d_comment'>// " + right.trim() + "</span>";
});
}
// wraps a unittest into a runnable script
function wrapIntoMain(code) {
var currentPackage = $('body')[0].id;
var codeOut = "";
// dynamically wrap into main if needed
if (code.indexOf("void main") >= 0) {
codeOut = "import " + currentPackage + "; ";
codeOut += "#line 1\n";
codeOut += code;
}
else {
var codeOut = "void main()\n{\n";
codeOut += " import " + currentPackage + ";\n";
// writing to the stdout is probably often used
codeOut += (currentPackage == "std.file") ? " import std.stdio: writeln, writef, writefln;\n " : " import std.stdio: write, writeln, writef, writefln;\n ";
codeOut += "#line 1\n";
codeOut += code.split("\n").join("\n ");
codeOut += "\n}";
}
return codeOut;
}
$(document).ready(function()
{
if ($('body')[0].id == "Home")
return;
// only for std at the moment
if (!$('body').hasClass("std"))
return;
// first selector is for ddoc - second for ddox
var codeBlocks = $('pre[class~=d_code]').add('pre[class~=code]');
codeBlocks.each(function(index)
{
var currentExample = $(this);
var orig = currentExample.html();
// disable regex assert -> writeln rewrite logic (for now)
//orig = reformatExample(orig);
// check whether it is from a ddoced unittest
// 1) check is for ddoc, 2) for ddox
// manual created tests most likely can't be run without modifications
if (!($(this).parent().parent().prev().hasClass("dlang_runnable") ||
$(this).prev().children(":last").hasClass("dlang_runnable")))
return;
currentExample.replaceWith(
'<div class="unittest_examples">'
+ '<div class="d_code">'
+ '<pre class="d_code">'+orig+'</pre>'
+ '</div>'
+ '<div class="d_run_code" style="display: block">'
+ '<textarea class="d_code" style="display: none;"></textarea>'
+ '</div>'
+ '<div class="d_example_buttons">'
+ '<div class="editButton"><i class="fa fa-edit" aria-hidden="true"></i> Edit</div>'
+ '<div class="runButton"><i class="fa fa-play" aria-hidden="true"></i> Run</div>'
+ '<div class="resetButton" style="display:none"><i class="fa fa-undo " aria-hidden="true"></i> Reset</div>'
+ '<div class="openInEditorButton" title="Open in an external editor"><i class="fa fa-external-link" aria-hidden="true"></i>Open in IDE</div>'
+ '</div>'
+ '<div class="d_code_output"><span class="d_code_title">Application output</span><br><pre class="d_code_output" readonly>Running...</pre>'
+ '</div>'
);
});
$('textarea[class=d_code]').each(function(index) {
var parent = $(this).parent();
var btnParent = parent.parent().children(".d_example_buttons");
var outputDiv = parent.parent().children(".d_code_output");
var editor = setupTextarea(this, {
parent: btnParent,
outputDiv: outputDiv,
stdin: false,
args: false,
transformOutput: wrapIntoMain,
defaultOutput: "All tests passed",
keepCode: true,
outputHeight: "auto",
backend: "tour"
});
});
});