summaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
authorSamy Pessé <samypesse@gmail.com>2014-04-27 17:14:57 +0200
committerSamy Pessé <samypesse@gmail.com>2014-04-27 17:14:57 +0200
commit6a33ad9ef7a73faaeb8faa839b2004fbcd502ec7 (patch)
tree892bd90740b5d7e1b252de2fd3145746bbee7171 /lib
parentca94055adf2d0519105b587116d5d56b559a2c3a (diff)
downloadgitbook-6a33ad9ef7a73faaeb8faa839b2004fbcd502ec7.zip
gitbook-6a33ad9ef7a73faaeb8faa839b2004fbcd502ec7.tar.gz
gitbook-6a33ad9ef7a73faaeb8faa839b2004fbcd502ec7.tar.bz2
Add base cache manifest
Diffstat (limited to 'lib')
-rw-r--r--lib/generate/fs.js1
-rw-r--r--lib/generate/manifest.js72
-rw-r--r--lib/generate/site/index.js32
3 files changed, 103 insertions, 2 deletions
diff --git a/lib/generate/fs.js b/lib/generate/fs.js
index f96efb3..12252e3 100644
--- a/lib/generate/fs.js
+++ b/lib/generate/fs.js
@@ -18,6 +18,7 @@ var getFiles = function(path) {
ig.addIgnoreRules([
'.git/',
'.gitignore',
+ '.DS_Store'
], '__custom_stuff');
// Push each file to our list
diff --git a/lib/generate/manifest.js b/lib/generate/manifest.js
new file mode 100644
index 0000000..8bb826a
--- /dev/null
+++ b/lib/generate/manifest.js
@@ -0,0 +1,72 @@
+var _ = require('lodash');
+var path = require('path');
+var Q = require('q');
+
+var fs = require("./fs");
+
+var extsToIgnore = [".gz"]
+
+var Manifest = function() {
+ this.revision = 0;
+ this.clear(Date.now());
+};
+
+// Regenerate manifest
+Manifest.prototype.clear = function(revision) {
+ if (revision) this.revision = revision;
+ this.sections = {
+ 'CACHE': {},
+ 'NETWORK': {},
+ 'FALLBACK': {}
+ };
+ return Q(this);
+};
+
+// Add a resource
+Manifest.prototype.add = function(category, resource, value) {
+ if (_.isArray(resource)) {
+ _.each(resource, function(subres) {
+ this.add(category, subres, value);
+ }, this);
+ return;
+ }
+ this.sections[category][resource] = value;
+};
+
+// Add a directory in cache
+Manifest.prototype.addFolder = function(folder, root, except) {
+ var that = this;
+ root = root || "/";
+
+ return fs.list(folder)
+ .then(function(files) {
+ _.each(
+ // Ignore diretcories
+ _.filter(files, function(file) {
+ return file.substr(-1) != "/" && !_.contains(except, path.join(root, file)) && !_.contains(extsToIgnore, path.extname(file));
+ }),
+ function(file) {
+ that.add("CACHE", path.join(root, file));
+ }
+ );
+ })
+};
+
+// Get manifest content
+Manifest.prototype.dump = function() {
+ var lines = [
+ "CACHE MANIFEST",
+ "# Revision "+this.revision
+ ];
+
+ _.each(this.sections, function(content, section) {
+ if (_.size(content) == 0) return;
+ lines.push("");
+ lines.push(section+":");
+ lines = lines.concat(_.keys(content));
+ }, this);
+
+ return Q(lines.join("\n"));
+};
+
+module.exports = Manifest;
diff --git a/lib/generate/site/index.js b/lib/generate/site/index.js
index 68328e7..f8986df 100644
--- a/lib/generate/site/index.js
+++ b/lib/generate/site/index.js
@@ -9,6 +9,7 @@ var parse = require("../../parse");
var BaseGenerator = require("../generator");
var indexer = require('./search_indexer');
+var Manifest = require('../manifest');
// Swig filter for returning the count of lines in a code section
swig.setFilter('lines', function(content) {
@@ -30,6 +31,10 @@ var Generator = function() {
this.revision = Date.now();
this.indexer = indexer();
+ this.manifest = new Manifest(Date.now());
+ this.manifest.add("NETWORK", [
+ '*'
+ ]);
};
util.inherits(Generator, BaseGenerator);
@@ -119,6 +124,8 @@ Generator.prototype.convertFile = function(content, _input) {
});
})
.then(function(sections) {
+ that.manifest.add("CACHE", _output);
+
return that._writeTemplate(that.template, {
progress: progress,
@@ -157,11 +164,22 @@ Generator.prototype.copyAssets = function() {
path.join(that.options.theme, "assets"),
path.join(that.options.output, "gitbook")
)
+
+ // Add to cach manifest
+ .then(function() {
+ return that.manifest.addFolder(path.join(that.options.output, "gitbook"), "gitbook");
+ })
+
// Copy plugins assets
.then(function() {
return Q.all(
_.map(that.plugins.list, function(plugin) {
- return plugin.copyAssets(path.join(that.options.output, "gitbook/plugins/", plugin.name))
+ var pluginAssets = path.join(that.options.output, "gitbook/plugins/", plugin.name);
+
+ return plugin.copyAssets(pluginAssets)
+ .then(function() {
+ return that.manifest.addFolder(pluginAssets, "gitbook/plugins/"+plugin.name);
+ });
})
);
})
@@ -175,9 +193,19 @@ Generator.prototype.writeSearchIndex = function() {
);
};
+
+// Add cache manifest
+Generator.prototype.writeCacheManifest = function() {
+ return fs.writeFile(
+ path.join(this.options.output, 'manifest.appcache'),
+ this.manifest.dump()
+ );
+};
+
Generator.prototype.finish = function() {
return this.copyAssets()
- .then(this.writeSearchIndex);
+ .then(this.writeSearchIndex)
+ .then(this.writeCacheManifest);
};
module.exports = Generator;