Skip to content

Commit 4139379

Browse files
wilzbach9il
authored andcommitted
Make all examples at the docs runnable like on dlang.org
1 parent afb7e9b commit 4139379

File tree

4 files changed

+100
-3
lines changed

4 files changed

+100
-3
lines changed

doc/Makefile

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,10 +107,10 @@ $(DOC_OUTPUT_DIR)/$(call D2HTML,$p) : $(call ADDSOURCE,$p) $(STDDOC) ;\
107107
IMAGES=images/mir.svg favicon.ico
108108

109109
JAVASCRIPT=$(addsuffix .js, $(addprefix js/, \
110-
dlang ddox listanchors run run-main-website jquery-1.7.2.min))
110+
codemirror-compressed dlang ddox listanchors run run_examples jquery-1.7.2.min))
111111

112112
STYLES=$(addsuffix .css, $(addprefix css/, \
113-
style print custom ))
113+
style print custom codemirror))
114114

115115
ALL_FILES = $(addprefix $(DOC_OUTPUT_DIR)/, \
116116
$(STYLES) $(IMAGES) $(JAVASCRIPT))
@@ -119,6 +119,10 @@ $(DOC_OUTPUT_DIR)/css/custom.css: $(DOC_SOURCE_DIR)/custom.css
119119
@mkdir -p $(dir $@)
120120
cp $< $@
121121

122+
$(DOC_OUTPUT_DIR)/js/run_examples.js: $(DOC_SOURCE_DIR)/run_examples_custom.js
123+
@mkdir -p $(dir $@)
124+
cp $< $@
125+
122126
$(DOC_OUTPUT_DIR)/images/mir.svg: $(ARTWORK_DIR)/logo/mir_site_logo.svg
123127
@mkdir -p $(dir $@)
124128
cp $< $@

doc/custom.ddoc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ $(COMMON_HEADERS_DLANG)
4545
<link rel="stylesheet" href="$(STATIC css/style.css)">
4646
<link rel="stylesheet" href="$(STATIC css/custom.css)">
4747
<link rel="stylesheet" href="$(STATIC css/print.css)" media="print">
48+
<link rel="stylesheet" href="$(STATIC css/codemirror.css)">
4849
<link rel="shortcut icon" href="$(FAVICON)">
4950
<meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=0.1, maximum-scale=10.0">
5051
$(EXTRA_HEADERS)
@@ -119,7 +120,9 @@ COMMON_SCRIPTS =
119120
$(COMMON_SCRIPTS_DLANG)
120121
_=
121122
COMMON_SCRIPTS_DLANG =
123+
$(SCRIPTLOAD $(STATIC js/codemirror-compressed.js))
122124
$(SCRIPTLOAD $(STATIC js/run.js))
125+
$(SCRIPTLOAD $(STATIC js/run_examples.js))
123126
_=
124127

125128
COMMON_HEADERS_DLANG=

doc/dlang.org

Submodule dlang.org updated 325 files

doc/run_examples_custom.js

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
/**
2+
* Run all unittest examples
3+
*
4+
* Copyright 2016 by D Language Foundation
5+
*
6+
* License: http://boost.org/LICENSE_1_0.txt, Boost License 1.0
7+
*/
8+
9+
// wraps a unittest into a runnable script
10+
function wrapIntoMain(code) {
11+
var currentPackage = $('body')[0].id;
12+
// BUMP mir-algorithm image here: https://github.com/dlang-tour/core-exec/blob/master/Dockerfile
13+
// run.dlang.io frontend: https://github.com/dlang-tour/core/blob/master/public/static/js/tour-controller.js#L398
14+
var codeOut = '/+dub.sdl:\ndependency "mir-algorithm" version="~>0.6.14"\n+/\n';
15+
16+
// dynamically wrap into main if needed
17+
if (code.indexOf("void main") >= 0) {
18+
codeOut += "import " + currentPackage + "; ";
19+
codeOut += code;
20+
}
21+
else {
22+
codeOut += "void main()\n{\n";
23+
codeOut += " import " + currentPackage + ";\n";
24+
// writing to the stdout is probably often used
25+
codeOut += " import std.stdio: write, writeln, writef, writefln;\n ";
26+
codeOut += code.split("\n").join("\n ");
27+
codeOut += "\n}";
28+
}
29+
return codeOut;
30+
}
31+
32+
$(document).ready(function()
33+
{
34+
if ($('body')[0].id == "Home")
35+
return;
36+
37+
// only for std at the moment
38+
if (!$('body').hasClass("std"))
39+
return;
40+
41+
// first selector is for ddoc - second for ddox
42+
var codeBlocks = $('pre[class~=d_code]').add('pre[class~=code]');
43+
codeBlocks.each(function(index)
44+
{
45+
var currentExample = $(this);
46+
var orig = currentExample.html();
47+
48+
// check whether it is from a ddoced unittest
49+
// 1) check is for ddoc, 2) for ddox
50+
// manual created tests most likely can't be run without modifications
51+
if (!($(this).parent().parent().prev().hasClass("dlang_runnable") ||
52+
$(this).prev().children(":last").hasClass("dlang_runnable")))
53+
return;
54+
55+
currentExample.replaceWith(
56+
'<div class="unittest_examples">'
57+
+ '<div class="d_code">'
58+
+ '<pre class="d_code">'+orig+'</pre>'
59+
+ '</div>'
60+
+ '<div class="d_run_code" style="display: block">'
61+
+ '<textarea class="d_code" style="display: none;"></textarea>'
62+
+ '</div>'
63+
+ '<div class="d_example_buttons">'
64+
+ '<div class="editButton"><i class="fa fa-edit" aria-hidden="true"></i> Edit</div>'
65+
+ '<div class="runButton"><i class="fa fa-play" aria-hidden="true"></i> Run</div>'
66+
+ '<div class="resetButton" style="display:none"><i class="fa fa-undo " aria-hidden="true"></i> Reset</div>'
67+
+ '<div class="openInEditorButton" title="Open in an external editor"><i class="fa fa-external-link" aria-hidden="true"></i></div>'
68+
+ '</div>'
69+
+ '<div class="d_code_output"><span class="d_code_title">Application output</span><br><pre class="d_code_output" readonly>Running...</pre>'
70+
+ '</div>'
71+
);
72+
});
73+
74+
$('textarea[class=d_code]').each(function(index) {
75+
var parent = $(this).parent();
76+
var btnParent = parent.parent().children(".d_example_buttons");
77+
var outputDiv = parent.parent().children(".d_code_output");
78+
var editor = setupTextarea(this, {
79+
parent: btnParent,
80+
outputDiv: outputDiv,
81+
stdin: false,
82+
args: false,
83+
transformOutput: wrapIntoMain,
84+
defaultOutput: "All tests passed",
85+
keepCode: true,
86+
outputHeight: "auto",
87+
backend: "tour"
88+
});
89+
});
90+
});

0 commit comments

Comments
 (0)