summaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
authorSamy Pessé <samypesse@gmail.com>2014-04-06 13:44:14 -0700
committerSamy Pessé <samypesse@gmail.com>2014-04-06 13:44:14 -0700
commit014b2f9c6902235216fee390078ece0049344c0b (patch)
tree4b7a40abe74cce7b961f8a3bc63da9d4f044b315 /lib
parentfb26b634ce2f332bcc3a8764009bdc65a2245f35 (diff)
parent65cb23feabb7ae311f18c5f50978686d202dafec (diff)
downloadgitbook-014b2f9c6902235216fee390078ece0049344c0b.zip
gitbook-014b2f9c6902235216fee390078ece0049344c0b.tar.gz
gitbook-014b2f9c6902235216fee390078ece0049344c0b.tar.bz2
Merge pull request #35 from GitbookIO/feature/multilangs
Fix #34: Feature/multilangs
Diffstat (limited to 'lib')
-rw-r--r--lib/generate/generator.js4
-rw-r--r--lib/generate/index.js114
-rw-r--r--lib/generate/page/index.js24
-rw-r--r--lib/generate/site/index.js67
-rw-r--r--lib/parse/index.js1
-rw-r--r--lib/parse/langs.js23
6 files changed, 149 insertions, 84 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 38e7503..5f44633 100644
--- a/lib/generate/index.js
+++ b/lib/generate/index.js
@@ -2,6 +2,7 @@ var Q = require("q");
var _ = require("lodash");
var path = require("path");
+var swig = require('swig');
var fs = require("./fs");
var parse = require("../parse");
@@ -54,27 +55,10 @@ var generate = function(options) {
// List all files in the repository
.then(function() {
- return fs.list(options.input);
- })
-
- // Check repository is valid
- .then(function(_files) {
- files = _files;
-
- if (!_.contains(files, "SUMMARY.md") || !_.contains(files, "README.md")) {
- return Q.reject(new Error("Invalid gitbook repository, need SUMMARY.md and README.md"));
- }
- })
-
- // Get summary
- .then(function() {
- return fs.readFile(path.join(options.input, "SUMMARY.md"), "utf-8")
- .then(function(_summary) {
- options.summary = parse.summary(_summary);
-
- // Parse navigation
- options.navigation = parse.navigation(options.summary);
- });
+ return fs.list(options.input)
+ .then(function(_files) {
+ files = _files;
+ })
})
// Create the generator
@@ -82,32 +66,74 @@ var generate = function(options) {
generator = new generators[options.generator](options);
})
- // Copy file and replace markdown file
+ // Detect multi-languages book
.then(function() {
- return Q.all(
- _.chain(files)
- .map(function(file) {
- if (!file) return;
-
- if (file[file.length -1] == "/") {
- return Q(generator.transferFolder(file));
- } else if (path.extname(file) == ".md" && options.navigation[file] != null) {
- return fs.readFile(path.join(options.input, file), "utf-8")
- .then(function(content) {
- return Q(generator.convertFile(content, file));
- });
- } else {
- return Q(generator.transferFile(file));
- }
+ if (_.contains(files, "README.md") && _.contains(files, "LANGS.md")) {
+ // Multi-languages book
+ return fs.readFile(path.join(options.input, "LANGS.md"), "utf-8")
+
+ // Generate sub-books
+ .then(function(_langsSummary) {
+ options.langsSummary = parse.langs(_langsSummary);
+
+ // Generated a book for each valid entry
+ return Q.all(
+ _.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()
- );
- })
- // Finish gneration
- .then(function() {
- return generator.finish();
- });
+ // Generate languages index
+ .then(function() {
+ return generator.langsIndex(options.langsSummary);
+ });
+ } else if (!_.contains(files, "SUMMARY.md") || !_.contains(files, "README.md")) {
+ // Invalid book
+ return Q.reject(new Error("Invalid gitbook repository, need SUMMARY.md and README.md"));
+ } else {
+ // Generate the book
+ return fs.readFile(path.join(options.input, "SUMMARY.md"), "utf-8")
+
+ // Get summary
+ .then(function(_summary) {
+ options.summary = parse.summary(_summary);
+
+ // Parse navigation
+ options.navigation = parse.navigation(options.summary);
+ })
+
+ // Copy file and replace markdown file
+ .then(function() {
+ return Q.all(
+ _.chain(files)
+ .map(function(file) {
+ if (!file) return;
+
+ if (file[file.length -1] == "/") {
+ return Q(generator.transferFolder(file));
+ } else if (path.extname(file) == ".md" && options.navigation[file] != null) {
+ return fs.readFile(path.join(options.input, file), "utf-8")
+ .then(function(content) {
+ return Q(generator.convertFile(content, file));
+ });
+ } else {
+ return Q(generator.transferFile(file));
+ }
+ })
+ .value()
+ );
+ })
+
+ // Finish gneration
+ .then(function() {
+ return generator.finish();
+ });
+ }
+ })
};
module.exports = {
diff --git a/lib/generate/page/index.js b/lib/generate/page/index.js
index 7f730ea..ac815e7 100644
--- a/lib/generate/page/index.js
+++ b/lib/generate/page/index.js
@@ -7,7 +7,7 @@ var hljs = require('highlight.js');
var fs = require("../fs");
var parse = require("../../parse");
-var BaseGenerator = require("../generator");
+var BaseGenerator = require("../site");
// Swig filter: highlight coloration
swig.setFilter('code', function(code, lang) {
@@ -71,30 +71,12 @@ Generator.prototype.finish = function() {
return Q()
// Generate html
.then(function(pages) {
- 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, {
pages: that.pages,
basePath: basePath,
staticBase: path.join(basePath, "gitbook"),
- });
- })
-
- // Write html to index.html
- .then(function(html) {
- return fs.writeFile(
- output,
- html
- );
+ }, output);
})
// Copy assets
diff --git a/lib/generate/site/index.js b/lib/generate/site/index.js
index bf711d3..9c3c10c 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,18 +81,29 @@ 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 that = this;
+ var basePath = ".";
+
+ return this._writeTemplate(this.langsTemplate, {
+ langs: langs.list,
+
+ basePath: basePath,
+ staticBase: path.join(basePath, "gitbook"),
+ }, path.join(this.options.output, "index.html"))
+ .then(function() {
+ // Copy assets
+ return that.copyAssets();
});
};
-// Symlink index.html and copy assets
-Generator.prototype.finish = function() {
+// Copy assets
+Generator.prototype.copyAssets = function() {
var that = this;
return fs.copy(
@@ -83,5 +111,6 @@ Generator.prototype.finish = function() {
path.join(that.options.output, "gitbook")
);
};
+Generator.prototype.finish = Generator.prototype.copyAssets;
module.exports = Generator; \ No newline at end of file
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