From 8569a741033be6bb3dbfd6fd9ecd3c03f05319c3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Samy=20Pess=C3=A9?= Date: Mon, 19 Jan 2015 15:05:13 +0100 Subject: [PATCH] Use template before parsing glossary, summary and langs --- lib/book.js | 11 ++++++++--- lib/template.js | 25 +++++++++++++++++++++++-- lib/utils/fs.js | 1 + 3 files changed, 32 insertions(+), 5 deletions(-) diff --git a/lib/book.js b/lib/book.js index 8c3561a48..0f5d45213 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 9576fe948..e4df84cf0 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 d39cfaa56..8f04435fb 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();