summaryrefslogtreecommitdiffstats
path: root/lib/book.js
blob: 9c489f16b3fb54a04f49e28680fe9171f87e262f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
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
	this.root = root;

	// Configuration
	this.config = new Configuration(this);
	Object.defineProperty(this, "options", {
		get: function () {
			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;