diff options
author | Samy Pessé <samypesse@gmail.com> | 2015-01-27 11:15:16 +0100 |
---|---|---|
committer | Samy Pessé <samypesse@gmail.com> | 2015-01-27 11:15:16 +0100 |
commit | 6125df58340456529cfb180cc62308cc00e4e769 (patch) | |
tree | 38cfe330153bc2bb8fcaef18cb446373ee0de0f9 | |
parent | 99cd3148ba99a7bf5afaa9b1bb51749411de9d18 (diff) | |
download | gitbook-6125df58340456529cfb180cc62308cc00e4e769.zip gitbook-6125df58340456529cfb180cc62308cc00e4e769.tar.gz gitbook-6125df58340456529cfb180cc62308cc00e4e769.tar.bz2 |
Add back "page:before" hook
-rw-r--r-- | lib/book.js | 84 | ||||
-rw-r--r-- | lib/generator.js | 2 | ||||
-rw-r--r-- | lib/generators/json.js | 26 | ||||
-rw-r--r-- | lib/generators/website.js | 24 | ||||
-rw-r--r-- | lib/template.js | 69 |
5 files changed, 138 insertions, 67 deletions
diff --git a/lib/book.js b/lib/book.js index 481ca12..2782544 100644 --- a/lib/book.js +++ b/lib/book.js @@ -209,11 +209,8 @@ Book.prototype.generate = function(generator) { that.log.debug.ln("transferring folder", file); return Q(generator.transferFolder(file)); } else if (_.contains(parsers.extensions, path.extname(file)) && that.navigation[file]) { - that.log.debug.ln("parsing", file); - return that.parsePage(file) - .then(function(content) { - return Q(generator.writeParsedFile(content, file)); - }); + that.log.debug.ln("converting", file); + return Q(generator.convertFile(file)); } else { that.log.debug.ln("transferring file", file); return Q(generator.transferFile(file)); @@ -460,8 +457,22 @@ Book.prototype.parseGlossary = function() { }; // Parse a page -Book.prototype.parsePage = function(filename) { - var that = this; +Book.prototype.parsePage = function(filename, options) { + var that = this, page = {}; + options = _.defaults(options || {}, { + // Interpolate before templating + interpolateTemplate: _.identity, + + // Interpolate after templating + interpolateContent: _.identity + }); + + var interpolate = function(fn) { + return Q(fn(page)) + .then(function(_page) { + page = _page; + }); + }; that.log.debug.ln("start parsing file", filename); @@ -470,27 +481,44 @@ Book.prototype.parsePage = function(filename) { if (!filetype) return Q.reject(new Error("Can't parse file: "+filename)); + // Type of parser used + page.type = filetype.name; + + // Path relative to book + page.path = filename; + + // Path absolute in the system + page.rawPath = path.resolve(that.root, filename); + + // Progress in the book + page.progress = parseProgress(that.navigation, filename); + that.log.debug.ln("render template", filename); - return that.template.renderFile(filename) - .then(function(content) { - that.log.debug.ln("use file parser", filetype.name, "for", filename); - return filetype.parser.page(content); - }) - .then(function(page) { - // Type of parser used - page.type = filetype.name; - // Path relative to book - page.path = filename; + // Read file content + return that.readFile(page.path) + .then(function(content) { + page.content = content; - // Path absolute in the system - page.rawPath = path.resolve(that.root, filename); + return interpolate(options.interpolateTemplate); + }) + + // Generate template + .then(function() { + return that.template.renderPage(page); + }) + + // Parse markup + .then(function(content) { + page.content = content; - // Progress in the book - page.progress = parseProgress(that.navigation, filename); + that.log.debug.ln("use file parser", filetype.name, "for", filename); + return filetype.parser.page(page.content); + }) - // Content sections - page.sections = pageUtil.normalize(page.sections, { + // Prepare html + .then(function(_page) { + page.sections = pageUtil.normalize(_page.sections, { input: filename, navigation: that.navigation, base: path.dirname(filename) || './', @@ -498,9 +526,10 @@ Book.prototype.parsePage = function(filename) { glossary: that.glossary }); - return page; + return interpolate(options.interpolateContent); }) - .then(function(page) { + + .then(function() { that.indexPage(page); return page; }); @@ -594,6 +623,11 @@ Book.prototype.parentRoot = function() { return this.root; }; +// Resolve a path in book +Book.prototype.resolve = function(p) { + return path.resolve(this.root, p); +}; + // Normalize a link to .html and convert README -> index Book.prototype.contentLink = function(link) { if ( diff --git a/lib/generator.js b/lib/generator.js index 8b310e7..4ff1373 100644 --- a/lib/generator.js +++ b/lib/generator.js @@ -43,7 +43,7 @@ BaseGenerator.prototype.preparePlugins = function() { }; // Write a parsed file to the output -BaseGenerator.prototype.writeParsedFile = function(page, input) { +BaseGenerator.prototype.convertFile = function(input) { return Q.reject(new Error("Could not convert "+input)); }; diff --git a/lib/generators/json.js b/lib/generators/json.js index a1202ad..f1af395 100644 --- a/lib/generators/json.js +++ b/lib/generators/json.js @@ -17,20 +17,24 @@ Generator.prototype.transferFile = function(input) { }; Generator.prototype.finish = function() { }; // Convert an input file -Generator.prototype.writeParsedFile = function(page) { +Generator.prototype.convertFile = function(input) { var that = this; - var json = { - progress: page.progress, - sections: page.sections - }; - var output = links.changeExtension(page.path, ".json"); - output = path.join(that.options.output, output); + return that.book.parsePage(input) + .then(function(page) { + var json = { + progress: page.progress, + sections: page.sections + }; - return fs.writeFile( - output, - JSON.stringify(json, null, 4) - ); + var output = links.changeExtension(page.path, ".json"); + output = path.join(that.options.output, output); + + return fs.writeFile( + output, + JSON.stringify(json, null, 4) + ); + }); }; // Generate languages index diff --git a/lib/generators/website.js b/lib/generators/website.js index ba1d2ce..08950c8 100644 --- a/lib/generators/website.js +++ b/lib/generators/website.js @@ -123,19 +123,25 @@ Generator.prototype.finish = function() { }; // Convert an input file -Generator.prototype.writeParsedFile = function(page) { +Generator.prototype.convertFile = function(input) { var that = this; - var relativeOutput = this.book.contentLink(page.path); - var output = path.join(that.options.output, relativeOutput); + return that.book.parsePage(input, { + interpolateTemplate: function(page) { + return that.callHook("page:before", page); + }, + interpolateContent: function(page) { + return that.callHook("page", page); + } + }) + .then(function(page) { + var relativeOutput = that.book.contentLink(page.path); + var output = path.join(that.options.output, relativeOutput); - var basePath = path.relative(path.dirname(output), this.options.output) || "."; - if (process.platform === 'win32') basePath = basePath.replace(/\\/g, '/'); + var basePath = path.relative(path.dirname(output), that.options.output) || "."; + if (process.platform === 'win32') basePath = basePath.replace(/\\/g, '/'); - that.book.log.info.ln("write parsed file", page.path, "to", relativeOutput); - return that.normalizePage(page) - .then(function(_page) { - page = _page; + that.book.log.info.ln("write parsed file", page.path, "to", relativeOutput); return that._writeTemplate(that.templates["page"], { progress: page.progress, 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; |