summaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
authorSamy Pessé <samypesse@gmail.com>2016-01-28 23:42:00 +0100
committerSamy Pessé <samypesse@gmail.com>2016-01-28 23:42:00 +0100
commite83d63c2aa5e30c26ada888990b263e6b786d3f6 (patch)
treeaf460422630bfbaa9016c51d5ec384fa3923d01d /lib
parentff30ba62ba694658d1575b0cf3a0fbf3d5e00d62 (diff)
downloadgitbook-e83d63c2aa5e30c26ada888990b263e6b786d3f6.zip
gitbook-e83d63c2aa5e30c26ada888990b263e6b786d3f6.tar.gz
gitbook-e83d63c2aa5e30c26ada888990b263e6b786d3f6.tar.bz2
Index page of summary when parsing
Diffstat (limited to 'lib')
-rw-r--r--lib/backbone/page.js7
-rw-r--r--lib/backbone/summary.js5
-rw-r--r--lib/book.js14
-rw-r--r--lib/generators/json.js2
-rw-r--r--lib/generators/website.js27
-rw-r--r--lib/output.js14
6 files changed, 63 insertions, 6 deletions
diff --git a/lib/backbone/page.js b/lib/backbone/page.js
index a17e413..bf3494a 100644
--- a/lib/backbone/page.js
+++ b/lib/backbone/page.js
@@ -5,7 +5,7 @@ A page represent a parsable file in the book (Markdown, Asciidoc, etc)
*/
function Page(book, filename) {
- if (!(this instanceof Page)) return new Page();
+ if (!(this instanceof Page)) return new Page(book, filename);
this.book = book;
this.filename = filename;
@@ -20,5 +20,10 @@ Page.prototype.withExtension = function(ext) {
);
};
+// Read the page as a string
+Page.prototype.read = function() {
+ return this.book.readFile(this.filename);
+};
+
module.exports = Page;
diff --git a/lib/backbone/summary.js b/lib/backbone/summary.js
index 43a373a..009fb76 100644
--- a/lib/backbone/summary.js
+++ b/lib/backbone/summary.js
@@ -39,6 +39,11 @@ TOCArticle.prototype.walk = function(iter) {
});
};
+// Return true if is pointing to a file
+TOCArticle.prototype.hasLocation = function() {
+ return Boolean(this.filename);
+};
+
// Return true if has children
TOCArticle.prototype.hasChildren = function() {
return this.articles.length > 0;
diff --git a/lib/book.js b/lib/book.js
index ce368a3..913df2f 100644
--- a/lib/book.js
+++ b/lib/book.js
@@ -168,6 +168,8 @@ Book.prototype.parse = function() {
}
return Promise()
+
+ // Parse the readme
.then(that.readme.load)
.then(function() {
if (that.readme.exists()) return;
@@ -175,13 +177,21 @@ Book.prototype.parse = function() {
throw new Error('No README file (or is ignored)');
})
+ // Parse the summary
.then(that.summary.load)
.then(function() {
- if (that.summary.exists()) return;
+ if (!that.summary.exists()) {
+ throw new Error('No SUMMARY file (or is ignored)');
+ }
- throw new Error('No SUMMARY file (or is ignored)');
+ // Index summary's articles
+ that.summary.walk(function(article) {
+ if (!article.hasLocation()) return;
+ that.addPage(article.filename);
+ });
})
+ // Parse the glossary
.then(that.glossary.load);
});
};
diff --git a/lib/generators/json.js b/lib/generators/json.js
index 19182bc..d2c6954 100644
--- a/lib/generators/json.js
+++ b/lib/generators/json.js
@@ -7,7 +7,7 @@ function JSONGenerator() {
util.inherits(JSONGenerator, Generator);
// Write a page (parsable file)
-Generator.prototype.writePage = function(page) {
+JSONGenerator.prototype.writePage = function(page) {
var json = {};
diff --git a/lib/generators/website.js b/lib/generators/website.js
new file mode 100644
index 0000000..a2c3311
--- /dev/null
+++ b/lib/generators/website.js
@@ -0,0 +1,27 @@
+var util = require('util');
+var Generator = require('./base');
+
+function WebsiteGenerator() {
+ Generator.apply(this, arguments);
+}
+util.inherits(WebsiteGenerator, Generator);
+
+// Copy an asset file
+WebsiteGenerator.prototype.writeAsset = function(filename) {
+ var that = this;
+
+ return that.book.readFile(filename)
+ .then(function(buf) {
+ return that.output.writeFile(filename, buf);
+ });
+};
+
+// Write a page (parsable file)
+WebsiteGenerator.prototype.writePage = function(page) {
+
+};
+
+
+
+
+module.exports = WebsiteGenerator;
diff --git a/lib/output.js b/lib/output.js
index c22e8de..89233da 100644
--- a/lib/output.js
+++ b/lib/output.js
@@ -1,6 +1,9 @@
+var _ = require('lodash');
+var fs = require('fs');
var Ignore = require('ignore');
var Promise = require('./utils/promise');
+var pathUtil = require('./utils/path');
var generators = require('./generators');
var PluginsManager = require('./plugins');
@@ -24,9 +27,16 @@ function Output(book, type) {
]);
}
-// Write a file to the output folder
-Output.prototype.writeFile = function(filename, buf) {
+// Resolve a file in the output directory
+Output.prototype.resolve = function(filename) {
+ return pathUtil.resolveInRoot.apply(null, [this.book.config.get('output')].concat(_.toArray(arguments)));
+};
+
+// Write a file/buffer to the output folder
+Output.prototype.writeFile = function(filename, buf) {
+ filename = this.resolve(filename);
+ return Promise.nfcall(fs.writeFileSync, filename, buf);
};
// Start the generation, for a parsed book