diff options
author | Samy Pesse <samypesse@gmail.com> | 2016-05-05 11:00:05 +0200 |
---|---|---|
committer | Samy Pesse <samypesse@gmail.com> | 2016-05-05 11:00:05 +0200 |
commit | 34947b5e207e84ef43a8194c0ec7bac19dccf709 (patch) | |
tree | 06e687700d95590cae678f8bef381e8a62dc0129 /lib/models | |
parent | c621380b664bcbef087df571b662e7a34e098168 (diff) | |
download | gitbook-34947b5e207e84ef43a8194c0ec7bac19dccf709.zip gitbook-34947b5e207e84ef43a8194c0ec7bac19dccf709.tar.gz gitbook-34947b5e207e84ef43a8194c0ec7bac19dccf709.tar.bz2 |
Add mode TemplateOutput to represent {content + blocks} from template
Diffstat (limited to 'lib/models')
-rw-r--r-- | lib/models/templateBlock.js | 54 | ||||
-rw-r--r-- | lib/models/templateEngine.js | 4 | ||||
-rw-r--r-- | lib/models/templateOutput.js | 44 |
3 files changed, 58 insertions, 44 deletions
diff --git a/lib/models/templateBlock.js b/lib/models/templateBlock.js index 2ec1328..200e048 100644 --- a/lib/models/templateBlock.js +++ b/lib/models/templateBlock.js @@ -8,8 +8,6 @@ var TemplateShortcut = require('./templateShortcut'); var NODE_ENDARGS = '%%endargs%%'; -var blockBodies = {}; - var TemplateBlock = Immutable.Record({ // Name of block, also the start tag name: String(), @@ -26,9 +24,6 @@ var TemplateBlock = Immutable.Record({ // List of shortcuts to replace with this block shortcuts: Immutable.Map(), - // Function to execute in post processing - post: null, - parse: true }, 'TemplateBlock'); @@ -36,10 +31,6 @@ TemplateBlock.prototype.getName = function() { return this.get('name'); }; -TemplateBlock.prototype.getPost = function() { - return this.get('post'); -}; - TemplateBlock.prototype.getParse = function() { return this.get('parse'); }; @@ -85,7 +76,7 @@ TemplateBlock.prototype.getExtensionName = function() { @return {Nunjucks.Extension} */ -TemplateBlock.prototype.toNunjucksExt = function(mainContext) { +TemplateBlock.prototype.toNunjucksExt = function(mainContext, blocksOutput) { var that = this; var name = this.getName(); var endTag = this.getEndTag(); @@ -195,7 +186,7 @@ TemplateBlock.prototype.toNunjucksExt = function(mainContext) { return that.applyBlock(mainBlock, ctx); }) .then(function(result) { - return that.blockResultToHtml(result); + return that.blockResultToHtml(result, blocksOutput); }) .nodeify(callback); }; @@ -221,19 +212,19 @@ TemplateBlock.prototype.applyBlock = function(inner, context) { var r = processFn.call(context, inner); if (Promise.isPromiseAlike(r)) { - return r.then(this.handleBlockResult.bind(this)); + return r.then(this.normalizeBlockResult.bind(this)); } else { - return this.handleBlockResult(r); + return this.normalizeBlockResult(r); } }; /** - Handle result from a block process function + Normalize result from a block process function - @param {Object} result + @param {Object|String} result @return {Object} */ -TemplateBlock.prototype.handleBlockResult = function(result) { +TemplateBlock.prototype.normalizeBlockResult = function(result) { if (is.string(result)) { result = { body: result }; } @@ -246,15 +237,17 @@ TemplateBlock.prototype.handleBlockResult = function(result) { Convert a block result to HTML @param {Object} result + @param {Object} blocksOutput: stored post processing blocks in this object @return {String} */ -TemplateBlock.prototype.blockResultToHtml = function(result) { +TemplateBlock.prototype.blockResultToHtml = function(result, blocksOutput) { var parse = this.getParse(); var indexedKey; - var toIndex = (!parse) || (this.getPost() !== undefined); + var toIndex = (!parse) || (result.post !== undefined); if (toIndex) { - indexedKey = TemplateBlock.indexBlockResult(result); + indexedKey = genKey(); + blocksOutput[indexedKey] = result; } // Parsable block, just return it @@ -268,29 +261,6 @@ TemplateBlock.prototype.blockResultToHtml = function(result) { }; /** - Index a block result, and return the indexed key - - @param {Object} blk - @return {String} -*/ -TemplateBlock.indexBlockResult = function(blk) { - var key = genKey(); - blockBodies[key] = blk; - - return key; -}; - -/** - Get a block results indexed for a specific key - - @param {String} key - @return {Object|undefined} -*/ -TemplateBlock.getBlockResultByKey = function(key) { - return blockBodies[key]; -}; - -/** Create a template block from a function or an object @param {String} blockName diff --git a/lib/models/templateEngine.js b/lib/models/templateEngine.js index 243bfc6..5724d55 100644 --- a/lib/models/templateEngine.js +++ b/lib/models/templateEngine.js @@ -67,7 +67,7 @@ TemplateEngine.prototype.getBlock = function(name) { @return {Nunjucks.Environment} */ -TemplateEngine.prototype.toNunjucks = function() { +TemplateEngine.prototype.toNunjucks = function(blocksOutput) { var loader = this.getLoader(); var blocks = this.getBlocks(); var filters = this.getFilters(); @@ -101,7 +101,7 @@ TemplateEngine.prototype.toNunjucks = function() { // Add blocks blocks.forEach(function(block) { var extName = block.getExtensionName(); - var Ext = block.toNunjucksExt(context); + var Ext = block.toNunjucksExt(context, blocksOutput); env.addExtension(extName, new Ext()); }); diff --git a/lib/models/templateOutput.js b/lib/models/templateOutput.js new file mode 100644 index 0000000..cd65a05 --- /dev/null +++ b/lib/models/templateOutput.js @@ -0,0 +1,44 @@ +var Immutable = require('immutable'); + +var TemplateOutput = Immutable.Record({ + // Text content of the template + content: String(), + + // Map of blocks to replace / post process + blocks: Immutable.Map() +}, 'TemplateOutput'); + +TemplateOutput.prototype.getContent = function() { + return this.get('content'); +}; + +TemplateOutput.prototype.getBlocks = function() { + return this.get('blocks'); +}; + +/** + Update content of this output + + @param {String} content + @return {TemplateContent} +*/ +TemplateOutput.prototype.setContent = function(content) { + return this.set('content', content); +}; + +/** + Create a TemplateOutput from a text content + and an object containing block definition + + @param {String} content + @param {Object} blocks + @return {TemplateOutput} +*/ +TemplateOutput.create = function(content, blocks) { + return new TemplateOutput({ + content: content, + blocks: Immutable.fromJS(blocks) + }); +}; + +module.exports = TemplateOutput; |