diff options
author | Samy Pesse <samypesse@gmail.com> | 2015-09-12 19:25:01 +0200 |
---|---|---|
committer | Samy Pesse <samypesse@gmail.com> | 2015-09-12 19:25:01 +0200 |
commit | 601ce3add380392fe9c1d598e01f05034ff058c3 (patch) | |
tree | 78bc33c81083ee73fd7c1f2bcbcf42cdc2c05a05 /lib/template.js | |
parent | 08ff39fd0b95eed935241da96dcd43ef660f8860 (diff) | |
download | gitbook-601ce3add380392fe9c1d598e01f05034ff058c3.zip gitbook-601ce3add380392fe9c1d598e01f05034ff058c3.tar.gz gitbook-601ce3add380392fe9c1d598e01f05034ff058c3.tar.bz2 |
Add default blocks (html and code)
Diffstat (limited to 'lib/template.js')
-rw-r--r-- | lib/template.js | 37 |
1 files changed, 28 insertions, 9 deletions
diff --git a/lib/template.js b/lib/template.js index 8014405..8ea8038 100644 --- a/lib/template.js +++ b/lib/template.js @@ -8,6 +8,7 @@ var git = require("./utils/git"); var fs = require("./utils/fs"); var batch = require("./utils/batch"); var pkg = require("../package.json"); +var defaultBlocks = require("./blocks"); // The loader should handle relative and git url @@ -71,16 +72,17 @@ var TemplateEngine = function(book) { // List of tags shortcuts this.shortcuts = []; - // Map of blocks + // Map of blocks bodies (that requires post-processing) + this.blockBodies = {}; + + // Map of added blocks this.blocks = {}; // Bind methods _.bindAll(this); - // Default block "html" that return html not parsed - this.addBlock("html", { - process: _.identity - }); + // Add default blocks + this.addBlocks(defaultBlocks); }; // Process a block in a context @@ -96,7 +98,7 @@ TemplateEngine.prototype.processBlock = function(blk) { var toAdd = (!blk.parse) || (blk.post != undefined); // Add to global map - if (toAdd) this.blocks[blk.id] = blk; + if (toAdd) this.blockBodies[blk.id] = blk; //Parsable block, just return it if (blk.parse) { @@ -113,7 +115,7 @@ TemplateEngine.prototype.replaceBlocks = function(content) { var that = this; return content.replace(/\@\%\@([\s\S]+?)\@\%\@/g, function(match, key) { - var blk = that.blocks[key]; + var blk = that.blockBodies[key]; if (!blk) return match; var body = blk.body; @@ -160,10 +162,19 @@ TemplateEngine.prototype.addFilter = function(filterName, func) { return true; }; +// Add multiple filters +TemplateEngine.prototype.addFilters = function(filters) { + _.each(filters, function(filter, name) { + this.addFilter(name, filter); + }, this); +}; + // Add a block TemplateEngine.prototype.addBlock = function(name, block) { var that = this; + if (_.isFunction(block)) block = { process: block }; + block = _.defaults(block || {}, { shortcuts: [], end: "end"+name, @@ -178,6 +189,7 @@ TemplateEngine.prototype.addBlock = function(name, block) { } this.log.debug.ln("add block '"+name+"'"); + this.blocks[name] = block; var Ext = function () { this.tags = [name]; @@ -298,6 +310,13 @@ TemplateEngine.prototype.addBlock = function(name, block) { }, this); }; +// Add multiple blocks +TemplateEngine.prototype.addBlocks = function(blocks) { + _.each(blocks, function(block, name) { + this.addBlock(name, block); + }, this); +}; + // Apply a shortcut to a string TemplateEngine.prototype._applyShortcut = function(parser, content, shortcut) { if (!_.contains(shortcut.parsers, parser)) return content; @@ -383,7 +402,7 @@ TemplateEngine.prototype.postProcess = function(content) { return Q(content) .then(that.replaceBlocks) .then(function(_content) { - return batch.execEach(that.blocks, { + return batch.execEach(that.blockBodies, { max: 20, fn: function(blk, blkId) { return Q() @@ -392,7 +411,7 @@ TemplateEngine.prototype.postProcess = function(content) { return blk.post(); }) .then(function() { - delete that.blocks[blkId]; + delete that.blockBodies[blkId]; }); } }) |