diff options
author | Samy Pessé <samypesse@gmail.com> | 2015-01-29 14:18:13 +0100 |
---|---|---|
committer | Samy Pessé <samypesse@gmail.com> | 2015-01-29 14:18:13 +0100 |
commit | 9172948c182a7d90afcf499856aea7bc7afdfec4 (patch) | |
tree | 3a07e9ba455bba071af55321b84221fd87fa3498 /lib/template.js | |
parent | 2bec7c5e10274b260faea3e007b056c19760cc6e (diff) | |
download | gitbook-9172948c182a7d90afcf499856aea7bc7afdfec4.zip gitbook-9172948c182a7d90afcf499856aea7bc7afdfec4.tar.gz gitbook-9172948c182a7d90afcf499856aea7bc7afdfec4.tar.bz2 |
Remove used blocks from templates
Diffstat (limited to 'lib/template.js')
-rw-r--r-- | lib/template.js | 22 |
1 files changed, 16 insertions, 6 deletions
diff --git a/lib/template.js b/lib/template.js index 433104c..f142763 100644 --- a/lib/template.js +++ b/lib/template.js @@ -80,18 +80,22 @@ var TemplateEngine = function(book) { // Process a block in a context TemplateEngine.prototype.processBlock = function(blk) { if (_.isString(blk)) blk = { body: blk }; + blk = _.defaults(blk, { parse: false }); blk.id = _.uniqueId("blk"); + //Parsable block, just return it + if (blk.parse) { + return blk.body; + } + // Add to global map this.blocks[blk.id] = blk; - // If don't parse id, return it as a macro - if (!blk.parse) return "%+%"+blk.id+"%+%"; - - return blk.body; + // Return it as a macro + return "%+%"+blk.id+"%+%"; }; // Replace blocks by body after processing @@ -101,8 +105,14 @@ TemplateEngine.prototype.replaceBlocks = function(content) { return content.replace(/\%\+\%([\s\S]+?)\%\+\%/g, function(match, key) { var blk = that.blocks[key]; - if (!blk || blk.parse) return match; - return blk.body; + if (!blk) return match; + + var body = blk.body; + + // Remove it from map + delete that.blocks[key]; + + return body; }); }; |