diff options
author | Samy Pessé <samypesse@gmail.com> | 2015-01-19 10:59:43 +0100 |
---|---|---|
committer | Samy Pessé <samypesse@gmail.com> | 2015-01-19 10:59:43 +0100 |
commit | 39b2aaf4898921fc845ffa165d038fa58404548d (patch) | |
tree | 251e5344791e60c0a17fadf193af4a5b9e5b84ce | |
parent | 58b04da60fd819ad851a0ccc826b45bbbafaa0b5 (diff) | |
download | gitbook-39b2aaf4898921fc845ffa165d038fa58404548d.zip gitbook-39b2aaf4898921fc845ffa165d038fa58404548d.tar.gz gitbook-39b2aaf4898921fc845ffa165d038fa58404548d.tar.bz2 |
Add parsing of summary and associated tests
-rw-r--r-- | lib/book.js | 74 | ||||
-rw-r--r-- | lib/parser.js | 28 | ||||
-rw-r--r-- | package.json | 3 | ||||
-rw-r--r-- | test/fixtures/summary/markdown/SUMMARY.md | 11 | ||||
-rw-r--r-- | test/summary.js | 22 |
5 files changed, 137 insertions, 1 deletions
diff --git a/lib/book.js b/lib/book.js index 62667a9..9c489f1 100644 --- a/lib/book.js +++ b/lib/book.js @@ -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; diff --git a/lib/parser.js b/lib/parser.js new file mode 100644 index 0000000..b4243e0 --- /dev/null +++ b/lib/parser.js @@ -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 +}; diff --git a/package.json b/package.json index e8a279d..5d034e8 100644 --- a/package.json +++ b/package.json @@ -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" diff --git a/test/fixtures/summary/markdown/SUMMARY.md b/test/fixtures/summary/markdown/SUMMARY.md new file mode 100644 index 0000000..a51deae --- /dev/null +++ b/test/fixtures/summary/markdown/SUMMARY.md @@ -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) + diff --git a/test/summary.js b/test/summary.js new file mode 100644 index 0000000..a90212f --- /dev/null +++ b/test/summary.js @@ -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); + }); +}); |