diff options
author | Aaron O'Mullan <aaron.omullan@friendco.de> | 2014-04-06 14:50:11 -0700 |
---|---|---|
committer | Aaron O'Mullan <aaron.omullan@friendco.de> | 2014-04-06 14:50:11 -0700 |
commit | 2f8543d7102d0b55b043563ee03e72d38d40cab5 (patch) | |
tree | 44444a9fb06048549d43b5a102d8ca37d64aa363 /lib/generate/site/index.js | |
parent | aee33d67b63fa5392ee7cec48af441f8068be1e8 (diff) | |
download | gitbook-2f8543d7102d0b55b043563ee03e72d38d40cab5.zip gitbook-2f8543d7102d0b55b043563ee03e72d38d40cab5.tar.gz gitbook-2f8543d7102d0b55b043563ee03e72d38d40cab5.tar.bz2 |
Generate search_index.json when generating site
Fixes #24
Diffstat (limited to 'lib/generate/site/index.js')
-rw-r--r-- | lib/generate/site/index.js | 38 |
1 files changed, 36 insertions, 2 deletions
diff --git a/lib/generate/site/index.js b/lib/generate/site/index.js index 9c3c10c..474de2a 100644 --- a/lib/generate/site/index.js +++ b/lib/generate/site/index.js @@ -8,6 +8,8 @@ var fs = require("../fs"); var parse = require("../../parse"); var BaseGenerator = require("../generator"); +var indexer = require('./search_indexer'); + // Swig filter for returning the count of lines in a code section swig.setFilter('lines', function(content) { return content.split('\n').length; @@ -21,6 +23,11 @@ swig.setFilter('mdLink', function(link) { var Generator = function() { BaseGenerator.apply(this, arguments); + // Attach methods to instance + _.bindAll(this); + + this.indexer = indexer(); + // Load base template this.template = swig.compileFile(path.resolve(this.options.theme, 'templates/site.html')); this.langsTemplate = swig.compileFile(path.resolve(this.options.theme, 'templates/langs.html')); @@ -53,6 +60,11 @@ Generator.prototype._writeTemplate = function(tpl, options, output) { }); }; +Generator.prototype.indexPage = function(lexed, pagePath) { + this.indexer.add(lexed, pagePath); + return Q(); +}; + // Convert a markdown file to html Generator.prototype.convertFile = function(content, _input) { var that = this; @@ -67,7 +79,17 @@ Generator.prototype.convertFile = function(content, _input) { return Q() .then(function() { - return parse.page(content, { + // Lex page + return parse.lex(content); + }) + .then(function(lexed) { + // Index page in search + return that.indexPage(lexed, _output) + .then(_.constant(lexed)); + }) + .then(function(lexed) { + // Get HTML generated sections + return parse.page(lexed, { repo: that.options.githubId, dir: path.dirname(input) || '/' }); @@ -111,6 +133,18 @@ Generator.prototype.copyAssets = function() { path.join(that.options.output, "gitbook") ); }; -Generator.prototype.finish = Generator.prototype.copyAssets; + +// Dump search index to disk +Generator.prototype.writeSearchIndex = function() { + return fs.writeFile( + path.join(this.options.output, 'search_index.json'), + this.indexer.dump() + ); +}; + +Generator.prototype.finish = function() { + return this.copyAssets() + .then(this.writeSearchIndex); +}; module.exports = Generator;
\ No newline at end of file |