diff options
author | James Phillpotts <jphillpotts@scottlogic.co.uk> | 2014-04-08 09:51:11 +0100 |
---|---|---|
committer | Aaron O'Mullan <aaron.omullan@friendco.de> | 2014-04-12 01:25:07 -0700 |
commit | f6595f51119baca04bf91e619f64518d400e5cae (patch) | |
tree | 765ad8ab0f40d3bb68c03e8df1d5246664556e9c /lib | |
parent | ab27725b9935b185a58b725e75aedeb579e87d8f (diff) | |
download | gitbook-f6595f51119baca04bf91e619f64518d400e5cae.zip gitbook-f6595f51119baca04bf91e619f64518d400e5cae.tar.gz gitbook-f6595f51119baca04bf91e619f64518d400e5cae.tar.bz2 |
Tabular quiz
Diffstat (limited to 'lib')
-rw-r--r-- | lib/parse/lex.js | 16 | ||||
-rw-r--r-- | lib/parse/page.js | 18 | ||||
-rw-r--r-- | lib/parse/renderer.js | 17 |
3 files changed, 51 insertions, 0 deletions
diff --git a/lib/parse/lex.js b/lib/parse/lex.js index cec6047..b77b844 100644 --- a/lib/parse/lex.js +++ b/lib/parse/lex.js @@ -34,6 +34,22 @@ function sectionType(nodes, idx) { return 'exercise'; } + if (nodes.length > 2) { + var nonBlockquoteNodes = nodes.slice(nodes[0].type === 'paragraph' ? 1 : 0); + nonBlockquoteNodes.splice(_.findIndex(nonBlockquoteNodes, { type: 'blockquote_start' }), + _.findIndex(nonBlockquoteNodes, { type: 'blockquote_end' })); + + if (nonBlockquoteNodes.length === 2) { + if (_.every(nonBlockquoteNodes, { type: 'table' })) { + if (_.every(nonBlockquoteNodes[0].cells, function(row) { + return _.every(row.slice(1), function(cell) { return cell === "( )"; }); + })) { + return 'quiz'; + } + } + } + } + return 'normal'; } diff --git a/lib/parse/page.js b/lib/parse/page.js index 3479c85..4e26455 100644 --- a/lib/parse/page.js +++ b/lib/parse/page.js @@ -36,6 +36,10 @@ function render(section, _options) { return marked.parser(section, options); } +function quizNodesTest(node) { + return node.type === 'table' || node.type === 'list'; +} + function parsePage(src, options) { options = options || {}; @@ -76,6 +80,20 @@ function parsePage(src, options) { context: codeNodes[3] ? codeNodes[3].text : null, } }; + } else if (section.type === 'quiz') { + var nonQuizNodes = _.reject(section, quizNodesTest); + var quizNodes = _.filter(section, quizNodesTest); + var feedback = nonQuizNodes.splice(_.findIndex(nonQuizNodes, { type: 'blockquote_start' }), _.findIndex(nonQuizNodes, { type: 'blockquote_end' })); + return { + id: section.id, + type: section.type, + content: render(nonQuizNodes), + quiz: { + base: render([quizNodes[0]]), + solution: render([quizNodes[1]]), + feedback: render(feedback.slice(1, feedback.length - 1)) + } + }; } // Render normal pages diff --git a/lib/parse/renderer.js b/lib/parse/renderer.js index 6b45a22..ba543ee 100644 --- a/lib/parse/renderer.js +++ b/lib/parse/renderer.js @@ -5,6 +5,7 @@ var path = require('path'); var marked = require('marked'); +var rendererId = 0; function GitBookRenderer(options, extra_options) { if(!(this instanceof GitBookRenderer)) { @@ -13,6 +14,8 @@ function GitBookRenderer(options, extra_options) { GitBookRenderer.super_.call(this, options); this._extra_options = extra_options; + this.quizRowId = 0; + this.id = rendererId++; } inherits(GitBookRenderer, marked.Renderer); @@ -83,6 +86,20 @@ GitBookRenderer.prototype.image = function(href, title, text) { return GitBookRenderer.super_.prototype.image.call(this, _href, title, text); }; +GitBookRenderer.prototype.tablerow = function(content) { + this.quizRowId += 1; + return GitBookRenderer.super_.prototype.tablerow(content); +}; + +GitBookRenderer.prototype.tablecell = function(content, flags) { + var radioContent = content; + if (content === "( )") { + radioContent = "<input type='radio' name='quiz-row-" + this.id + "-" + this.quizRowId + "'/>"; + } else if (content === "(x)") { + radioContent = "<input type='radio' name='quiz-row-" + this.id + "-" + this.quizRowId + "' checked/>"; + } + return GitBookRenderer.super_.prototype.tablecell(radioContent, flags); +}; // Exports module.exports = GitBookRenderer; |