diff options
-rw-r--r-- | lib/book.js | 11 | ||||
-rw-r--r-- | lib/template.js | 25 | ||||
-rw-r--r-- | lib/utils/fs.js | 1 |
3 files changed, 32 insertions, 5 deletions
diff --git a/lib/book.js b/lib/book.js index 8c3561a..0f5d452 100644 --- a/lib/book.js +++ b/lib/book.js @@ -90,7 +90,7 @@ Book.prototype.parseLangs = function() { .then(function(langs) { if (!langs) return []; - return that.readFile(langs.path) + return that.template.renderFile(langs.path) .then(function(content) { return langs.parser.langs(content); }); @@ -108,7 +108,7 @@ Book.prototype.parseSummary = function() { .then(function(summary) { if (!summary) throw "No SUMMARY file"; - return that.readFile(summary.path) + return that.template.renderFile(summary.path) .then(function(content) { return summary.parser.summary(content); }); @@ -126,7 +126,7 @@ Book.prototype.parseGlossary = function() { .then(function(glossary) { if (!glossary) return {}; - return that.readFile(glossary.path) + return that.template.renderFile(glossary.path) .then(function(content) { return glossary.parser.glossary(content); }); @@ -174,4 +174,9 @@ Book.prototype.readFile = function(filename) { ); }; +// Return stat for a file +Book.prototype.statFile = function(filename) { + return fs.stat(path.join(this.root, filename)); +}; + module.exports= Book; diff --git a/lib/template.js b/lib/template.js index 9576fe9..e4df84c 100644 --- a/lib/template.js +++ b/lib/template.js @@ -1,17 +1,38 @@ var _ = require("lodash"); +var Q = require("q"); var nunjucks = require("nunjucks"); var TemplateEngine = function(book) { this.book = book; + + // Nunjucks env + this.env = new nunjucks.Environment( + new nunjucks.FileSystemLoader(book.root), + { + // Escaping is done after by the markdown parser + autoescape: false + } + ); }; // Render a file from the book TemplateEngine.prototype.renderFile = function(filename) { var that = this; - return this.book.readFile(filename) - .then(function(content) { + return that.book.statFile(filename) + .then(function(stat) { + var context = { + // Variabels from book.json + book: that.book.options.variables, + + // infos about the file + file: { + path: filename, + mtime: stat.mtime + } + }; + return Q.nfcall(that.env.render.bind(that.env), filename, context); }); }; diff --git a/lib/utils/fs.js b/lib/utils/fs.js index d39cfaa..8f04435 100644 --- a/lib/utils/fs.js +++ b/lib/utils/fs.js @@ -63,6 +63,7 @@ var getFiles = function(path) { module.exports = { list: getFiles, + stat: Q.denodeify(fs.stat), readFile: Q.denodeify(fs.readFile), writeFile: function(filename, data, options) { var d = Q.defer(); |