From d1fbda322bfe99adaff11f8778a6e0b50ccfe269 Mon Sep 17 00:00:00 2001 From: Sebastian Wilzbach Date: Thu, 15 Jun 2017 13:39:27 +0200 Subject: [PATCH 1/2] Allow runnable examples anywhere --- dlang.org.ddoc | 2 ++ index.dd | 6 ++++-- js/run.js | 10 ++++------ spec/module.dd | 12 ++++++++++++ 4 files changed, 22 insertions(+), 8 deletions(-) diff --git a/dlang.org.ddoc b/dlang.org.ddoc index 7f0782a572..a137aae38d 100644 --- a/dlang.org.ddoc +++ b/dlang.org.ddoc @@ -351,6 +351,8 @@ _= RELATIVE_LINK2=$(ALOCAL $1, $+) _= +RUNNABLE_EXAMPLE=$(DIVC runnable-examples, $1) + SAMPLESRC=$(SPANC sample_src, $(AHTTPS github.com/dlang/dmd/blob/master/samples/$0, /dmd/samples/d/$0)) SCINI=$(TC pre, scini notranslate, $0) SCRIPTLOAD= diff --git a/index.dd b/index.dd index 1977dc4fc3..75c5fedd41 100644 --- a/index.dd +++ b/index.dd @@ -29,6 +29,7 @@ $(DIVC intro, $(DIV, $(DIV, ) ) $(DIVID your-code-here-default, +$(RUNNABLE_EXAMPLE ---- // Round floating point numbers import std.algorithm, std.conv, std.functional, @@ -49,6 +50,7 @@ void main() } ---- ) +) $(EXTRA_EXAMPLE ---- // Sort lines @@ -418,8 +420,8 @@ Macros: TAG=<$1>$+ TAG2=<$1 $2>$3 D=$0 - EXAMPLE=$(TAG2 a, id="a$1-control" class="example-control", )$(TAG2 div, id="a$1" class="example-box", $2) - EXTRA_EXAMPLE= + EXAMPLE=$(TAG2 a, id="a$1-control" class="example-control", )$(TAG2 div, id="a$1" class="example-box", $(RUNNABLE_EXAMPLE $2)) + EXTRA_EXAMPLE= LAYOUT_PREFIX= LAYOUT_SUFFIX= $(SCRIPTLOAD $(ROOT_DIR)js/platform-downloads.js, data-latest="$(LATEST)") diff --git a/js/run.js b/js/run.js index 63a82ed5d4..e3dc0bcdb5 100644 --- a/js/run.js +++ b/js/run.js @@ -335,17 +335,15 @@ $(document).ready(function() var currentPage = $(location).attr('pathname'); - if ($('body')[0].id != "Home") - return; - - $('pre[class~=d_code]').each(function(index) + $('.runnable-examples').each(function(index) { - var stripedText = $(this).text().replace(/\s/gm,''); + var el = $(this).children().first(); + var stripedText = el.text().replace(/\s/gm,''); var md5sum = MD5(stripedText); var stdin = ""; var args = ""; - var currentExample = $(this); + var currentExample = el; var orig = currentExample.html(); if (typeof mainPage !== 'undefined' && md5sum in mainPage) diff --git a/spec/module.dd b/spec/module.dd index 3efab02bc1..67d301cd1b 100644 --- a/spec/module.dd +++ b/spec/module.dd @@ -224,6 +224,7 @@ $(H3 $(LNAME2 name_lookup, Symbol Name Lookup)) $(P The simplest form of importing is to just list the modules being imported:) +$(RUNNABLE_EXAMPLE --------- module myapp.main; @@ -239,6 +240,7 @@ class Foo : BaseClass } } --------- +) $(P When a symbol name is used unqualified, a two-phase lookup will happen. First, the module scope will be searched, starting from the innermost @@ -383,6 +385,7 @@ $(H3 $(LNAME2 renamed_imports, Renamed Imports)) all references to the module's symbols must be qualified with:) +$(RUNNABLE_EXAMPLE --- import io = std.stdio; @@ -393,6 +396,7 @@ void main() writeln("hello!"); // error, writeln is undefined } --- +) $(P Renamed imports are handy when dealing with very long import names.) @@ -402,6 +406,7 @@ $(H3 $(LNAME2 selective_imports, Selective Imports)) $(P Specific symbols can be exclusively imported from a module and bound into the current namespace:) +$(RUNNABLE_EXAMPLE --- import std.stdio : writeln, foo = write; @@ -414,6 +419,7 @@ void main() fwritefln(stdout, "abc"); // error, fwritefln undefined } --- +) $(P $(D static) cannot be used with selective imports.) @@ -421,6 +427,7 @@ $(H3 $(LNAME2 renamed_selective_imports, Renamed and Selective Imports)) $(P When renaming and selective importing are combined:) +$(RUNNABLE_EXAMPLE ------------ import io = std.stdio : foo = writeln; @@ -437,12 +444,14 @@ void main() // foo is not a member of io } -------------- +) $(H3 $(LNAME2 scoped_imports, Scoped Imports)) $(P Import declarations may be used at any scope. For example:) +$(RUNNABLE_EXAMPLE -------------- void main() { @@ -450,6 +459,7 @@ void main() writeln("bar"); } -------------- +) $(P The imports are looked up to satisfy any unresolved symbols at that scope. Imported symbols may hide symbols from outer scopes.) @@ -461,6 +471,7 @@ void main() at function scope cannot be forward referenced. ) +$(RUNNABLE_EXAMPLE -------------- void main() { @@ -477,6 +488,7 @@ void main() std.stdio.writeln("bar"); // error, std is undefined } -------------- +) From 44cbc937104d2c1529bc020f5856b8ab47366453 Mon Sep 17 00:00:00 2001 From: Sebastian Wilzbach Date: Thu, 15 Jun 2017 15:52:54 +0200 Subject: [PATCH 2/2] Remove the MD5-hashing approach to save STDIN and ARGS --- css/style.css | 6 + dlang.org.ddoc | 2 + index.dd | 16 ++- js/run-main-website.js | 22 ---- js/run.js | 254 +++-------------------------------------- posix.mak | 2 +- 6 files changed, 43 insertions(+), 259 deletions(-) delete mode 100644 js/run-main-website.js diff --git a/css/style.css b/css/style.css index 8ca6bcdcd3..da7b53632d 100644 --- a/css/style.css +++ b/css/style.css @@ -1723,6 +1723,12 @@ input.resetButton{display: none} .d_example_buttons { text-align: left; } +.runnable-examples-stdin { + display: none; +} +.runnable-examples-args { + display: none; +} /* Runnable-examples css -end */ .page-contents diff --git a/dlang.org.ddoc b/dlang.org.ddoc index a137aae38d..c918c167fc 100644 --- a/dlang.org.ddoc +++ b/dlang.org.ddoc @@ -352,6 +352,8 @@ RELATIVE_LINK2=$(ALOCAL $1, $+) _= RUNNABLE_EXAMPLE=$(DIVC runnable-examples, $1) +RUNNABLE_EXAMPLE_STDIN=$0 +RUNNABLE_EXAMPLE_ARGS=$0 SAMPLESRC=$(SPANC sample_src, $(AHTTPS github.com/dlang/dmd/blob/master/samples/$0, /dmd/samples/d/$0)) SCINI=$(TC pre, scini notranslate, $0) diff --git a/index.dd b/index.dd index 75c5fedd41..4e8ab3e473 100644 --- a/index.dd +++ b/index.dd @@ -30,6 +30,7 @@ $(DIVC intro, $(DIV, $(DIV, ) $(DIVID your-code-here-default, $(RUNNABLE_EXAMPLE +$(RUNNABLE_EXAMPLE_STDIN 2.4 plus 2.4 equals 5 for sufficiently large values of 2.) ---- // Round floating point numbers import std.algorithm, std.conv, std.functional, @@ -52,6 +53,15 @@ void main() ) ) $(EXTRA_EXAMPLE +$(RUNNABLE_EXAMPLE_STDIN +Mercury +Venus +Earth +Mars +Jupiter +Saturn +Uranus +Neptune) ---- // Sort lines import std.stdio, std.array, std.algorithm; @@ -227,6 +237,11 @@ void main() $(P Built-in linear and associative arrays, slices, and ranges make daily programming simple and pleasant for tasks, both small and large. $(EXAMPLE 3, +$(RUNNABLE_EXAMPLE_STDIN +The D programming language +Modern convenience. +Modeling power. +Native efficiency.) ---- #!/usr/bin/env rdmd import std.range, std.stdio; @@ -425,7 +440,6 @@ Macros: LAYOUT_PREFIX= LAYOUT_SUFFIX= $(SCRIPTLOAD $(ROOT_DIR)js/platform-downloads.js, data-latest="$(LATEST)") - $(SCRIPTLOAD $(ROOT_DIR)js/run-main-website.js) $(SCRIPTLOAD //arsdnet.net/this-week-in-d/twid-latest.js) LAYOUT_TITLE= TOUR=$(DIVC item, $(SECTION4 $(LINK2 $1, $(TC i, fa fa-$2 big-icon)$3), $4)) diff --git a/js/run-main-website.js b/js/run-main-website.js deleted file mode 100644 index 76a32787de..0000000000 --- a/js/run-main-website.js +++ /dev/null @@ -1,22 +0,0 @@ -/** -Runnable examples functionality - -Copyright: 2012 by Digital Mars - -License: http://boost.org/LICENSE_1_0.txt, Boost License 1.0 - -Authors: Damian Ziemba -*/ -var mainPage = new Array(); - -// Top rotation - sort lines -mainPage["f34e2dd1777620887b1899e48463d82b"] = ["Mercury\nVenus\nEarth\nMars\nJupiter\nSaturn\nUranus\nNeptune"]; - -// Top rotation - round floating point numbers -mainPage["c308dda4f09440aa35d857241991f55e"] = ["2.4 plus 2.4 equals 5 for sufficiently large values of 2."]; - -// Automatic memory management makes for safe... -mainPage["54f2095e83f8ef851a711db0a29b0e26"] = null; - -// Built-in linear and associative arrays, slices, and ranges make... -mainPage["6c843684c0231b847f933e6d7b5a697a"] = ["The D programming language.\nModern convenience.\nModeling power.\nNative efficiency."]; diff --git a/js/run.js b/js/run.js index e3dc0bcdb5..58845d385c 100644 --- a/js/run.js +++ b/js/run.js @@ -37,222 +37,10 @@ void main(string[] args) { [end here] ------ -Go to http://dpaste.dzfl.pl/md5sum, paste your example to Source box and click "Compute". -Copy generated md5sum. Open run-main-website.js file and add following - -mainPage["yourMd5Sum"] = ["standard input is has 0 position", "standard args has 1 position"]; - -Save, reload website and see if standard input and/or standard arguments are displayed in your example form. - TL;DR All examples are replaced with custom form by default. You need to do additional work only if you wan't your example to have deafault standard input or default standard arguments. - -*/ - -/** -Taken from http://www.webtoolkit.info/javascript-md5.html */ -var MD5 = function (string) { - - function RotateLeft(lValue, iShiftBits) { - return (lValue<>>(32-iShiftBits)); - } - - function AddUnsigned(lX,lY) { - var lX4,lY4,lX8,lY8,lResult; - lX8 = (lX & 0x80000000); - lY8 = (lY & 0x80000000); - lX4 = (lX & 0x40000000); - lY4 = (lY & 0x40000000); - lResult = (lX & 0x3FFFFFFF)+(lY & 0x3FFFFFFF); - if (lX4 & lY4) { - return (lResult ^ 0x80000000 ^ lX8 ^ lY8); - } - if (lX4 | lY4) { - if (lResult & 0x40000000) { - return (lResult ^ 0xC0000000 ^ lX8 ^ lY8); - } else { - return (lResult ^ 0x40000000 ^ lX8 ^ lY8); - } - } else { - return (lResult ^ lX8 ^ lY8); - } - } - - function F(x,y,z) { return (x & y) | ((~x) & z); } - function G(x,y,z) { return (x & z) | (y & (~z)); } - function H(x,y,z) { return (x ^ y ^ z); } - function I(x,y,z) { return (y ^ (x | (~z))); } - - function FF(a,b,c,d,x,s,ac) { - a = AddUnsigned(a, AddUnsigned(AddUnsigned(F(b, c, d), x), ac)); - return AddUnsigned(RotateLeft(a, s), b); - }; - - function GG(a,b,c,d,x,s,ac) { - a = AddUnsigned(a, AddUnsigned(AddUnsigned(G(b, c, d), x), ac)); - return AddUnsigned(RotateLeft(a, s), b); - }; - - function HH(a,b,c,d,x,s,ac) { - a = AddUnsigned(a, AddUnsigned(AddUnsigned(H(b, c, d), x), ac)); - return AddUnsigned(RotateLeft(a, s), b); - }; - - function II(a,b,c,d,x,s,ac) { - a = AddUnsigned(a, AddUnsigned(AddUnsigned(I(b, c, d), x), ac)); - return AddUnsigned(RotateLeft(a, s), b); - }; - - function ConvertToWordArray(string) { - var lWordCount; - var lMessageLength = string.length; - var lNumberOfWords_temp1=lMessageLength + 8; - var lNumberOfWords_temp2=(lNumberOfWords_temp1-(lNumberOfWords_temp1 % 64))/64; - var lNumberOfWords = (lNumberOfWords_temp2+1)*16; - var lWordArray=Array(lNumberOfWords-1); - var lBytePosition = 0; - var lByteCount = 0; - while ( lByteCount < lMessageLength ) { - lWordCount = (lByteCount-(lByteCount % 4))/4; - lBytePosition = (lByteCount % 4)*8; - lWordArray[lWordCount] = (lWordArray[lWordCount] | (string.charCodeAt(lByteCount)<>>29; - return lWordArray; - }; - - function WordToHex(lValue) { - var WordToHexValue="",WordToHexValue_temp="",lByte,lCount; - for (lCount = 0;lCount<=3;lCount++) { - lByte = (lValue>>>(lCount*8)) & 255; - WordToHexValue_temp = "0" + lByte.toString(16); - WordToHexValue = WordToHexValue + WordToHexValue_temp.substr(WordToHexValue_temp.length-2,2); - } - return WordToHexValue; - }; - - function Utf8Encode(string) { - string = string.replace(/\r\n/g,"\n"); - var utftext = ""; - - for (var n = 0; n < string.length; n++) { - - var c = string.charCodeAt(n); - - if (c < 128) { - utftext += String.fromCharCode(c); - } - else if((c > 127) && (c < 2048)) { - utftext += String.fromCharCode((c >> 6) | 192); - utftext += String.fromCharCode((c & 63) | 128); - } - else { - utftext += String.fromCharCode((c >> 12) | 224); - utftext += String.fromCharCode(((c >> 6) & 63) | 128); - utftext += String.fromCharCode((c & 63) | 128); - } - - } - - return utftext; - }; - - var x=Array(); - var k,AA,BB,CC,DD,a,b,c,d; - var S11=7, S12=12, S13=17, S14=22; - var S21=5, S22=9 , S23=14, S24=20; - var S31=4, S32=11, S33=16, S34=23; - var S41=6, S42=10, S43=15, S44=21; - - string = Utf8Encode(string); - - x = ConvertToWordArray(string); - - a = 0x67452301; b = 0xEFCDAB89; c = 0x98BADCFE; d = 0x10325476; - - for (k=0;k 0) { - var elements = mainPage[md5sum]; - - if (elements == null) - return; // this example is not runnable online - - if (elements[0] != null) - stdin = elements[0]; - - if (elements[1] != null) - args = elements[1]; + stdin = '
Standard input
' + + '
'; + } + if (args.length > 0) + { + args = '
Command line arguments
' + + '
'; } + var currentExample = el; + var orig = currentExample.html(); + currentExample.replaceWith( '
'+orig+'
' + '
' + '' - + '
Standard input
' - + '
' - + '
Command line arguments
' - + '
' + + stdin + args + '
Application output
Running...
' + '' - + '' - + '' + + (args.length > 0 ? '' : '') + + (stdin.length > 0 ? '' : '') + '' + '
' ); diff --git a/posix.mak b/posix.mak index 1f16c07d63..9d86800353 100644 --- a/posix.mak +++ b/posix.mak @@ -157,7 +157,7 @@ IMAGES=favicon.ico $(ORGS_USING_D) $(addprefix images/, \ JAVASCRIPT=$(addsuffix .js, $(addprefix js/, \ codemirror-compressed dlang ddox listanchors platform-downloads run \ - run_examples run-main-website show_contributors jquery-1.7.2.min)) + run_examples show_contributors jquery-1.7.2.min)) STYLES=$(addsuffix .css, $(addprefix css/, \ style print codemirror ddox))