summaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
Diffstat (limited to 'lib')
-rw-r--r--lib/book.js74
-rw-r--r--lib/parser.js28
2 files changed, 102 insertions, 0 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
+};