summaryrefslogtreecommitdiffstats
path: root/lib/book.js
diff options
context:
space:
mode:
Diffstat (limited to 'lib/book.js')
-rw-r--r--lib/book.js113
1 files changed, 103 insertions, 10 deletions
diff --git a/lib/book.js b/lib/book.js
index a2bd40b..2761022 100644
--- a/lib/book.js
+++ b/lib/book.js
@@ -64,8 +64,11 @@ var Book = function(root, context, parent) {
// List of plugins
this.plugins = [];
- // Readme file
- this.readmeFile = "README.md";
+ // Structure files
+ this.summaryFile = null;
+ this.glossaryFile = null;
+ this.readmeFile = null;
+ this.langsFile = null;
// Search Index
this.searchIndex = lunr(function () {
@@ -319,6 +322,7 @@ Book.prototype.parseReadme = function() {
if (!_.contains(that.files, readme.path)) throw "README file is ignored";
that.readmeFile = readme.path;
+ that._defaultsStructure(that.readmeFile);
that.logDebug("readme located at", that.readmeFile);
return that.template.renderFile(that.readmeFile)
@@ -344,8 +348,11 @@ Book.prototype.parseLangs = function() {
.then(function(langs) {
if (!langs) return [];
- that.logDebug("languages index located at", langs.path);
- return that.template.renderFile(langs.path)
+ that.langsFile = langs.path;
+ that._defaultsStructure(that.langsFile);
+
+ that.logDebug("languages index located at", that.langsFile);
+ return that.template.renderFile(that.langsFile)
.then(function(content) {
return langs.parser.langs(content);
});
@@ -367,10 +374,12 @@ Book.prototype.parseSummary = function() {
if (!summary) throw "No SUMMARY file";
// Remove the summary from the list of files to parse
- that.files = _.without(that.files, summary.path);
+ that.summaryFile = summary.path;
+ that._defaultsStructure(that.summaryFile);
+ that.files = _.without(that.files, that.summaryFile);
- that.logDebug("summary located at", summary.path);
- return that.template.renderFile(summary.path)
+ that.logDebug("summary located at", that.summaryFile);
+ return that.template.renderFile(that.summaryFile)
.then(function(content) {
return summary.parser.summary(content, {
entryPoint: that.readmeFile,
@@ -396,10 +405,12 @@ Book.prototype.parseGlossary = function() {
if (!glossary) return [];
// Remove the glossary from the list of files to parse
- that.files = _.without(that.files, glossary.path);
+ that.glossaryFile = glossary.path;
+ that._defaultsStructure(that.glossaryFile);
+ that.files = _.without(that.files, that.glossaryFile);
- that.logDebug("glossary located at", glossary.path);
- return that.template.renderFile(glossary.path)
+ that.logDebug("glossary located at", that.glossaryFile);
+ return that.template.renderFile(that.glossaryFile)
.then(function(content) {
return glossary.parser.glossary(content);
});
@@ -546,6 +557,17 @@ Book.prototype.indexPage = function(page) {
});
};
+// Default structure paths to an extension
+Book.prototype._defaultsStructure = function(filename) {
+ var that = this;
+ var extension = path.extname(filename);
+
+ that.readmeFile = that.readmeFile || that.config.getStructure("readme")+extension;
+ that.summaryFile = that.summaryFile || that.config.getStructure("summary")+extension;
+ that.glossaryFile = that.glossaryFile || that.config.getStructure("glossary")+extension;
+ that.langsFile = that.langsFile || that.config.getStructure("langs")+extension;
+}
+
// Log message
Book.prototype.log = function(level) {
if (level < this.context.logLevel) return;
@@ -559,4 +581,75 @@ Book.prototype.logInfo = _.partial(Book.prototype.log, Book.LOG_LEVELS.INFO);
Book.prototype.logWarn = _.partial(Book.prototype.log, Book.LOG_LEVELS.WARNING);
Book.prototype.logError = _.partial(Book.prototype.log, Book.LOG_LEVELS.ERROR);
+
+// Init and return a book
+Book.init = function(root) {
+ var book = new Book(root);
+ var extensionToUse = ".md";
+
+ var chaptersPaths = function(chapters) {
+ return _.reduce(chapters || [], function(accu, chapter) {
+ if (!chapter.path) return accu;
+ return accu.concat(
+ _.filter([
+ {
+ title: chapter.title,
+ path: chapter.path
+ }
+ ].concat(chaptersPaths(chapter.articles)))
+ );
+ }, []);
+ };
+
+ book.logInfo("init book at", root);
+ return fs.mkdirp(root)
+ .then(function() {
+ book.logInfo("detect structure from SUMMARY (if it exists)");
+ return book.parseSummary();
+ })
+ .fail(function() {
+ return Q();
+ })
+ .then(function() {
+ var summary = book.summaryFile || "SUMMARY.md";
+ var chapters = book.summary.chapters || [];
+ extensionToUse = path.extname(summary);
+
+ if (chapters.length == 0) {
+ chapters = [
+ {
+ title: "Summary",
+ path: "SUMMARY"+extensionToUse
+ },
+ {
+ title: "Introduction",
+ path: "README"+extensionToUse
+ }
+ ];
+ }
+
+ return Q(chaptersPaths(chapters));
+ })
+ .then(function(chapters) {
+ // Create files that don't exist
+ return Q.all(_.map(chapters, function(chapter) {
+ var absolutePath = path.resolve(book.root, chapter.path);
+
+ return fs.exists(absolutePath)
+ .then(function(exists) {
+ book.logInfo("create", chapter.path);
+ if(exists) return;
+
+ return fs.mkdirp(path.dirname(absolutePath))
+ .then(function() {
+ return fs.writeFile(absolutePath, '# '+chapter.title+'\n');
+ });
+ });
+ }));
+ })
+ .then(function() {
+ book.logInfo("initialization is finished");
+ });
+};
+
module.exports= Book;