Skip to content

Commit 6797d9e

Browse files
authored
Merge pull request #1709 from wilzbach/allow-runnable-examples-anywhere
Allow runnable examples anywhere
2 parents 69b9033 + 44cbc93 commit 6797d9e

File tree

7 files changed

+63
-265
lines changed

7 files changed

+63
-265
lines changed

css/style.css

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1723,6 +1723,12 @@ input.resetButton{display: none}
17231723
.d_example_buttons {
17241724
text-align: left;
17251725
}
1726+
.runnable-examples-stdin {
1727+
display: none;
1728+
}
1729+
.runnable-examples-args {
1730+
display: none;
1731+
}
17261732
/* Runnable-examples css -end */
17271733

17281734
.page-contents

dlang.org.ddoc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -351,6 +351,10 @@ _=
351351
RELATIVE_LINK2=$(ALOCAL $1, $+)
352352
_=
353353

354+
RUNNABLE_EXAMPLE=$(DIVC runnable-examples, $1)
355+
RUNNABLE_EXAMPLE_STDIN=<code class="runnable-examples-stdin">$0</code>
356+
RUNNABLE_EXAMPLE_ARGS=<code class="runnable-examples-args">$0</code>
357+
354358
SAMPLESRC=$(SPANC sample_src, $(AHTTPS github.com/dlang/dmd/blob/master/samples/$0, /dmd/samples/d/$0))
355359
SCINI=$(TC pre, scini notranslate, $0)
356360
SCRIPTLOAD=<script type="text/javascript" src="$1"$+></script>

