diff --git a/css/style.css b/css/style.css index 5cd1a26904..183b28c034 100644 --- a/css/style.css +++ b/css/style.css @@ -2113,3 +2113,8 @@ dt.d_decl:hover .decl_anchor { text-decoration: none; color: #333; } + +/* Style for the example run buttons on the Phobos library documentation */ +.d_example_buttons { + text-align: right; +} diff --git a/dlang.org.ddoc b/dlang.org.ddoc index 1d54a73e5c..54d14203d7 100644 --- a/dlang.org.ddoc +++ b/dlang.org.ddoc @@ -93,6 +93,7 @@ $(DIVC container, ) ) $(COMMON_SCRIPTS) +$(EXTRA_FOOTERS) $(LAYOUT_SUFFIX) @@ -108,7 +109,7 @@ DDOC_BUGS = $(DDOCKEYVAL Bugs, $0) DDOC_COPYRIGHT = $(DDOCKEYVAL Copyright, $0) DDOC_DATE = $(DDOCKEYVAL Date, $0) DDOC_DEPRECATED = $(DDOCKEYVAL Deprecated, $0) -DDOC_EXAMPLES = $(DDOCKEYVAL Examples, $0) +DDOC_EXAMPLES = $(DDOCKEYVAL Examples, $0) DDOC_HISTORY = $(DDOCKEYVAL History, $0) DDOC_LICENSE = $(DDOCKEYVAL License, $0) DDOC_RETURNS = $(DDOCKEYVAL Returns, $0) @@ -147,6 +148,7 @@ $(TR 32 bit64 bit) _= EXTRA_HEADERS= EXTRA_JS= +EXTRA_FOOTERS= _= FAVICON=$(STATIC favicon.ico) diff --git a/js/run.js b/js/run.js index d7bb3f9a47..9a56cb9ebe 100644 --- a/js/run.js +++ b/js/run.js @@ -45,7 +45,7 @@ mainPage["yourMd5Sum"] = ["standard input is has 0 position", "standard args has 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 +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. */ @@ -54,11 +54,11 @@ 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); @@ -79,32 +79,32 @@ var MD5 = function (string) { 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; @@ -127,7 +127,7 @@ var MD5 = function (string) { lWordArray[lNumberOfWords-1] = lMessageLength>>>29; return lWordArray; }; - + function WordToHex(lValue) { var WordToHexValue="",WordToHexValue_temp="",lByte,lCount; for (lCount = 0;lCount<=3;lCount++) { @@ -137,15 +137,15 @@ var MD5 = function (string) { } 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); } @@ -158,25 +158,25 @@ var MD5 = function (string) { 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"); } @@ -282,7 +282,7 @@ function parseOutput(data, o, oTitle) { if (typeof data.compilation == "undefined") { - o.text("Temporarily unavaible"); + o.text("Temporarily unavailable"); return; } @@ -296,15 +296,16 @@ function parseOutput(data, o, oTitle) var rstatus = parseInt(safeVar(data, "runtime.status")); var cerr = safeVar(data, "compilation.err"); var rerr = safeVar(data, "runtime.err"); + var defaultOutput = data.defaultOutput || '-- No output --'; if (cstatus != 0) { oTitle.text("Compilation output ("+cstatus+": "+cerr+")"); if ($.browser.msie) - o.html(cout.nl2br()); + o.html(nl2br(cout)); else o.text(cout); - + return; } else @@ -312,10 +313,10 @@ function parseOutput(data, o, oTitle) oTitle.text("Application output");// (compile "+ctime+"ms, run "+rtime+"ms)"); if ( cout != "") output = 'Compilation output: \n' + cout + "\n"; - - output += (stdout == "" && stderr == "" ? '-- No output --' : stdout); - if (stderr != "") + output += (stdout == "" && stderr == "" ? defaultOutput : stdout); + + if (stderr != "") output += stderr; if (rstatus != 0) @@ -323,17 +324,17 @@ function parseOutput(data, o, oTitle) } if ($.browser.msie) - o.html(output.nl2br()); + o.html(nl2br(cout)); else o.text(output); } -$(document).ready(function() +$(document).ready(function() { setUpExamples(); var currentPage = $(location).attr('pathname'); - + if ($('body')[0].id != "Home") return; @@ -358,7 +359,7 @@ $(document).ready(function() stdin = elements[0]; if (elements[1] != null) - args = elements[1]; + args = elements[1]; } currentExample.replaceWith( @@ -377,78 +378,111 @@ $(document).ready(function() + '' ); }); - + $('textarea[class=d_code]').each(function(index) { - var thisObj = $(this); + var parent = $(this).parent(); + var outputDiv = parent.children("div.d_code_output"); + setupTextarea(this, {parent: parent, outputDiv: outputDiv, + stdin: true, args: true}); + }); +}); - var parent = thisObj.parent(); - parent.css("display", "block"); - var orgSrc = parent.parent().children("div.d_code").children("pre.d_code"); +function setupTextarea(el, opts) +{ + opts = opts || {}; + // set default opts + opts = jQuery.extend({}, { + stdin: false, + args: false, + transformOutput: function(out) { return out } + }, opts); + + if (!!opts.parent) + var parent = opts.parent; + else + console.error("parent node node not found"); + + if (!!opts.outputDiv) + var outputDiv = opts.outputDiv; + else + console.error("outputDiv node not found"); + + var thisObj = $(el); + parent.css("display", "block"); + var orgSrc = parent.parent().children("div.d_code").children("pre.d_code"); - var prepareForMain = function() + var prepareForMain = function() + { + var src = $.browser.msie && $.browser.version < 9.0 ? orgSrc[0].innerText : orgSrc.text(); + var arr = src.split("\n"); + var str = ""; + for ( i = 0; i < arr.length; i++) { - var src = $.browser.msie && $.browser.version < 9.0 ? orgSrc[0].innerText : orgSrc.text(); - var arr = src.split("\n"); - var str = ""; - for ( i = 0; i < arr.length; i++) - { - str += arr[i]+"\n"; - } - if ($.browser.msie && $.browser.version < 9.0) - str = str.substr(0, str.length - 1); - else - str = str.substr(0, str.length - 2); - - return str; - }; - - var editor = CodeMirror.fromTextArea(thisObj[0], { - lineNumbers: true, - tabSize: 4, - indentUnit: 4, - indentWithTabs: true, - mode: "text/x-d", - lineWrapping: true, - theme: "eclipse", - readOnly: false, - matchBrackets: true - }); + str += arr[i]+"\n"; + } + if ($.browser.msie && $.browser.version < 9.0) + str = str.substr(0, str.length - 1); + else + str = str.substr(0, str.length - 2); - editor.setValue(prepareForMain()); + return str; + }; - var height = function(diff) { - var par = code != null ? code : parent.parent().children("div.d_code"); - return (parseInt(par.css('height')) - diff) + 'px'; - }; + var editor = CodeMirror.fromTextArea(thisObj[0], { + lineNumbers: true, + tabSize: 4, + indentUnit: 4, + indentWithTabs: true, + mode: "text/x-d", + lineWrapping: true, + theme: "eclipse", + readOnly: false, + matchBrackets: true + }); - var runBtn = parent.children("input.runButton"); - var editBtn = parent.children("input.editButton"); - var inputBtn = parent.children("input.inputButton"); - var resetBtn = parent.children("input.resetButton"); - var argsBtn = parent.children("input.argsButton"); - var stdinDiv = parent.children("div.d_code_stdin"); - var argsDiv = parent.children("div.d_code_args"); - var outputDiv = parent.children("div.d_code_output"); + editor.setValue(prepareForMain()); - var code = $(editor.getWrapperElement()); - code.css('display', 'none'); + var height = function(diff) { + var par = code != null ? code : parent.parent().children("div.d_code"); + return (parseInt(par.css('height')) - diff) + 'px'; + }; - var output = outputDiv.children("textarea.d_code_output"); - var outputTitle = outputDiv.children("span.d_code_title"); - var stdin = stdinDiv.children("textarea.d_code_stdin"); + var runBtn = parent.children("input.runButton"); + var editBtn = parent.children("input.editButton"); + var resetBtn = parent.children("input.resetButton"); + + var code = $(editor.getWrapperElement()); + code.css('display', 'none'); + + var output = outputDiv.children("textarea.d_code_output"); + var outputTitle = outputDiv.children("span.d_code_title"); + if (opts.args) { + var argsBtn = parent.children("input.argsButton"); + var argsDiv = parent.children("div.d_code_args"); var args = argsDiv.children("textarea.d_code_args"); var orgArgs = args.val(); + } + if (opts.stdin) { + var inputBtn = parent.children("input.inputButton"); + var stdinDiv = parent.children("div.d_code_stdin"); + var stdin = stdinDiv.children("textarea.d_code_stdin"); var orgStdin = stdin.val(); + } - var hideAllWindows = function() - { + var hideAllWindows = function() + { + if (opts.stdin) { stdinDiv.css('display', 'none'); + } + if (opts.args) { argsDiv.css('display', 'none'); - outputDiv.css('display', 'none'); - parent.parent().children("div.d_code").css('display', 'none'); - code.css('display', 'none'); - }; + } + outputDiv.css('display', 'none'); + parent.parent().children("div.d_code").css('display', 'none'); + code.css('display', 'none'); + }; + if (opts.args) { argsBtn.click(function(){ resetBtn.css('display', 'inline-block'); args.css('height', height(31)); @@ -456,7 +490,9 @@ $(document).ready(function() argsDiv.css('display', 'block'); args.focus(); }); + } + if (opts.stdin) { inputBtn.click(function(){ resetBtn.css('display', 'inline-block'); stdin.css('height', height(31)); @@ -464,60 +500,70 @@ $(document).ready(function() stdinDiv.css('display', 'block'); stdin.focus(); }); - editBtn.click(function(){ - resetBtn.css('display', 'inline-block'); - hideAllWindows(); - code.css('display', 'block'); - editor.refresh(); - editor.focus(); - }); - resetBtn.click(function(){ - resetBtn.css('display', 'none'); - editor.setValue(prepareForMain()); + } + + editBtn.click(function(){ + resetBtn.css('display', 'inline-block'); + hideAllWindows(); + code.css('display', 'block'); + editor.refresh(); + editor.focus(); + }); + resetBtn.click(function(){ + resetBtn.css('display', 'none'); + editor.setValue(prepareForMain()); + if (opts.args) { args.val(orgArgs); + } + if (opts.stdin) { stdin.val(orgStdin); - hideAllWindows(); - parent.parent().children("div.d_code").css('display', 'block'); - }); - runBtn.click(function(){ - resetBtn.css('display', 'inline-block'); - $(this).attr("disabled", true); - hideAllWindows(); - output.css('height', height(31)); - outputDiv.css('display', 'block'); - outputTitle.text("Application output"); - output.html("Running..."); - output.focus(); - - $.ajax({ - type: 'POST', - url: "//dpaste.dzfl.pl/request/", - dataType: "json", - data: - { - 'code' : editor.getValue(), - 'stdin' : stdin.val(), - 'args': args.val() - }, - success: function(data) - { - parseOutput(data, output, outputTitle); - runBtn.attr("disabled", false); - }, - error: function(jqXHR, textStatus, errorThrown ) + } + hideAllWindows(); + parent.parent().children("div.d_code").css('display', 'block'); + }); + runBtn.click(function(){ + resetBtn.css('display', 'inline-block'); + $(this).attr("disabled", true); + hideAllWindows(); + output.css('height', height(31)); + outputDiv.css('display', 'block'); + outputTitle.text("Application output"); + output.html("Running..."); + output.focus(); + + var data = { + 'code' : opts.transformOutput(editor.getValue()), + } + if (opts.stdin) { + data.stdin = stdin.val(); + } + if (opts.args) { + data.args = args.val(); + } + $.ajax({ + type: 'POST', + url: "https://dpaste.dzfl.pl/request/", + dataType: "json", + data: data, + success: function(data) + { + parseOutput(data, output, outputTitle); + runBtn.attr("disabled", false); + }, + error: function(jqXHR, textStatus, errorThrown ) + { + output.html("Temporarily unavailable"); + if (typeof console != "undefined") { - output.html("Temporarily unavailable"); - if (typeof console != "undefined") - { - console.log(textStatus + ": " + errorThrown); - } - - runBtn.attr("disabled", false); + console.log(textStatus + ": " + errorThrown); } - }); + + runBtn.attr("disabled", false); + } }); }); -}); +}; + function setUpExamples() { diff --git a/js/run_examples.js b/js/run_examples.js new file mode 100644 index 0000000000..13b0e71667 --- /dev/null +++ b/js/run_examples.js @@ -0,0 +1,96 @@ +/** + * 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(/(assert<\/span>\((.*)==(.*)\);)+/g, function(match, text, left, right) { + return "writeln(" + left.trim() + "); " + + "// " + right.trim() + ""; + }); +} + +// 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 += code; + } + else { + var codeOut = "void main(){ "; + codeOut += "import " + currentPackage + "; "; + // writing to the stdout is probably often used + codeOut += "import std.stdio: write, writeln, writef, writefln; "; + codeOut += code; + codeOut += "\n}"; + } + return codeOut; +} + +$(document).ready(function() +{ + if ($('body')[0].id == "Home") + return; + + // only for std at the moment + if (!$('body').hasClass("std")) + return; + + // only enable for pre-release version + if (location.pathname.indexOf("prerelease") < 0) + return; + + // ignore not yet compatible modules + // copied from Phobos posix.mak + var ignoredModulesList = "allocator/allocator_list.d,allocator/building_blocks/allocator_list.d,allocator/building_blocks/free_list.d,allocator/building_blocks/quantizer,allocator/building_blocks/quantizer,allocator/building_blocks/stats_collector.d,base64.d,bitmanip.d,concurrency.d,conv.d,csv.d,datetime.d,digest/hmac.d,digest/sha.d,file.d,index.d,isemail.d,logger/core.d,logger/nulllogger.d,math.d,ndslice/selection.d,ndslice/slice.d,numeric.d,stdio.d,traits.d,typecons.d,uni.d,utf.d,uuid.d".split(",") + var currentModulePath = $('body')[0].id.replace('.', '/') + '.d'; + if (ignoredModulesList.filter(function(x) { currentModulePath.indexOf(x) >= 0 }).length > 0) { + return; + } + + $('pre[class~=d_code]').each(function(index) + { + var currentExample = $(this); + var orig = currentExample.html(); + + orig = reformatExample(orig); + + // check whether it is from a ddoced unittest + // manual created tests most likely can't be run without modifications + if (!$(this).parent().parent().prev().hasClass("dlang_runnable")) + return; + + currentExample.replaceWith( + '
' + + '
' + + '' + + '' + + '' + + '
' + + '
' + + '
'+orig+'
' + + '
' + + '
' + + '' + + '
Application output
' + + '
' + + '
' + ); + }); + + $('textarea[class=d_code]').each(function(index) { + var parent = $(this).parent(); + var btnParent = parent.parent().children(".d_example_buttons"); + var outputDiv = parent.children("div.d_code_output"); + setupTextarea(this, {parent: btnParent, outputDiv: outputDiv, + stdin: false, args: false, transformOutput: wrapIntoMain}); + }); +}); diff --git a/posix.mak b/posix.mak index 588a34d3e9..4ad246fadb 100644 --- a/posix.mak +++ b/posix.mak @@ -135,7 +135,7 @@ IMAGES=favicon.ico $(ORGS_USING_D) $(addprefix images/, \ JAVASCRIPT=$(addsuffix .js, $(addprefix js/, \ codemirror-compressed dlang ddox listanchors platform-downloads run \ - run-main-website show_contributors jquery-1.7.2.min)) + run_examples run-main-website show_contributors jquery-1.7.2.min)) STYLES=$(addsuffix .css, $(addprefix css/, \ style print codemirror ddox)) diff --git a/std_navbar-prerelease.ddoc b/std_navbar-prerelease.ddoc index 9fcc2a8c32..897ea9f956 100644 --- a/std_navbar-prerelease.ddoc +++ b/std_navbar-prerelease.ddoc @@ -12,3 +12,4 @@ $(SUBNAV_TEMPLATE $(UL $(MODULE_MENU)) ) _= +EXTRA_FOOTERS=$(SCRIPTLOAD $(STATIC js/run_examples.js))