Generate multi-lingual index

This commit is contained in:
Samy Pessé
2016-02-24 13:35:59 +01:00
parent bd73c79787
commit e85ad95bcf
4 changed files with 76 additions and 1 deletions
+5
View File
@@ -159,4 +159,9 @@ EbookOutput.prototype.ebookConvertOption = function() {
});
};
// Don't write multi-lingual index for wbook
EbookOutput.prototype.outputMultilingualIndex = function() {
};
module.exports = EbookOutput;
+19
View File
@@ -210,11 +210,30 @@ WebsiteOutput.prototype.finish = function() {
.then(function() {
if (that.book.isLanguageBook()) return;
return that.plugins.copyResources(that.name, that.resolve('gitbook'));
})
// Generate homepage to select languages
.then(function() {
if (!that.book.isMultilingual()) return;
return that.outputMultilingualIndex();
});
};
// ----- Utilities ----
// Write multi-languages index
WebsiteOutput.prototype.outputMultilingualIndex = function() {
var that = this;
return that.render('languages', that.getContext())
.then(function(html) {
return that.writeFile(
'index.html',
html
);
});
};
// Render a template using nunjucks
// Templates are stored in `_layouts` folders
WebsiteOutput.prototype.render = function(tpl, context) {
+17
View File
@@ -84,6 +84,22 @@ function outputDefaultBook(Output, files, summary, opts) {
});
}
// Output a book with a specific generator
function outputBook(Output, files, opts) {
return setupBook(files, opts)
.then(function(book) {
// Parse the book
return book.parse()
// Start generation
.then(function() {
var output = new Output(book);
return output.generate()
.thenResolve(output);
});
});
}
// Log an error
function logError(err) {
console.log(err.stack || err);
@@ -93,6 +109,7 @@ module.exports = {
fs: nodeFS,
setupFS: setupFS,
setupBook: setupBook,
outputBook: outputBook,
setupDefaultBook: setupDefaultBook,
outputDefaultBook: outputDefaultBook,
logError: logError
+35 -1
View File
@@ -25,7 +25,6 @@ describe('Website Output', function() {
it('should correctly copy plugins', function() {
output.should.have.file('gitbook/gitbook-plugin-highlight/website.css');
});
});
describe('Book with chapters', function() {
@@ -58,7 +57,42 @@ describe('Website Output', function() {
output.should.have.file('hello/index.html');
output.should.have.file('hello/test.html');
});
});
describe('Multilingual Book', function() {
var output;
before(function() {
return mock.outputBook(WebsiteOutput, {
'LANGS.md': '# Languages\n\n'
+ '* [en](./en)\n'
+ '* [fr](./fr)\n\n',
'en/README.md': '# Hello',
'fr/README.md': '# Bonjour'
})
.then(function(_output) {
output = _output;
});
});
it('should correctly generate an index.html for each language', function() {
output.should.have.file('en/index.html');
output.should.have.file('fr/index.html');
});
it('should correctly copy assets', function() {
output.should.have.file('gitbook/app.js');
});
it('should not copy assets for each language', function() {
output.should.have.not.file('en/gitbook/app.js');
output.should.have.not.file('fr/gitbook/app.js');
});
it('should correctly generate an index.html', function() {
output.should.have.file('index.html');
});
});
});