summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--lib/parse/lex.js14
-rw-r--r--lib/utils/lang.js2
-rw-r--r--test/fixtures/SECTIONS.md68
-rw-r--r--test/sections.js22
4 files changed, 101 insertions, 5 deletions
diff --git a/lib/parse/lex.js b/lib/parse/lex.js
index def74b4..cec6047 100644
--- a/lib/parse/lex.js
+++ b/lib/parse/lex.js
@@ -19,13 +19,17 @@ function splitSections(nodes) {
// What is the type of this section
function sectionType(nodes, idx) {
- var codeNodes = _.filter(nodes, {
- type: 'code'
- }).length;
+ var codeType = { type: 'code' };
+
+ // Number of code nodes in section
+ var len = _.filter(nodes, codeType).length;
if(
- (codeNodes === 3 || codeNodes === 4) &&
- (idx % 2) === 1)
+ // Got 3 or 4 code blocks
+ (len === 3 || len === 4) &&
+ // Ensure all nodes are at the end
+ _.all(_.last(nodes, len), codeType)
+ )
{
return 'exercise';
}
diff --git a/lib/utils/lang.js b/lib/utils/lang.js
index 7fd71e1..9eabbb5 100644
--- a/lib/utils/lang.js
+++ b/lib/utils/lang.js
@@ -5,6 +5,8 @@ var MAP = {
};
function normalize(lang) {
+ if(!lang) { return null; }
+
var lower = lang.toLowerCase();
return MAP[lower] || lower;
}
diff --git a/test/fixtures/SECTIONS.md b/test/fixtures/SECTIONS.md
new file mode 100644
index 0000000..3405605
--- /dev/null
+++ b/test/fixtures/SECTIONS.md
@@ -0,0 +1,68 @@
+# Title
+
+Some text
+
+---
+
+## NOT Exercise
+
+Simple subsection NOT exercise
+
+```
+x = 1
+```
+
+What is this
+
+```
+y = [1, 2, 3]
+```
+
+```
+z = {a: 1, b: 2}
+```
+
+---
+
+## Exercise
+
+Define a variable `x` equal to 10.
+
+```js
+var x =
+```
+
+```js
+var x = 10;
+```
+
+```js
+assert(x == 10);
+```
+
+```js
+// This is context code available everywhere
+// The user will be able to call magicFunc in his code
+function magicFunc() {
+ return 3;
+}
+```
+
+---
+
+## Another exercise
+
+Bla bla bla ... This time with no `context` code.
+
+
+```js
+var x =
+```
+
+```js
+var x = 10;
+```
+
+```js
+assert(x == 10);
+```
diff --git a/test/sections.js b/test/sections.js
new file mode 100644
index 0000000..ef4abf9
--- /dev/null
+++ b/test/sections.js
@@ -0,0 +1,22 @@
+var fs = require('fs');
+var path = require('path');
+var assert = require('assert');
+
+var lex = require('../').parse.lex;
+
+
+var CONTENT = fs.readFileSync(path.join(__dirname, './fixtures/SECTIONS.md'), 'utf8');
+var LEXED = lex(CONTENT);
+
+
+describe('Section parsing', function() {
+ it('should correctly split sections', function() {
+ assert.equal(LEXED.length, 3);
+ });
+
+ it('should robustly detect exercises', function() {
+ assert.equal(LEXED[0].type, 'normal');
+ assert.equal(LEXED[1].type, 'exercise');
+ assert.equal(LEXED[2].type, 'exercise');
+ });
+});