mirror of
https://github.com/GitbookIO/gitbook.git
synced 2026-09-21 01:53:26 +00:00
Add parsing of summary and associated tests
This commit is contained in:
+74
@@ -1,6 +1,10 @@
|
||||
var Q = require("q");
|
||||
var _ = require("lodash");
|
||||
var path = require("path");
|
||||
|
||||
var fs = require("./utils/fs");
|
||||
var Configuration = require("./configuration");
|
||||
var parser = require("./parser");
|
||||
|
||||
var Book = function(root) {
|
||||
// Root folder of the book
|
||||
@@ -13,6 +17,76 @@ var Book = function(root) {
|
||||
return this.config.options;
|
||||
}
|
||||
});
|
||||
|
||||
// Summary
|
||||
this.summary = [];
|
||||
};
|
||||
|
||||
// Initialize and parse the book: config, summary, glossary
|
||||
Book.prototype.init = function() {
|
||||
var that = this;
|
||||
|
||||
return this.config.load()
|
||||
.then(function() {
|
||||
|
||||
})
|
||||
.thenResolve(this);
|
||||
};
|
||||
|
||||
// Parse summary
|
||||
Book.prototype.parseSummary = function() {
|
||||
var that = this;
|
||||
|
||||
return that.findFile("SUMMARY")
|
||||
.then(function(summary) {
|
||||
if (!summary) throw "No SUMMARY file";
|
||||
|
||||
return that.readFile(summary.path)
|
||||
.then(function(content) {
|
||||
return summary.parser.summary(content);
|
||||
});
|
||||
})
|
||||
.then(function(summary) {
|
||||
that.summary = summary;
|
||||
});
|
||||
};
|
||||
|
||||
// Find file that can be parsed with a specific filename
|
||||
Book.prototype.findFile = function(filename) {
|
||||
var that = this;
|
||||
|
||||
return _.reduce(parser.extensions, function(prev, ext) {
|
||||
return prev.then(function(output) {
|
||||
// Stop if already find a parser
|
||||
if (output) return output;
|
||||
|
||||
var filepath = filename+ext;
|
||||
|
||||
return that.fileExists(filepath)
|
||||
.then(function(exists) {
|
||||
if (!exists) return null;
|
||||
return {
|
||||
parser: parser.get(ext).parser,
|
||||
path: filepath
|
||||
};
|
||||
})
|
||||
});
|
||||
}, Q(null));
|
||||
};
|
||||
|
||||
// Check if a file exists in the book
|
||||
Book.prototype.fileExists = function(filename) {
|
||||
return fs.exists(
|
||||
path.join(this.root, filename)
|
||||
);
|
||||
};
|
||||
|
||||
// Read a file
|
||||
Book.prototype.readFile = function(filename) {
|
||||
return fs.readFile(
|
||||
path.join(this.root, filename),
|
||||
{ encoding: "utf8" }
|
||||
);
|
||||
};
|
||||
|
||||
module.exports= Book;
|
||||
|
||||
@@ -0,0 +1,28 @@
|
||||
var _ = require("lodash");
|
||||
var path = require("path");
|
||||
|
||||
// This list is ordered by priority of parser to use
|
||||
var PARSER = [
|
||||
{
|
||||
extensions: [".md", ".markdown"],
|
||||
parser: require("gitbook-markdown")
|
||||
}
|
||||
];
|
||||
|
||||
// Return a specific parser according to an extension
|
||||
function getParser(ext) {
|
||||
return _.find(PARSER, function(input) {
|
||||
return _.contains(input.extensions, ext);
|
||||
});
|
||||
}
|
||||
|
||||
// Return parser for a file
|
||||
function getParserForFile(filename) {
|
||||
return getParser(path.extname(filename));
|
||||
};
|
||||
|
||||
module.exports = {
|
||||
extensions: _.flatten(_.pluck(PARSER, "extensions")),
|
||||
get: getParser,
|
||||
getForFile: getParserForFile
|
||||
};
|
||||
+2
-1
@@ -9,7 +9,8 @@
|
||||
"lodash": "2.4.1",
|
||||
"graceful-fs": "3.0.5",
|
||||
"fs-extra": "0.14.0",
|
||||
"fstream-ignore": "1.0.2"
|
||||
"fstream-ignore": "1.0.2",
|
||||
"gitbook-markdown": "1.0.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
"mocha": "1.18.2"
|
||||
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
# Summary
|
||||
|
||||
* [Chapter 1](chapter-1/README.md)
|
||||
* [Article 1](chapter-1/ARTICLE1.md)
|
||||
* [Article 2](chapter-1/ARTICLE2.md)
|
||||
* [article 1.2.1](\chapter-1\ARTICLE-1-2-1.md)
|
||||
* [article 1.2.2](/chapter-1/ARTICLE-1-2-2.md)
|
||||
* [Chapter 2](chapter-2/README.md)
|
||||
* [Chapter 3](chapter-3/README.md)
|
||||
* [Chapter 4](chapter-4/README.md)
|
||||
|
||||
@@ -0,0 +1,22 @@
|
||||
var path = require('path');
|
||||
var assert = require('assert');
|
||||
|
||||
var Book = require('../').Book;
|
||||
|
||||
describe('Summary parsing', function () {
|
||||
it('should correctly parse it from markdown', function(done) {
|
||||
var book = new Book(path.join(__dirname, './fixtures/summary/markdown'));
|
||||
book.parseSummary()
|
||||
.then(function() {
|
||||
var LEXED = book.summary;
|
||||
|
||||
assert.equal(LEXED.chapters[0].path,'README.md');
|
||||
assert.equal(LEXED.chapters[1].path,'chapter-1/README.md');
|
||||
assert.equal(LEXED.chapters[2].path,'chapter-2/README.md');
|
||||
assert.equal(LEXED.chapters[3].path,'chapter-3/README.md');
|
||||
})
|
||||
.then(function() {
|
||||
done()
|
||||
}, done);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user