diff options
author | Aaron O'Mullan <aaron.omullan@gmail.com> | 2014-04-12 01:51:06 -0700 |
---|---|---|
committer | Aaron O'Mullan <aaron.omullan@gmail.com> | 2014-04-12 01:51:06 -0700 |
commit | 78cb9a56ba2cb89842d9940cf3dd81f582c6270a (patch) | |
tree | b73929b036c03ce961feee64e9e1044cdffb12d6 /lib/parse/renderer.js | |
parent | ab27725b9935b185a58b725e75aedeb579e87d8f (diff) | |
parent | 6173fb76ad3d9ad9357389021aa347df3b1bde92 (diff) | |
download | gitbook-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/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; |