diff options
Diffstat (limited to 'lib/parse/renderer.js')
-rw-r--r-- | lib/parse/renderer.js | 27 |
1 files changed, 27 insertions, 0 deletions
diff --git a/lib/parse/renderer.js b/lib/parse/renderer.js index 6b45a22..2a72d48 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,30 @@ 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); +}; + +var fieldRegex = /^([(\[])([ x])[\])]/; +GitBookRenderer.prototype._createCheckboxAndRadios = function(text) { + var match = fieldRegex.exec(text); + if (!match) { + return text; + } + var field = "<input name='quiz-row-" + this.id + "-" + this.quizRowId + "' type='"; + field += match[1] === '(' ? "radio" : "checkbox"; + field += match[2] === 'x' ? "' checked/>" : "'/>"; + return text.replace(fieldRegex, field); +} + +GitBookRenderer.prototype.tablecell = function(content, flags) { + return GitBookRenderer.super_.prototype.tablecell(this._createCheckboxAndRadios(content), flags); +}; + +GitBookRenderer.prototype.listitem = function(text) { + return GitBookRenderer.super_.prototype.listitem(this._createCheckboxAndRadios(text)); +}; // Exports module.exports = GitBookRenderer; |