Skip to content

Commit

Permalink
Switch to use tour.dlang.org instead of DPaste
Browse files Browse the repository at this point in the history
  • Loading branch information
wilzbach committed Jun 28, 2017
1 parent d47df2c commit dc0492e
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 32 deletions.
106 changes: 75 additions & 31 deletions js/run.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,53 +66,94 @@ function safeVar(data, path)
return res;
}

function parseOutput(data, o, oTitle)
var backends = {
"dpaste": {
"url": "https://dpaste.dzfl.pl/request/",
"requestTransform": function(data) {
return data;
},
"parseOutput": function(data) {
var r = {};
if (data.compilation === "undefined")
return null;
r.cout = safeVar(data, "compilation.stdout");
r.stdout = safeVar(data, "runtime.stdout");
r.stderr = safeVar(data, "runtime.stderr");
r.ctime = parseInt(safeVar(data, "compilation.time"));
r.rtime = parseInt(safeVar(data, "runtime.time"));
r.cstatus = parseInt(safeVar(data, "compilation.status"));
r.rstatus = parseInt(safeVar(data, "runtime.status"));
r.cerr = safeVar(data, "compilation.err");
r.rerr = safeVar(data, "runtime.err");
r.defaultOutput = data.output || opts.defaultOutput;
return r;
}
},
"tour": {
"url": "https://tour.dlang.org/api/v1/run",
"requestTransform": function(data) {
data.source = data.code;
delete data.code;
return data;
},
"parseOutput": function(data, opts) {
var r = {};
if (data.success === "undefined") {
return null;
}
r.cout = data.success === false ? data.output : "";
r.stdout = data.success === true ? data.output : "";
r.stderr = "";
r.ctime = "";
r.rtime = "";
r.cstatus = data.errors.length === 0 ? 0 : 1;
r.rstatus = data.success === true ? 0 : 1;
r.cerr = "";
r.rerr = "";
r.defaultOutput = data.output || opts.defaultOutput;
return r;
}
}
};

function parseOutput(res, o, oTitle)
{
if (typeof data.compilation == "undefined")
if (!res)
{
o.text("Temporarily unavailable");
return;
}

var output = "";
var cout = safeVar(data, "compilation.stdout");
var stdout = safeVar(data, "runtime.stdout");
var stderr = safeVar(data, "runtime.stderr");
var ctime = parseInt(safeVar(data, "compilation.time"));
var rtime = parseInt(safeVar(data, "runtime.time"));
var cstatus = parseInt(safeVar(data, "compilation.status"));
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)
var defaultOutput = res.defaultOutput || '-- No output --';

if (res.cstatus != 0)
{
oTitle.text("Compilation output ("+cstatus+": "+cerr+")");
oTitle.text("Compilation output ("+res.cstatus+": "+res.cerr+")");
if ($.browser.msie)
o.html(nl2br(cout));
o.html(nl2br(res.cout));
else
o.text(cout);
o.text(res.cout);

return;
}
else
{
oTitle.text("Application output");// (compile "+ctime+"ms, run "+rtime+"ms)");
if ( cout != "")
output = 'Compilation output: \n' + cout + "\n";
if ( res.cout != "")
output = 'Compilation output: \n' + res.cout + "\n";

output += (stdout == "" && stderr == "" ? defaultOutput : stdout);
output += (res.stdout == "" && res.stderr == "" ? res.defaultOutput : res.stdout);

if (stderr != "")
output += stderr;
if (res.stderr != "")
output += res.stderr;

if (rstatus != 0)
oTitle.text("Application output ("+rstatus+": "+rerr+")");
if (res.rstatus != 0)
oTitle.text("Application output ("+res.rstatus+": "+res.rerr+")");
}

if ($.browser.msie)
o.html(nl2br(cout));
o.html(nl2br(res.cout));
else
o.text(output);
}
Expand Down Expand Up @@ -179,6 +220,8 @@ function setupTextarea(el, opts)
transformOutput: function(out) { return out }
}, opts);

var backend = backends[opts.backend || "dpaste"];

if (!!opts.parent)
var parent = opts.parent;
else
Expand Down Expand Up @@ -327,23 +370,24 @@ function setupTextarea(el, opts)
output.focus();

var data = {
'code' : opts.transformOutput(editor.getValue()),
}
code: opts.transformOutput(editor.getValue())
};
if (opts.stdin) {
data.stdin = stdin.val();
}
if (opts.args) {
data.args = args.val();
}
data = backend.requestTransform(data);
$.ajax({
type: 'POST',
url: "https://dpaste.dzfl.pl/request/",
url: backend.url,
dataType: "json",
data: data,
contentType:"application/json; charset=utf-8",
data: JSON.stringify(data),
success: function(data)
{
data.defaultOutput = opts.defaultOutput;
parseOutput(data, output, outputTitle);
parseOutput(backend.parseOutput(data, opts), output, outputTitle);
runBtn.attr("disabled", false);
},
error: function(jqXHR, textStatus, errorThrown )
Expand Down
3 changes: 2 additions & 1 deletion js/run_examples.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,8 @@ $(document).ready(function()
transformOutput: wrapIntoMain,
defaultOutput: "All tests passed",
keepCode: true,
outputHeight: "auto"
outputHeight: "auto",
backend: "tour"
});
});
});

0 comments on commit dc0492e

Please sign in to comment.