summaryrefslogtreecommitdiffstats
path: root/lib/parse
diff options
context:
space:
mode:
authorAaron O'Mullan <aaron.omullan@friendco.de>2014-10-13 15:07:46 +0200
committerAaron O'Mullan <aaron.omullan@friendco.de>2014-10-13 15:07:46 +0200
commitd730a3aac601f000d770dbccca02ac4bb2d07245 (patch)
treef80730d3e1aeb578faa69bc25ed75e1dc93cfa75 /lib/parse
parentffb0b1201268134ca697723b473ec74c010442d3 (diff)
downloadgitbook-d730a3aac601f000d770dbccca02ac4bb2d07245.zip
gitbook-d730a3aac601f000d770dbccca02ac4bb2d07245.tar.gz
gitbook-d730a3aac601f000d770dbccca02ac4bb2d07245.tar.bz2
Split section classification to separate files
Maybe have a sections folder in the future and one per type (quiz/exercise/normal) ?
Diffstat (limited to 'lib/parse')
-rw-r--r--lib/parse/is_exercise.js17
-rw-r--r--lib/parse/is_quiz.js45
-rw-r--r--lib/parse/lex.js56
3 files changed, 65 insertions, 53 deletions
diff --git a/lib/parse/is_exercise.js b/lib/parse/is_exercise.js
new file mode 100644
index 0000000..74ed753
--- /dev/null
+++ b/lib/parse/is_exercise.js
@@ -0,0 +1,17 @@
+var _ = require('lodash');
+
+function isExercise(nodes) {
+ var codeType = { type: 'code' };
+
+ // Number of code nodes in section
+ var len = _.filter(nodes, codeType).length;
+
+ return (
+ // Got 3 or 4 code blocks
+ (len === 3 || len === 4) &&
+ // Ensure all nodes are at the end
+ _.all(_.last(nodes, len), codeType)
+ );
+}
+
+module.exports = isExercise;
diff --git a/lib/parse/is_quiz.js b/lib/parse/is_quiz.js
new file mode 100644
index 0000000..aa6125a
--- /dev/null
+++ b/lib/parse/is_quiz.js
@@ -0,0 +1,45 @@
+var _ = require('lodash');
+
+function isQuizNode(node) {
+ return (/^[(\[][ x][)\]]/).test(node.text || node);
+}
+
+function isQuiz(nodes) {
+ if (nodes.length < 3) {
+ return false;
+ }
+
+ // Support having a first paragraph block
+ // before our series of questions
+ var quizNodes = nodes.slice(nodes[0].type === 'paragraph' ? 1 : 0);
+
+ // No questions
+ if (!_.some(quizNodes, { type: 'blockquote_start' })) {
+ return false;
+ }
+
+ // Check if section has list of questions
+ // or table of questions
+ var listIdx = _.findIndex(quizNodes, { type: 'list_item_start' });
+ var tableIdx = _.findIndex(quizNodes, { type: 'table' });
+
+
+ if(
+ // List of questions
+ listIdx !== -1 && isQuizNode(quizNodes[listIdx + 1]) ||
+
+ // Table of questions
+ (
+ tableIdx !== -1 &&
+ // Last entry
+ tableIdx === nodes.length - 1 &&
+ _.every(quizNodes[tableIdx].cells[0].slice(1), isQuizNode)
+ )
+ ) {
+ return true;
+ }
+
+ return false;
+}
+
+module.exports = isQuiz;
diff --git a/lib/parse/lex.js b/lib/parse/lex.js
index d9f9fd9..3391acf 100644
--- a/lib/parse/lex.js
+++ b/lib/parse/lex.js
@@ -1,6 +1,9 @@
var _ = require('lodash');
var kramed = require('kramed');
+var isExercise = require('./is_exercise');
+var isQuiz = require('./is_quiz');
+
// Split a page up into sections (lesson, exercises, ...)
function splitSections(nodes) {
var section = [];
@@ -17,59 +20,6 @@ function splitSections(nodes) {
}, []).concat([section]); // Add remaining nodes
}
-function isQuizNode(node) {
- return (/^[(\[][ x][)\]]/).test(node.text || node);
-}
-
-function isExercise(nodes) {
- var codeType = { type: 'code' };
-
- // Number of code nodes in section
- var len = _.filter(nodes, codeType).length;
-
- return (
- // Got 3 or 4 code blocks
- (len === 3 || len === 4) &&
- // Ensure all nodes are at the end
- _.all(_.last(nodes, len), codeType)
- );
-}
-
-function isQuiz(nodes) {
- if (nodes.length < 3) {
- return false;
- }
-
- // Support having a first paragraph block
- // before our series of questions
- var quizNodes = nodes.slice(nodes[0].type === 'paragraph' ? 1 : 0);
-
- // No questions
- if (!_.some(quizNodes, { type: 'blockquote_start' })) {
- return false;
- }
-
- // Check if section has list of questions
- // or table of questions
- var listIdx = _.findIndex(quizNodes, { type: 'list_item_start' });
- var tableIdx = _.findIndex(quizNodes, { type: 'table' });
-
- if(
- // List of questions
- listIdx !== -1 && isQuizNode(quizNodes[listIdx + 1]) ||
-
- // Table of questions
- (
- tableIdx !== -1 &&
- _.every(quizNodes[tableIdx].cells[0].slice(1), isQuizNode)
- )
- ) {
- return true;
- }
-
- return false;
-}
-
// What is the type of this section
function sectionType(nodes, idx) {
if(isExercise(nodes)) {