summaryrefslogtreecommitdiffstats
path: root/theme
diff options
context:
space:
mode:
authorAaron O'Mullan <aaron.omullan@friendco.de>2014-04-08 16:58:04 -0700
committerAaron O'Mullan <aaron.omullan@friendco.de>2014-04-08 16:58:04 -0700
commit9265b0185aaabd04552c61a56e0aed470a0e5dd7 (patch)
tree070fa43d59eaf06e8e9cf45fb74ab4fc4ad41ed3 /theme
parent392573a9905a36d4a6c3cc1947c533f742b653a2 (diff)
downloadgitbook-9265b0185aaabd04552c61a56e0aed470a0e5dd7.zip
gitbook-9265b0185aaabd04552c61a56e0aed470a0e5dd7.tar.gz
gitbook-9265b0185aaabd04552c61a56e0aed470a0e5dd7.tar.bz2
Add context code support to exercises (optional)
Partial #52, fixes #49, fixes #46
Diffstat (limited to 'theme')
-rw-r--r--theme/assets/app.js24
-rw-r--r--theme/javascript/core/exercise.js3
-rw-r--r--theme/javascript/execute/javascript.js5
-rw-r--r--theme/javascript/utils/execute.js18
-rw-r--r--theme/templates/includes/book/exercise.html3
5 files changed, 39 insertions, 14 deletions
diff --git a/theme/assets/app.js b/theme/assets/app.js
index 3a10746..9daec4a 100644
--- a/theme/assets/app.js
+++ b/theme/assets/app.js
@@ -21146,9 +21146,11 @@ define('execute/javascript',[],function() {
return {
id: "javascript",
assertCode: "function assert(condition, message) { \nif (!condition) { \n throw message || \"Assertion failed\"; \n } \n }\n",
- REPL: JSREPL
+ REPL: JSREPL,
+ sep: ";\n",
};
});
+
define('utils/execute',[
"execute/javascript"
], function(javascript) {
@@ -21222,13 +21224,21 @@ define('utils/execute',[
repl.loadLanguage(lang.id, eventHandler);
};
- var execute = function(lang, solution, validation, callback) {
+ var execute = function(lang, solution, validation, context, callback) {
+ // Language data
+ var langd = LANGUAGES[lang];
+
// Check language is supported
- if (!LANGUAGES[lang]) return callback(new Error("Language '"+lang+"' not available for execution"));
+ if (!langd) return callback(new Error("Language '"+lang+"' not available for execution"));
// Validate with validation code
- var code = [solution, LANGUAGES[lang].assertCode, validation].join(";\n");
- evalJS(LANGUAGES[lang], code, function(err, res) {
+ var code = [
+ context,
+ solution,
+ langd.assertCode,
+ validation,
+ ].join(langd.sep);
+ evalJS(langd, code, function(err, res) {
if(err) return callback(err);
if (res.type == "error") callback(new Error(res.value));
@@ -21238,6 +21248,7 @@ define('utils/execute',[
return execute;
});
+
define('core/exercise',[
"jQuery",
"utils/execute",
@@ -21248,6 +21259,7 @@ define('core/exercise',[
var prepareExercise = function($exercise) {
var codeSolution = $exercise.find(".code-solution").text();
var codeValidation = $exercise.find(".code-validation").text();
+ var codeContext = $exercise.find(".code-context").text();
var editor = ace.edit($exercise.find(".editor").get(0));
editor.setTheme("ace/theme/tomorrow");
@@ -21260,7 +21272,7 @@ define('core/exercise',[
analytic.track("exercise.submit");
- execute("javascript", editor.getValue(), codeValidation, function(err, result) {
+ execute("javascript", editor.getValue(), codeValidation, codeContext, function(err, result) {
$exercise.toggleClass("return-error", err != null);
$exercise.toggleClass("return-success", err == null);
if (err) $exercise.find(".alert-danger").text(err.message || err);
diff --git a/theme/javascript/core/exercise.js b/theme/javascript/core/exercise.js
index f890f2a..5219546 100644
--- a/theme/javascript/core/exercise.js
+++ b/theme/javascript/core/exercise.js
@@ -8,6 +8,7 @@ define([
var prepareExercise = function($exercise) {
var codeSolution = $exercise.find(".code-solution").text();
var codeValidation = $exercise.find(".code-validation").text();
+ var codeContext = $exercise.find(".code-context").text();
var editor = ace.edit($exercise.find(".editor").get(0));
editor.setTheme("ace/theme/tomorrow");
@@ -20,7 +21,7 @@ define([
analytic.track("exercise.submit");
- execute("javascript", editor.getValue(), codeValidation, function(err, result) {
+ execute("javascript", editor.getValue(), codeValidation, codeContext, function(err, result) {
$exercise.toggleClass("return-error", err != null);
$exercise.toggleClass("return-success", err == null);
if (err) $exercise.find(".alert-danger").text(err.message || err);
diff --git a/theme/javascript/execute/javascript.js b/theme/javascript/execute/javascript.js
index d967038..ddbc2c1 100644
--- a/theme/javascript/execute/javascript.js
+++ b/theme/javascript/execute/javascript.js
@@ -2,6 +2,7 @@ define(function() {
return {
id: "javascript",
assertCode: "function assert(condition, message) { \nif (!condition) { \n throw message || \"Assertion failed\"; \n } \n }\n",
- REPL: JSREPL
+ REPL: JSREPL,
+ sep: ";\n",
};
-}); \ No newline at end of file
+});
diff --git a/theme/javascript/utils/execute.js b/theme/javascript/utils/execute.js
index 76033f6..eb2a19b 100644
--- a/theme/javascript/utils/execute.js
+++ b/theme/javascript/utils/execute.js
@@ -71,13 +71,21 @@ define([
repl.loadLanguage(lang.id, eventHandler);
};
- var execute = function(lang, solution, validation, callback) {
+ var execute = function(lang, solution, validation, context, callback) {
+ // Language data
+ var langd = LANGUAGES[lang];
+
// Check language is supported
- if (!LANGUAGES[lang]) return callback(new Error("Language '"+lang+"' not available for execution"));
+ if (!langd) return callback(new Error("Language '"+lang+"' not available for execution"));
// Validate with validation code
- var code = [solution, LANGUAGES[lang].assertCode, validation].join(";\n");
- evalJS(LANGUAGES[lang], code, function(err, res) {
+ var code = [
+ context,
+ solution,
+ langd.assertCode,
+ validation,
+ ].join(langd.sep);
+ evalJS(langd, code, function(err, res) {
if(err) return callback(err);
if (res.type == "error") callback(new Error(res.value));
@@ -86,4 +94,4 @@ define([
};
return execute;
-}); \ No newline at end of file
+});
diff --git a/theme/templates/includes/book/exercise.html b/theme/templates/includes/book/exercise.html
index 1acbe79..fb5aaa6 100644
--- a/theme/templates/includes/book/exercise.html
+++ b/theme/templates/includes/book/exercise.html
@@ -16,6 +16,9 @@
<pre class="hidden code-solution">{{ section.code.solution }}</pre>
<pre class="hidden code-validation">{{ section.code.validation }}</pre>
+{% if section.code.context %}
+<pre class="hidden code-context">{{ section.code.context }}</pre>
+{% endif %}
<div class="btn-group btn-group-justified">
<a href="#" class="btn btn-default action-submit">Submit</a>