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 /lib/book.js | |
parent | 99cd3148ba99a7bf5afaa9b1bb51749411de9d18 (diff) | |
download | gitbook-6125df58340456529cfb180cc62308cc00e4e769.zip gitbook-6125df58340456529cfb180cc62308cc00e4e769.tar.gz gitbook-6125df58340456529cfb180cc62308cc00e4e769.tar.bz2 |
Add back "page:before" hook
Diffstat (limited to 'lib/book.js')
-rw-r--r-- | lib/book.js | 84 |
1 files changed, 59 insertions, 25 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 ( |