summaryrefslogtreecommitdiffstats
path: root/lib/generate
diff options
context:
space:
mode:
authorSamy Pessé <samypesse@gmail.com>2014-04-06 12:52:35 -0700
committerSamy Pessé <samypesse@gmail.com>2014-04-06 12:52:35 -0700
commit53f714b7f3ac2523a4ce98dcb1a1b8a8ee687c43 (patch)
treea0af3069daec9c7e5b7c2944aee8a107c40a1847 /lib/generate
parentd19906d5b2638cd55c1b65eeab3327d7533ebca1 (diff)
downloadgitbook-53f714b7f3ac2523a4ce98dcb1a1b8a8ee687c43.zip
gitbook-53f714b7f3ac2523a4ce98dcb1a1b8a8ee687c43.tar.gz
gitbook-53f714b7f3ac2523a4ce98dcb1a1b8a8ee687c43.tar.bz2
Add base for generating languages index
Diffstat (limited to 'lib/generate')
-rw-r--r--lib/generate/generator.js4
-rw-r--r--lib/generate/index.js36
-rw-r--r--lib/generate/site/index.js56
3 files changed, 61 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;