mirror of
https://github.com/GitbookIO/gitbook.git
synced 2026-09-23 19:06:31 +00:00
Merge pull request #1014 from GitbookIO/fix/1013
Fix #1013: prioritize structure defined in book.json
This commit is contained in:
+21
-14
@@ -356,10 +356,10 @@ Book.prototype.parsePlugins = function() {
|
||||
// Parse readme to extract defaults title and description
|
||||
Book.prototype.parseReadme = function() {
|
||||
var that = this;
|
||||
var structure = that.config.getStructure('readme');
|
||||
that.log.debug.ln('start parsing readme:', structure);
|
||||
var filename = that.config.getStructure('readme', true);
|
||||
that.log.debug.ln('start parsing readme:', filename);
|
||||
|
||||
return that.findFile(structure)
|
||||
return that.findFile(filename)
|
||||
.then(function(readme) {
|
||||
if (!readme) throw 'No README file';
|
||||
if (!_.contains(that.files, readme.path)) throw 'README file is ignored';
|
||||
@@ -390,10 +390,10 @@ Book.prototype.parseReadme = function() {
|
||||
Book.prototype.parseLangs = function() {
|
||||
var that = this;
|
||||
|
||||
var structure = that.config.getStructure('langs');
|
||||
that.log.debug.ln('start parsing languages index:', structure);
|
||||
var filename = that.config.getStructure('langs', true);
|
||||
that.log.debug.ln('start parsing languages index:', filename);
|
||||
|
||||
return that.findFile(structure)
|
||||
return that.findFile(filename)
|
||||
.then(function(langs) {
|
||||
if (!langs) return [];
|
||||
|
||||
@@ -421,10 +421,10 @@ Book.prototype.parseLangs = function() {
|
||||
Book.prototype.parseSummary = function() {
|
||||
var that = this;
|
||||
|
||||
var structure = that.config.getStructure('summary');
|
||||
that.log.debug.ln('start parsing summary:', structure);
|
||||
var filename = that.config.getStructure('summary', true);
|
||||
that.log.debug.ln('start parsing summary:', filename);
|
||||
|
||||
return that.findFile(structure)
|
||||
return that.findFile(filename)
|
||||
.then(function(summary) {
|
||||
if (!summary) throw 'No SUMMARY file';
|
||||
|
||||
@@ -459,10 +459,10 @@ Book.prototype.parseSummary = function() {
|
||||
Book.prototype.parseGlossary = function() {
|
||||
var that = this;
|
||||
|
||||
var structure = that.config.getStructure('glossary');
|
||||
that.log.debug.ln('start parsing glossary: ', structure);
|
||||
var filename = that.config.getStructure('glossary', true);
|
||||
that.log.debug.ln('start parsing glossary: ', filename);
|
||||
|
||||
return that.findFile(structure)
|
||||
return that.findFile(filename)
|
||||
.then(function(glossary) {
|
||||
if (!glossary) return [];
|
||||
|
||||
@@ -600,12 +600,19 @@ Book.prototype.parsePage = function(filename, options) {
|
||||
Book.prototype.findFile = function(filename) {
|
||||
var that = this;
|
||||
|
||||
return _.reduce(parsers.extensions, function(prev, ext) {
|
||||
var ext = path.extname(filename);
|
||||
var basename = path.basename(filename, ext);
|
||||
|
||||
// Ordered list of extensions to test
|
||||
var exts = parsers.extensions;
|
||||
if (ext) exts = _.uniq([ext].concat(exts));
|
||||
|
||||
return _.reduce(exts, function(prev, ext) {
|
||||
return prev.then(function(output) {
|
||||
// Stop if already find a parser
|
||||
if (output) return output;
|
||||
|
||||
var filepath = filename+ext;
|
||||
var filepath = basename+ext;
|
||||
|
||||
return that.fileExists(filepath)
|
||||
.then(function(exists) {
|
||||
|
||||
@@ -181,8 +181,12 @@ Configuration.prototype.dump = function() {
|
||||
};
|
||||
|
||||
// Get structure file
|
||||
Configuration.prototype.getStructure = function(name) {
|
||||
return this.options.structure[name].split('.').slice(0, -1).join('.');
|
||||
Configuration.prototype.getStructure = function(name, dontStripExt) {
|
||||
var filename = this.options.structure[name];
|
||||
if (dontStripExt) return filename;
|
||||
|
||||
filename = filename.split('.').slice(0, -1).join('.');
|
||||
return filename;
|
||||
};
|
||||
|
||||
// Return normalized language
|
||||
|
||||
@@ -0,0 +1 @@
|
||||
== Readme for the bookk
|
||||
@@ -0,0 +1 @@
|
||||
# Readme for GitHub
|
||||
@@ -0,0 +1 @@
|
||||
# Summary
|
||||
@@ -0,0 +1,5 @@
|
||||
{
|
||||
"structure": {
|
||||
"readme": "README.adoc"
|
||||
}
|
||||
}
|
||||
+37
-37
@@ -1,91 +1,91 @@
|
||||
var fs = require("fs");
|
||||
var path = require("path");
|
||||
var fs = require('fs');
|
||||
var path = require('path');
|
||||
|
||||
describe("JSON generator", function () {
|
||||
describe("Basic Book", function() {
|
||||
describe('JSON generator', function () {
|
||||
describe('Basic Book', function() {
|
||||
var book;
|
||||
|
||||
before(function() {
|
||||
return books.generate("basic", "json")
|
||||
return books.generate('basic', 'json')
|
||||
.then(function(_book) {
|
||||
book = _book;
|
||||
});
|
||||
});
|
||||
|
||||
it("should correctly output a README.json", function() {
|
||||
book.should.have.file("README.json");
|
||||
it('should correctly output a README.json', function() {
|
||||
book.should.have.file('README.json');
|
||||
});
|
||||
|
||||
it("should output a valid json", function() {
|
||||
book.should.have.jsonfile("README.json");
|
||||
it('should output a valid json', function() {
|
||||
book.should.have.jsonfile('README.json');
|
||||
});
|
||||
|
||||
describe("Page Format", function() {
|
||||
describe('Page Format', function() {
|
||||
var page;
|
||||
|
||||
before(function() {
|
||||
page = JSON.parse(
|
||||
fs.readFileSync(
|
||||
path.join(book.options.output, "README.json"),
|
||||
{ encoding: "utf-8" }
|
||||
path.join(book.options.output, 'README.json'),
|
||||
{ encoding: 'utf-8' }
|
||||
)
|
||||
);
|
||||
});
|
||||
|
||||
it("should contains valid section", function() {
|
||||
page.should.have.property("sections").with.lengthOf(1);
|
||||
page.sections[0].should.have.property("content").which.is.a.String();
|
||||
page.sections[0].should.have.property("type", "normal");
|
||||
it('should contains valid section', function() {
|
||||
page.should.have.property('sections').with.lengthOf(1);
|
||||
page.sections[0].should.have.property('content').which.is.a.String();
|
||||
page.sections[0].should.have.property('type', 'normal');
|
||||
});
|
||||
|
||||
it("should contains valid progress", function() {
|
||||
page.should.have.property("progress");
|
||||
page.progress.should.have.property("chapters").with.lengthOf(1);
|
||||
page.progress.should.have.property("current");
|
||||
it('should contains valid progress', function() {
|
||||
page.should.have.property('progress');
|
||||
page.progress.should.have.property('chapters').with.lengthOf(1);
|
||||
page.progress.should.have.property('current');
|
||||
});
|
||||
|
||||
it("should contains no languages", function() {
|
||||
page.should.have.property("langs").with.lengthOf(0);
|
||||
it('should contains no languages', function() {
|
||||
page.should.have.property('langs').with.lengthOf(0);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe("Multilingual Book", function() {
|
||||
describe('Multilingual Book', function() {
|
||||
var book;
|
||||
|
||||
before(function() {
|
||||
return books.generate("languages", "json")
|
||||
return books.generate('languages', 'json')
|
||||
.then(function(_book) {
|
||||
book = _book;
|
||||
});
|
||||
});
|
||||
|
||||
it("should correctly output READMEs", function() {
|
||||
book.should.have.file("README.json");
|
||||
book.should.have.file("en/README.json");
|
||||
book.should.have.file("fr/README.json");
|
||||
it('should correctly output READMEs', function() {
|
||||
book.should.have.file('README.json');
|
||||
book.should.have.file('en/README.json');
|
||||
book.should.have.file('fr/README.json');
|
||||
});
|
||||
|
||||
it("should output valid json", function() {
|
||||
book.should.have.jsonfile("README.json");
|
||||
book.should.have.jsonfile("en/README.json");
|
||||
book.should.have.jsonfile("fr/README.json");
|
||||
it('should output valid json', function() {
|
||||
book.should.have.jsonfile('README.json');
|
||||
book.should.have.jsonfile('en/README.json');
|
||||
book.should.have.jsonfile('fr/README.json');
|
||||
});
|
||||
|
||||
describe("Page Format", function() {
|
||||
describe('Page Format', function() {
|
||||
var page;
|
||||
|
||||
before(function() {
|
||||
page = JSON.parse(
|
||||
fs.readFileSync(
|
||||
path.join(book.options.output, "README.json"),
|
||||
{ encoding: "utf-8" }
|
||||
path.join(book.options.output, 'README.json'),
|
||||
{ encoding: 'utf-8' }
|
||||
)
|
||||
);
|
||||
});
|
||||
|
||||
it("should contains no languages", function() {
|
||||
page.should.have.property("langs").with.lengthOf(2);
|
||||
it('should contains no languages', function() {
|
||||
page.should.have.property('langs').with.lengthOf(2);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@@ -0,0 +1,8 @@
|
||||
describe('Structure', function () {
|
||||
it('should prioritize structure defined in book.json', function() {
|
||||
return books.parse('structure')
|
||||
.then(function(book) {
|
||||
book.readmeFile.should.equal('README.adoc');
|
||||
});
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user