From d7de9a962dc90b1a56ee2469a4c56078be44be5d Mon Sep 17 00:00:00 2001 From: Sebastian Wilzbach Date: Sat, 3 Feb 2018 17:51:03 +0100 Subject: [PATCH] Automatically inject the latest version (#68) * Make all examples at the docs runnable like on dlang.org * Automatically inject the latest version --- doc/Makefile | 10 +++-- doc/custom.ddoc | 7 ++- doc/dlang.org | 2 +- doc/run_examples_custom.js | 90 ++++++++++++++++++++++++++++++++++++++ 4 files changed, 104 insertions(+), 5 deletions(-) create mode 100644 doc/run_examples_custom.js diff --git a/doc/Makefile b/doc/Makefile index f7798337..0f2188d3 100644 --- a/doc/Makefile +++ b/doc/Makefile @@ -3,7 +3,7 @@ ########################################################### # tags -LATEST:=$(git describe --abbrev=0 --tags | tr -d v) +LATEST:=$(shell git describe --abbrev=0 --tags | tr -d v) # binaries DMD=dmd @@ -88,10 +88,10 @@ $(DOC_OUTPUT_DIR)/$(call D2HTML,$p) : $(call ADDSOURCE,$p) $(STDDOC) ;\ IMAGES=images/mir.svg favicon.ico JAVASCRIPT=$(addsuffix .js, $(addprefix js/, \ - dlang ddox listanchors run run-main-website jquery-1.7.2.min)) + codemirror-compressed dlang ddox listanchors run run_examples jquery-1.7.2.min)) STYLES=$(addsuffix .css, $(addprefix css/, \ - style print custom )) + style print custom codemirror)) ALL_FILES = $(addprefix $(DOC_OUTPUT_DIR)/, \ $(STYLES) $(IMAGES) $(JAVASCRIPT)) @@ -100,6 +100,10 @@ $(DOC_OUTPUT_DIR)/css/custom.css: $(DOC_SOURCE_DIR)/custom.css @mkdir -p $(dir $@) cp $< $@ +$(DOC_OUTPUT_DIR)/js/run_examples.js: $(DOC_SOURCE_DIR)/run_examples_custom.js + @mkdir -p $(dir $@) + cp $< $@ + $(DOC_OUTPUT_DIR)/images/mir.svg: $(ARTWORK_DIR)/logo/mir_site_logo.svg @mkdir -p $(dir $@) cp $< $@ diff --git a/doc/custom.ddoc b/doc/custom.ddoc index 20aff4c7..bb83400e 100644 --- a/doc/custom.ddoc +++ b/doc/custom.ddoc @@ -45,12 +45,15 @@ $(COMMON_HEADERS_DLANG) + $(EXTRA_HEADERS) -$(SCRIPT document.body.className += ' have-javascript') +$(SCRIPT document.body.className += ' have-javascript'; +var currentVersion = "$(LATEST)"; +) $(DIVID top, $(DIVC helper, $(DIVC helper expand-container, Menu $(NAVIGATION) @@ -118,7 +121,9 @@ COMMON_SCRIPTS = $(COMMON_SCRIPTS_DLANG) _= COMMON_SCRIPTS_DLANG = + $(SCRIPTLOAD $(STATIC js/codemirror-compressed.js)) $(SCRIPTLOAD $(STATIC js/run.js)) + $(SCRIPTLOAD $(STATIC js/run_examples.js)) _= COMMON_HEADERS_DLANG= diff --git a/doc/dlang.org b/doc/dlang.org index 2fa23235..8c4cdeaf 160000 --- a/doc/dlang.org +++ b/doc/dlang.org @@ -1 +1 @@ -Subproject commit 2fa232357638ad439f82243dfc7f9d1c2f237c46 +Subproject commit 8c4cdeafd4e5dce01486d828bb3a4ea8b1227da7 diff --git a/doc/run_examples_custom.js b/doc/run_examples_custom.js new file mode 100644 index 00000000..fc6bc8fb --- /dev/null +++ b/doc/run_examples_custom.js @@ -0,0 +1,90 @@ +/** + * Run all unittest examples + * + * Copyright 2016 by D Language Foundation + * + * License: http://boost.org/LICENSE_1_0.txt, Boost License 1.0 + */ + +// wraps a unittest into a runnable script +function wrapIntoMain(code) { + var currentPackage = $('body')[0].id; + // BUMP mir-algorithm image here: https://github.com/dlang-tour/core-exec/blob/master/Dockerfile + // run.dlang.io frontend: https://github.com/dlang-tour/core/blob/master/public/static/js/tour-controller.js#L398 + var codeOut = '/+dub.sdl:\ndependency "mir-random" version="~>'+currentVersion+'"\n+/\n'; + + // dynamically wrap into main if needed + if (code.indexOf("void main") >= 0) { + codeOut += "import " + currentPackage + "; "; + codeOut += code; + } + else { + codeOut += "void main()\n{\n"; + codeOut += " import " + currentPackage + ";\n"; + // writing to the stdout is probably often used + codeOut += " import std.stdio: write, writeln, writef, writefln;\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(); + + // 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( + '
' + + '
' + + '
'+orig+'
' + + '
' + + '
' + + '' + + '
' + + '
' + + '
Edit
' + + '
Run
' + + '' + + '
' + + '
' + + '
Application output
Running...
' + + '
' + ); + }); + + $('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" + }); + }); +});