Skip to content

Commit

Permalink
Added support for specifying stdin for an example.
Browse files Browse the repository at this point in the history
  • Loading branch information
anandology committed Feb 28, 2022
1 parent 7b3b335 commit f00cda9
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 4 deletions.
32 changes: 28 additions & 4 deletions courses/assets/livecode.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,10 @@ function setupExample(element) {
outputPreview: false,
mode: null,
runtime: null,
buttons: []
buttons: [],
env: {},
events: {},
headers: {}
},

outputHooks: [],
Expand All @@ -94,9 +97,23 @@ function setupExample(element) {
}
},

triggerEvent(name) {
if (name in this.options.events) {
this.options.events[name](this);
}
},

parseOptions() {
var lang = this.findLanguage();
this.options = {...this.options, ...livecode.getOptions(lang)};
var id = $(this.element).data("id") || "default";
var hasOptions = $(`#livecode-options-${id}`).length > 0;
var options = hasOptions ? $(`#livecode-options-${id}`).data() : {};

this.options = {
...this.options,
...livecode.getOptions(lang),
...options
};
this.options.language = lang;

if (this.element.hasClass("autopreview")) {
Expand Down Expand Up @@ -146,6 +163,8 @@ function setupExample(element) {
this.setupRun();
this.setupPreview();
this.setupTabs();

this.triggerEvent("created");
},

setupPreview() {
Expand Down Expand Up @@ -192,6 +211,7 @@ function setupExample(element) {
var runtime = this.getRuntime();
var url = `${LIVECODE_BASE_URL}/runtimes/${runtime}`;
var codemirror = this.codemirror;
var headers = this.options.headers;

var editor = this;

Expand All @@ -204,7 +224,7 @@ function setupExample(element) {
fetch(url, {
method: "POST",
body: code,
headers: {'x-falcon-mode': mode}
headers: {'x-falcon-mode': mode, ...headers}
})
.then(response => response.text())
.then(output => {
Expand Down Expand Up @@ -267,9 +287,11 @@ function setupExample(element) {
};

editor.setup();
return editor;
}

var livecode = {
editors: [],
defaultOptions: {
golang: {
mode: "go"
Expand Down Expand Up @@ -298,9 +320,11 @@ var livecode = {
},

setup() {
var livecode = this;
$(function() {
$("pre.example").each((i, e) => {
setupExample(e);
var editor = setupExample(e);
livecode.editors.push(editor);
});
});
}
Expand Down
14 changes: 14 additions & 0 deletions overrides/main.html
Original file line number Diff line number Diff line change
@@ -1,6 +1,20 @@
{% extends "base.html" %}

{% block scripts %}

<script type="text/javascript">
// clipboard integration from mkdocs-material overrides the id
// set by the fenced-code-block. This is a workaround to move the
// id to data-id attribute.
var elems = document.getElementsByTagName("pre");
for (var i=0; i<elems.length; i++) {
console.log(elems[i]);
if (elems[i].id) {
elems[i].setAttribute("data-id", elems[i].id);
}
}
</script>

{{ super() }}

<script type="text/javascript" src="../../course.js"></script>
Expand Down

0 comments on commit f00cda9

Please sign in to comment.