Skip to content

Commit

Permalink
Automatically inject the latest version (#68)
Browse files Browse the repository at this point in the history
* Make all examples at the docs runnable like on dlang.org

* Automatically inject the latest version
  • Loading branch information
wilzbach authored and 9il committed Feb 3, 2018
1 parent 37e8ea8 commit d7de9a9
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 5 deletions.
10 changes: 7 additions & 3 deletions doc/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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))
Expand All @@ -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 $< $@
Expand Down
7 changes: 6 additions & 1 deletion doc/custom.ddoc
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,15 @@ $(COMMON_HEADERS_DLANG)
<link rel="stylesheet" href="$(STATIC css/style.css)">
<link rel="stylesheet" href="$(STATIC css/custom.css)">
<link rel="stylesheet" href="$(STATIC css/print.css)" media="print">
<link rel="stylesheet" href="$(STATIC css/codemirror.css)">
<link rel="shortcut icon" href="$(FAVICON)">
<meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=0.1, maximum-scale=10.0">
$(EXTRA_HEADERS)
</head>
<body id='$(TITLE)' class='$(BODYCLASS)'>
$(SCRIPT document.body.className += ' have-javascript')
$(SCRIPT document.body.className += ' have-javascript';
var currentVersion = "$(LATEST)";
)
$(DIVID top, $(DIVC helper, $(DIVC helper expand-container,
<a href="$(ROOT_DIR)menu.html" title="Menu" class="hamburger expand-toggle"><span>Menu</span></a>
$(NAVIGATION)
Expand Down Expand Up @@ -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=
Expand Down
2 changes: 1 addition & 1 deletion doc/dlang.org
Submodule dlang.org updated 331 files
90 changes: 90 additions & 0 deletions doc/run_examples_custom.js
Original file line number Diff line number Diff line change
@@ -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(
'<div class="unittest_examples">'
+ '<div class="d_code">'
+ '<pre class="d_code">'+orig+'</pre>'
+ '</div>'
+ '<div class="d_run_code" style="display: block">'
+ '<textarea class="d_code" style="display: none;"></textarea>'
+ '</div>'
+ '<div class="d_example_buttons">'
+ '<div class="editButton"><i class="fa fa-edit" aria-hidden="true"></i> Edit</div>'
+ '<div class="runButton"><i class="fa fa-play" aria-hidden="true"></i> Run</div>'
+ '<div class="resetButton" style="display:none"><i class="fa fa-undo " aria-hidden="true"></i> Reset</div>'
+ '<div class="openInEditorButton" title="Open in an external editor"><i class="fa fa-external-link" aria-hidden="true"></i></div>'
+ '</div>'
+ '<div class="d_code_output"><span class="d_code_title">Application output</span><br><pre class="d_code_output" readonly>Running...</pre>'
+ '</div>'
);
});

$('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"
});
});
});

0 comments on commit d7de9a9

Please sign in to comment.