tiny js script that runs listings in the playground

This commit is contained in:
stisa 2017-10-22 17:08:50 +02:00
commit 50587abe9c
2 changed files with 65 additions and 0 deletions

View file

@ -175,6 +175,44 @@
</div>
</div>
# end for
<script>
// tiny helper that run the code in the playground
var els = Array.prototype.slice.call(document.getElementsByClassName("listing"))
els.forEach(function(element) {
var runDiv = document.createElement("DIV")
runDiv.setAttribute("class", "runDiv")
var btn = document.createElement("BUTTON")
var t = document.createTextNode("Run")
var code = element.innerText
btn.appendChild(t)
btn.onclick = function(){
var httpRequest = new XMLHttpRequest();
httpRequest.open("POST", "https://play.nim-lang.org/compile", true)
function displayContents(e){
if (httpRequest.readyState!=httpRequest.DONE){return}
if (httpRequest.status == 200){
var res = JSON.parse(httpRequest.responseText)
var resDiv = document.createElement("DIV")
resDiv.setAttribute("class", "resDiv successComp")
if (res.log != "Compilation Failed\u000A"){
resDiv.innerHTML = res.log
} else {
resDiv.innerHTML = res.compileLog
resDiv.setAttribute("class","resDiv failedComp")
}
runDiv.appendChild(resDiv)
} else {
console.log("There was a problem with the request.")
}
}
httpRequest.onreadystatechange = displayContents
httpRequest.send(JSON.stringify({"code": code, "compilationTarget": "c"}))
}
runDiv.appendChild(document.createElement("HR"))
runDiv.appendChild(btn)
element.appendChild(runDiv)
}, this);
</script>
</div>
#end proc
#

View file

@ -711,3 +711,30 @@ blockquote p {
color: rgb(109, 109, 109) !important;
}
.runDiv > hr {
border: 1px solid slategray;
}
.runDiv > button{
cursor: pointer;
background-color: lightslategray;
text-decoration: none;
float: right;
color: #FFF;
display: block;
}
.runDiv > .resDiv {
width: 80%;
margin-left: 1em;
padding: 0.2em 1em 0.2em 1em;
display: inline-block;
}
.successComp {
color: lightgreen;
}
.failedComp {
color: lightcoral;
}