diff options
author | Samy Pessé <samypesse@gmail.com> | 2014-04-06 12:52:35 -0700 |
---|---|---|
committer | Samy Pessé <samypesse@gmail.com> | 2014-04-06 12:52:35 -0700 |
commit | 53f714b7f3ac2523a4ce98dcb1a1b8a8ee687c43 (patch) | |
tree | a0af3069daec9c7e5b7c2944aee8a107c40a1847 | |
parent | d19906d5b2638cd55c1b65eeab3327d7533ebca1 (diff) | |
download | gitbook-53f714b7f3ac2523a4ce98dcb1a1b8a8ee687c43.zip gitbook-53f714b7f3ac2523a4ce98dcb1a1b8a8ee687c43.tar.gz gitbook-53f714b7f3ac2523a4ce98dcb1a1b8a8ee687c43.tar.bz2 |
Add base for generating languages index
-rw-r--r-- | lib/generate/generator.js | 4 | ||||
-rw-r--r-- | lib/generate/index.js | 36 | ||||
-rw-r--r-- | lib/generate/site/index.js | 56 | ||||
-rw-r--r-- | lib/parse/index.js | 1 | ||||
-rw-r--r-- | lib/parse/langs.js | 23 | ||||
-rw-r--r-- | theme/templates/langs.html | 19 |
6 files changed, 104 insertions, 35 deletions
diff --git a/lib/generate/generator.js b/lib/generate/generator.js index 3fe080e..13022a5 100644 --- a/lib/generate/generator.js +++ b/lib/generate/generator.js @@ -23,6 +23,10 @@ BaseGenerator.prototype.transferFolder = function(input) { ); }; +BaseGenerator.prototype.langsIndex = function(langs) { + return Q.reject(new Error("Langs index is not supported in this generator")); +}; + BaseGenerator.prototype.finish = function() { return Q.reject(new Error("Could not finish generation")); }; diff --git a/lib/generate/index.js b/lib/generate/index.js index b46c96b..c8b98ef 100644 --- a/lib/generate/index.js +++ b/lib/generate/index.js @@ -61,6 +61,11 @@ var generate = function(options) { }) }) + // Create the generator + .then(function() { + generator = new generators[options.generator](options); + }) + // Detect multi-languages book .then(function() { if (_.contains(files, "README.md") && _.contains(files, "LANGS.md")) { @@ -69,22 +74,22 @@ var generate = function(options) { // Generate sub-books .then(function(_langsSummary) { - options.langsSummary = parse.summary(_langsSummary); + options.langsSummary = parse.langs(_langsSummary); // Generated a book for each valid entry return Q.all( - _.chain(langsSummary.chapters) - .filter(function(entry) { - return entry.path != null; - }) - .map(function(entry) { + _.map(options.langsSummary.list, function(entry) { return generate(_.extend({}, options, { input: path.join(options.input, entry.path), output: path.join(options.output, entry.path) })); - }) - .value() + }) ); + }) + + // Generate languages index + .then(function() { + return generator.langsIndex(options.langsSummary); }); } else if (!_.contains(files, "SUMMARY.md") || !_.contains(files, "README.md")) { // Invalid book @@ -101,11 +106,6 @@ var generate = function(options) { options.navigation = parse.navigation(options.summary); }) - // Create the generator - .then(function() { - generator = new generators[options.generator](options); - }) - // Copy file and replace markdown file .then(function() { return Q.all( @@ -127,13 +127,13 @@ var generate = function(options) { .value() ); }) - - // Finish gneration - .then(function() { - return generator.finish(); - }); } }) + + // Finish gneration + .then(function() { + return generator.finish(); + }); }; module.exports = { diff --git a/lib/generate/site/index.js b/lib/generate/site/index.js index bf711d3..a8626a9 100644 --- a/lib/generate/site/index.js +++ b/lib/generate/site/index.js @@ -23,10 +23,36 @@ var Generator = function() { // 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')); }; util.inherits(Generator, BaseGenerator); +// Generate a template +Generator.prototype._writeTemplate = function(tpl, options, output) { + var that = this; + return Q() + .then(function(sections) { + return tpl(_.extend({ + title: that.options.title, + description: that.options.description, + + githubAuthor: that.options.github.split("/")[0], + githubId: that.options.github, + githubHost: that.options.githubHost, + + summary: that.options.summary, + allNavigation: that.options.navigation + }, options)); + }) + .then(function(html) { + return fs.writeFile( + output, + html + ); + }); +}; + // Convert a markdown file to html Generator.prototype.convertFile = function(content, _input) { var that = this; @@ -47,16 +73,7 @@ Generator.prototype.convertFile = function(content, _input) { }); }) .then(function(sections) { - return that.template({ - title: that.options.title, - description: that.options.description, - - githubAuthor: that.options.github.split("/")[0], - githubId: that.options.github, - githubHost: that.options.githubHost, - - summary: that.options.summary, - allNavigation: that.options.navigation, + return that._writeTemplate(that.template, { progress: progress, _input: _input, @@ -64,16 +81,21 @@ Generator.prototype.convertFile = function(content, _input) { basePath: basePath, staticBase: path.join(basePath, "gitbook"), - }); - }) - .then(function(html) { - return fs.writeFile( - output, - html - ); + }, output); }); }; +// Generate languages index +Generator.prototype.langsIndex = function(langs) { + var basePath = "."; + return this._writeTemplate(this.langsTemplate, { + langs: langs.list, + + basePath: basePath, + staticBase: path.join(basePath, "gitbook"), + }, path.join(this.options.output, "index.html")); +}; + // Symlink index.html and copy assets Generator.prototype.finish = function() { var that = this; diff --git a/lib/parse/index.js b/lib/parse/index.js index 951e401..ec98347 100644 --- a/lib/parse/index.js +++ b/lib/parse/index.js @@ -1,5 +1,6 @@ module.exports = { summary: require('./summary'), + langs: require('./langs'), page: require('./page'), progress: require('./progress'), navigation: require('./navigation'), diff --git a/lib/parse/langs.js b/lib/parse/langs.js new file mode 100644 index 0000000..46c3a46 --- /dev/null +++ b/lib/parse/langs.js @@ -0,0 +1,23 @@ +var _ = require("lodash") +var parseSummary = require("./summary"); + +var parseLangs = function(content) { + var summary = parseSummary(content); + + return { + list: _.chain(summary.chapters) + .filter(function(entry) { + return entry.path != null; + }) + .map(function(entry) { + return { + title: entry.title, + path: entry.path, + lang: entry.path.replace("/", "") + }; + }) + .value() + } +}; + +module.exports = parseLangs;
\ No newline at end of file diff --git a/theme/templates/langs.html b/theme/templates/langs.html new file mode 100644 index 0000000..238889d --- /dev/null +++ b/theme/templates/langs.html @@ -0,0 +1,19 @@ +{% extends "layout.html" %} + +{% block title %}{{ title }}{% endblock %} + +{% block content %} +<div class="book-langs-index"> + <h1>{{ title }}</h1> + + <ul class="languages"> + {% for lang in langs %} + <li> + <a href="{{ basePath}}/{{ lang.path }}">{{ lang.title }}</a> + </li> + {% endfor %} + </ul> +</div> +{% endblock %} + +{% block javascript %}{% endblock %}
\ No newline at end of file |