mirror of
https://github.com/GitbookIO/gitbook.git
synced 2026-09-21 01:53:26 +00:00
Use template before parsing glossary, summary and langs
This commit is contained in:
+8
-3
@@ -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;
|
||||
|
||||
+23
-2
@@ -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);
|
||||
});
|
||||
};
|
||||
|
||||
|
||||
@@ -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();
|
||||
|
||||
Reference in New Issue
Block a user