diff options
Diffstat (limited to 'lib/template.js')
-rw-r--r-- | lib/template.js | 69 |
1 files changed, 48 insertions, 21 deletions
diff --git a/lib/template.js b/lib/template.js index 47244e4..d04deee 100644 --- a/lib/template.js +++ b/lib/template.js @@ -35,6 +35,10 @@ var BookLoader = nunjucks.Loader.extend({ }); }) .nodeify(callback); + }, + + resolve: function(from, to) { + return path.resolve(path.dirname(from), to); } }); @@ -62,30 +66,53 @@ var TemplateEngine = function(book) { ); }; +// Render a string from the book +TemplateEngine.prototype.renderString = function(content, context, options) { + var context = _.extend({}, context, { + // Variables from book.json + book: this.book.options.variables, + + // infos about gitbook + gitbook: { + version: pkg.version + } + }); + options = _.defaults(options || {}, { path: null}); + if (options.path) options.path = this.book.resolve(options.path); + + return Q.nfcall(this.env.renderString.bind(this.env), content, context, options); +}; + // Render a file from the book -TemplateEngine.prototype.renderFile = function(filename) { - var that = this; - - return that.book.statFile(filename) - .then(function(stat) { - var context = { - // Variables from book.json - book: that.book.options.variables, - - // infos about the file - file: { - path: filename, - mtime: stat.mtime - }, - - // infos about gitbook - gitbook: { - version: pkg.version +TemplateEngine.prototype.renderFile = function(filename, options) { + var that = this, context; + + return that.book.readFile(filename) + .then(function(content) { + return that.renderString(content, {}, { + path: filename + }); + }); +}; + +// Render a page from the book +TemplateEngine.prototype.renderPage = function(page) { + var that = this, context; + + return that.book.statFile(page.path) + .then(function(stat) { + context = { + // infos about the file + file: { + path: page.path, + mtime: stat.mtime } - }; + }; - return Q.nfcall(that.env.render.bind(that.env), filename, context); - }); + return that.renderString(page.content, context, { + path: page.path + }); + }); }; module.exports = TemplateEngine; |