index.dd

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ $(DIVC intro, $(DIV, $(DIV,
2929
)
3030
)
3131
$(DIVID your-code-here-default,
32+
$(RUNNABLE_EXAMPLE
33+
$(RUNNABLE_EXAMPLE_STDIN 2.4 plus 2.4 equals 5 for sufficiently large values of 2.)
3234
----
3335
// Round floating point numbers
3436
import std.algorithm, std.conv, std.functional,
@@ -49,7 +51,17 @@ void main()
4951
}
5052
----
5153
)
54+
)
5255
$(EXTRA_EXAMPLE
56+
$(RUNNABLE_EXAMPLE_STDIN
57+
Mercury
58+
Venus
59+
Earth
60+
Mars
61+
Jupiter
62+
Saturn
63+
Uranus
64+
Neptune)
5365
----
5466
// Sort lines
5567
import std.stdio, std.array, std.algorithm;
@@ -225,6 +237,11 @@ void main()
225237

226238
$(P Built-in linear and associative arrays, slices, and ranges make daily
227239
programming simple and pleasant for tasks, both small and large. $(EXAMPLE 3,
240+
$(RUNNABLE_EXAMPLE_STDIN
241+
The D programming language
242+
Modern convenience.
243+
Modeling power.
244+
Native efficiency.)
228245
----
229246
#!/usr/bin/env rdmd
230247
import std.range, std.stdio;
@@ -418,12 +435,11 @@ Macros:
418435
TAG=<$1>$+</$1>
419436
TAG2=<$1 $2>$3</$1>
420437
D=<span class="d_inlinecode">$0</span>
421-
EXAMPLE=$(TAG2 a, id="a$1-control" class="example-control", )$(TAG2 div, id="a$1" class="example-box", $2)
422-
EXTRA_EXAMPLE=<div class="your-code-here-extra" style="display:none">$0</div>
438+
EXAMPLE=$(TAG2 a, id="a$1-control" class="example-control", )$(TAG2 div, id="a$1" class="example-box", $(RUNNABLE_EXAMPLE $2))
439+
EXTRA_EXAMPLE=<div class="your-code-here-extra" style="display:none">$(RUNNABLE_EXAMPLE $0)</div>
423440
LAYOUT_PREFIX=
424441
LAYOUT_SUFFIX=
425442
$(SCRIPTLOAD $(ROOT_DIR)js/platform-downloads.js, data-latest="$(LATEST)")
426-
$(SCRIPTLOAD $(ROOT_DIR)js/run-main-website.js)
427443
$(SCRIPTLOAD //arsdnet.net/this-week-in-d/twid-latest.js)
428444
LAYOUT_TITLE=
429445
TOUR=$(DIVC item, $(SECTION4 $(LINK2 $1, $(TC i, fa fa-$2 big-icon)$3), $4))

js/run-main-website.js

Lines changed: 0 additions & 22 deletions
This file was deleted.

js/run.js

Lines changed: 21 additions & 239 deletions
Original file line numberDiff line numberDiff line change
@@ -37,222 +37,10 @@ void main(string[] args) {
3737
[end here]
3838
------
3939
40-
Go to http://dpaste.dzfl.pl/md5sum, paste your example to Source box and click "Compute".
41-
Copy generated md5sum. Open run-main-website.js file and add following
42-
43-
mainPage["yourMd5Sum"] = ["standard input is has 0 position", "standard args has 1 position"];
44-
45-
Save, reload website and see if standard input and/or standard arguments are displayed in your example form.
46-
4740
TL;DR
4841
All examples are replaced with custom form by default. You need to do additional work only if you wan't
4942
your example to have deafault standard input or default standard arguments.
50-
51-
*/
52-
53-
/**
54-
Taken from http://www.webtoolkit.info/javascript-md5.html
5543
*/
56-
var MD5 = function (string) {
57-
58-
function RotateLeft(lValue, iShiftBits) {
59-
return (lValue<<iShiftBits) | (lValue>>>(32-iShiftBits));
60-
}
61-
62-
function AddUnsigned(lX,lY) {
63-
var lX4,lY4,lX8,lY8,lResult;
64-
lX8 = (lX & 0x80000000);
65-
lY8 = (lY & 0x80000000);
66-
lX4 = (lX & 0x40000000);
67-
lY4 = (lY & 0x40000000);
68-
lResult = (lX & 0x3FFFFFFF)+(lY & 0x3FFFFFFF);
69-
if (lX4 & lY4) {
70-
return (lResult ^ 0x80000000 ^ lX8 ^ lY8);
71-
}
72-
if (lX4 | lY4) {
73-
if (lResult & 0x40000000) {
74-
return (lResult ^ 0xC0000000 ^ lX8 ^ lY8);
75-
} else {
76-
return (lResult ^ 0x40000000 ^ lX8 ^ lY8);
77-
}
78-
} else {
79-
return (lResult ^ lX8 ^ lY8);
80-
}
81-
}
82-
83-
function F(x,y,z) { return (x & y) | ((~x) & z); }
84-
function G(x,y,z) { return (x & z) | (y & (~z)); }
85-
function H(x,y,z) { return (x ^ y ^ z); }
86-
function I(x,y,z) { return (y ^ (x | (~z))); }
87-
88-
function FF(a,b,c,d,x,s,ac) {
89-
a = AddUnsigned(a, AddUnsigned(AddUnsigned(F(b, c, d), x), ac));
90-
return AddUnsigned(RotateLeft(a, s), b);
91-
};
92-
93-
function GG(a,b,c,d,x,s,ac) {
94-
a = AddUnsigned(a, AddUnsigned(AddUnsigned(G(b, c, d), x), ac));
95-
return AddUnsigned(RotateLeft(a, s), b);
96-
};
97-
98-
function HH(a,b,c,d,x,s,ac) {
99-
a = AddUnsigned(a, AddUnsigned(AddUnsigned(H(b, c, d), x), ac));
100-
return AddUnsigned(RotateLeft(a, s), b);
101-
};
102-
103-
function II(a,b,c,d,x,s,ac) {
104-
a = AddUnsigned(a, AddUnsigned(AddUnsigned(I(b, c, d), x), ac));
105-
return AddUnsigned(RotateLeft(a, s), b);
106-
};
107-
108-
function ConvertToWordArray(string) {
109-
var lWordCount;
110-
var lMessageLength = string.length;
111-
var lNumberOfWords_temp1=lMessageLength + 8;
112-
var lNumberOfWords_temp2=(lNumberOfWords_temp1-(lNumberOfWords_temp1 % 64))/64;
113-
var lNumberOfWords = (lNumberOfWords_temp2+1)*16;
114-
var lWordArray=Array(lNumberOfWords-1);
115-
var lBytePosition = 0;
116-
var lByteCount = 0;
117-
while ( lByteCount < lMessageLength ) {
118-
lWordCount = (lByteCount-(lByteCount % 4))/4;
119-
lBytePosition = (lByteCount % 4)*8;
120-
lWordArray[lWordCount] = (lWordArray[lWordCount] | (string.charCodeAt(lByteCount)<<lBytePosition));
121-
lByteCount++;
122-
}
123-
lWordCount = (lByteCount-(lByteCount % 4))/4;
124-
lBytePosition = (lByteCount % 4)*8;
125-
lWordArray[lWordCount] = lWordArray[lWordCount] | (0x80<<lBytePosition);
126-
lWordArray[lNumberOfWords-2] = lMessageLength<<3;
127-
lWordArray[lNumberOfWords-1] = lMessageLength>>>29;
128-
return lWordArray;
129-
};
130-
131-
function WordToHex(lValue) {
132-
var WordToHexValue="",WordToHexValue_temp="",lByte,lCount;
133-
for (lCount = 0;lCount<=3;lCount++) {
134-
lByte = (lValue>>>(lCount*8)) & 255;
135-
WordToHexValue_temp = "0" + lByte.toString(16);
136-
WordToHexValue = WordToHexValue + WordToHexValue_temp.substr(WordToHexValue_temp.length-2,2);
137-
}
138-
return WordToHexValue;
139-
};
140-
141-
function Utf8Encode(string) {
142-
string = string.replace(/\r\n/g,"\n");
143-
var utftext = "";
144-
145-
for (var n = 0; n < string.length; n++) {
146-
147-
var c = string.charCodeAt(n);
148-
149-
if (c < 128) {
150-
utftext += String.fromCharCode(c);
151-
}
152-
else if((c > 127) && (c < 2048)) {
153-
utftext += String.fromCharCode((c >> 6) | 192);
154-
utftext += String.fromCharCode((c & 63) | 128);
155-
}
156-
else {
157-
utftext += String.fromCharCode((c >> 12) | 224);
158-
utftext += String.fromCharCode(((c >> 6) & 63) | 128);
159-
utftext += String.fromCharCode((c & 63) | 128);
160-
}
161-
162-
}
163-
164-
return utftext;
165-
};
166-
167-
var x=Array();
168-
var k,AA,BB,CC,DD,a,b,c,d;
169-
var S11=7, S12=12, S13=17, S14=22;
170-
var S21=5, S22=9 , S23=14, S24=20;
171-
var S31=4, S32=11, S33=16, S34=23;
172-
var S41=6, S42=10, S43=15, S44=21;
173-
174-
string = Utf8Encode(string);
175-
176-
x = ConvertToWordArray(string);
177-
178-
a = 0x67452301; b = 0xEFCDAB89; c = 0x98BADCFE; d = 0x10325476;
179-
180-
for (k=0;k<x.length;k+=16) {
181-
AA=a; BB=b; CC=c; DD=d;
182-
a=FF(a,b,c,d,x[k+0], S11,0xD76AA478);
183-
d=FF(d,a,b,c,x[k+1], S12,0xE8C7B756);
184-
c=FF(c,d,a,b,x[k+2], S13,0x242070DB);
185-
b=FF(b,c,d,a,x[k+3], S14,0xC1BDCEEE);
186-
a=FF(a,b,c,d,x[k+4], S11,0xF57C0FAF);
187-
d=FF(d,a,b,c,x[k+5], S12,0x4787C62A);
188-
c=FF(c,d,a,b,x[k+6], S13,0xA8304613);
189-
b=FF(b,c,d,a,x[k+7], S14,0xFD469501);
190-
a=FF(a,b,c,d,x[k+8], S11,0x698098D8);
191-
d=FF(d,a,b,c,x[k+9], S12,0x8B44F7AF);
192-
c=FF(c,d,a,b,x[k+10],S13,0xFFFF5BB1);
193-
b=FF(b,c,d,a,x[k+11],S14,0x895CD7BE);
194-
a=FF(a,b,c,d,x[k+12],S11,0x6B901122);
195-
d=FF(d,a,b,c,x[k+13],S12,0xFD987193);
196-
c=FF(c,d,a,b,x[k+14],S13,0xA679438E);
197-
b=FF(b,c,d,a,x[k+15],S14,0x49B40821);
198-
a=GG(a,b,c,d,x[k+1], S21,0xF61E2562);
199-
d=GG(d,a,b,c,x[k+6], S22,0xC040B340);
200-
c=GG(c,d,a,b,x[k+11],S23,0x265E5A51);
201-
b=GG(b,c,d,a,x[k+0], S24,0xE9B6C7AA);
202-
a=GG(a,b,c,d,x[k+5], S21,0xD62F105D);
203-
d=GG(d,a,b,c,x[k+10],S22,0x2441453);
204-
c=GG(c,d,a,b,x[k+15],S23,0xD8A1E681);
205-
b=GG(b,c,d,a,x[k+4], S24,0xE7D3FBC8);
206-
a=GG(a,b,c,d,x[k+9], S21,0x21E1CDE6);
207-
d=GG(d,a,b,c,x[k+14],S22,0xC33707D6);
208-
c=GG(c,d,a,b,x[k+3], S23,0xF4D50D87);
209-
b=GG(b,c,d,a,x[k+8], S24,0x455A14ED);
210-
a=GG(a,b,c,d,x[k+13],S21,0xA9E3E905);
211-
d=GG(d,a,b,c,x[k+2], S22,0xFCEFA3F8);
212-
c=GG(c,d,a,b,x[k+7], S23,0x676F02D9);
213-
b=GG(b,c,d,a,x[k+12],S24,0x8D2A4C8A);
214-
a=HH(a,b,c,d,x[k+5], S31,0xFFFA3942);
215-
d=HH(d,a,b,c,x[k+8], S32,0x8771F681);
216-
c=HH(c,d,a,b,x[k+11],S33,0x6D9D6122);
217-
b=HH(b,c,d,a,x[k+14],S34,0xFDE5380C);
218-
a=HH(a,b,c,d,x[k+1], S31,0xA4BEEA44);
219-
d=HH(d,a,b,c,x[k+4], S32,0x4BDECFA9);
220-
c=HH(c,d,a,b,x[k+7], S33,0xF6BB4B60);
221-
b=HH(b,c,d,a,x[k+10],S34,0xBEBFBC70);
222-
a=HH(a,b,c,d,x[k+13],S31,0x289B7EC6);
223-
d=HH(d,a,b,c,x[k+0], S32,0xEAA127FA);
224-
c=HH(c,d,a,b,x[k+3], S33,0xD4EF3085);
225-
b=HH(b,c,d,a,x[k+6], S34,0x4881D05);
226-
a=HH(a,b,c,d,x[k+9], S31,0xD9D4D039);
227-
d=HH(d,a,b,c,x[k+12],S32,0xE6DB99E5);
228-
c=HH(c,d,a,b,x[k+15],S33,0x1FA27CF8);
229-
b=HH(b,c,d,a,x[k+2], S34,0xC4AC5665);
230-
a=II(a,b,c,d,x[k+0], S41,0xF4292244);
231-
d=II(d,a,b,c,x[k+7], S42,0x432AFF97);
232-
c=II(c,d,a,b,x[k+14],S43,0xAB9423A7);
233-
b=II(b,c,d,a,x[k+5], S44,0xFC93A039);
234-
a=II(a,b,c,d,x[k+12],S41,0x655B59C3);
235-
d=II(d,a,b,c,x[k+3], S42,0x8F0CCC92);
236-
c=II(c,d,a,b,x[k+10],S43,0xFFEFF47D);
237-
b=II(b,c,d,a,x[k+1], S44,0x85845DD1);
238-
a=II(a,b,c,d,x[k+8], S41,0x6FA87E4F);
239-
d=II(d,a,b,c,x[k+15],S42,0xFE2CE6E0);
240-
c=II(c,d,a,b,x[k+6], S43,0xA3014314);
241-
b=II(b,c,d,a,x[k+13],S44,0x4E0811A1);
242-
a=II(a,b,c,d,x[k+4], S41,0xF7537E82);
243-
d=II(d,a,b,c,x[k+11],S42,0xBD3AF235);
244-
c=II(c,d,a,b,x[k+2], S43,0x2AD7D2BB);
245-
b=II(b,c,d,a,x[k+9], S44,0xEB86D391);
246-
a=AddUnsigned(a,AA);
247-
b=AddUnsigned(b,BB);
248-
c=AddUnsigned(c,CC);
249-
d=AddUnsigned(d,DD);
250-
}
251-
252-
var temp = WordToHex(a)+WordToHex(b)+WordToHex(c)+WordToHex(d);
253-
254-
return temp.toLowerCase();
255-
}
25644

25745
var nl2br = function()
25846
{
@@ -335,45 +123,39 @@ $(document).ready(function()
335123

336124
var currentPage = $(location).attr('pathname');
337125

338-
if ($('body')[0].id != "Home")
339-
return;
340-
341-
$('pre[class~=d_code]').each(function(index)
126+
$('.runnable-examples').each(function(index)
342127
{
343-
var stripedText = $(this).text().replace(/\s/gm,'');
344-
var md5sum = MD5(stripedText);
128+
var root = $(this);
129+
var el = root.children("pre");
130+
var stripedText = el.text().replace(/\s/gm,'');
345131

346-
var stdin = "";
347-
var args = "";
348-
var currentExample = $(this);
349-
var orig = currentExample.html();
132+
var stdin = root.children(".runnable-examples-stdin").text();
133+
var args = root.children(".runnable-examples-args").text();
350134

351-
if (typeof mainPage !== 'undefined' && md5sum in mainPage)
135+
// only show stdin or args if they are set
136+
if (stdin.length > 0)
352137
{
353-
var elements = mainPage[md5sum];
354-
355-
if (elements == null)
356-
return; // this example is not runnable online
357-
358-
if (elements[0] != null)
359-
stdin = elements[0];
360-
361-
if (elements[1] != null)
362-
args = elements[1];
138+
stdin = '<div class="d_code_stdin"><span class="d_code_title">Standard input</span><br>'
139+
+ '<textarea class="d_code_stdin">'+stdin+'</textarea></div>';
363140
}
141+
if (args.length > 0)
142+
{
143+
args = '<div class="d_code_args"><span class="d_code_title">Command line arguments</span><br>'
144+
+ '<textarea class="d_code_args">'+args+'</textarea></div>';
145+
}
146+
147+
var currentExample = el;
148+
var orig = currentExample.html();
364149

365150
currentExample.replaceWith(
366151
'<div class="d_code"><pre class="d_code">'+orig+'</pre></div>'
367152
+ '<div class="d_run_code">'
368153
+ '<textarea class="d_code" style="display: none;"></textarea>'
369-
+ '<div class="d_code_stdin"><span class="d_code_title">Standard input</span><br>'
370-
+ '<textarea class="d_code_stdin">'+stdin+'</textarea></div>'
371-
+ '<div class="d_code_args"><span class="d_code_title">Command line arguments</span><br>'
372-
+ '<textarea class="d_code_args">'+args+'</textarea></div>'
154+
+ stdin + args
373155
+ '<div class="d_code_output"><span class="d_code_title">Application output</span><br><pre class="d_code_output" readonly>Running...</pre></div>'
374156
+ '<input type="button" class="editButton" value="Edit">'
375-
+ '<input type="button" class="argsButton" value="Args">'
376-
+ '<input type="button" class="inputButton" value="Input">'
157+
+ (args.length > 0 ? '<input type="button" class="argsButton" value="Args">' : '')
158+
+ (stdin.length > 0 ? '<input type="button" class="inputButton" value="Input">' : '')
377159
+ '<input type="button" class="runButton" value="Run">'
378160
+ '<input type="button" class="resetButton" value="Reset"></div>'
379161
);

posix.mak

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ IMAGES=favicon.ico $(ORGS_USING_D) $(addprefix images/, \
157157

158158
JAVASCRIPT=$(addsuffix .js, $(addprefix js/, \
159159
codemirror-compressed dlang ddox listanchors platform-downloads run \
160-
run_examples run-main-website show_contributors jquery-1.7.2.min))
160+
run_examples show_contributors jquery-1.7.2.min))
161161

162162
STYLES=$(addsuffix .css, $(addprefix css/, \
163163
style print codemirror ddox))

0 commit comments

Comments
 (0)