summaryrefslogtreecommitdiffstats
path: root/lib/parse/lex.js
diff options
context:
space:
mode:
authorAaron O'Mullan <aaron.omullan@gmail.com>2014-04-12 01:51:06 -0700
committerAaron O'Mullan <aaron.omullan@gmail.com>2014-04-12 01:51:06 -0700
commit78cb9a56ba2cb89842d9940cf3dd81f582c6270a (patch)
treeb73929b036c03ce961feee64e9e1044cdffb12d6 /lib/parse/lex.js
parentab27725b9935b185a58b725e75aedeb579e87d8f (diff)
parent6173fb76ad3d9ad9357389021aa347df3b1bde92 (diff)
downloadgitbook-78cb9a56ba2cb89842d9940cf3dd81f582c6270a.zip
gitbook-78cb9a56ba2cb89842d9940cf3dd81f582c6270a.tar.gz
gitbook-78cb9a56ba2cb89842d9940cf3dd81f582c6270a.tar.bz2
Merge pull request #85 from GitbookIO/pr/63
Pr/63
Diffstat (limited to 'lib/parse/lex.js')
-rw-r--r--lib/parse/lex.js54
1 files changed, 49 insertions, 5 deletions
diff --git a/lib/parse/lex.js b/lib/parse/lex.js
index cec6047..813dd33 100644
--- a/lib/parse/lex.js
+++ b/lib/parse/lex.js
@@ -17,21 +17,65 @@ function splitSections(nodes) {
}, []).concat([section]); // Add remaining nodes
}
-// What is the type of this section
-function sectionType(nodes, idx) {
+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;
- if(
+ 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)) {
return 'exercise';
+ } else if(isQuiz(nodes)) {
+ return 'quiz';
}
return 'normal';