summaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
Diffstat (limited to 'lib')
-rw-r--r--lib/book.js8
-rw-r--r--lib/configuration.js4
-rw-r--r--lib/generator.js5
-rw-r--r--lib/generators/ebook.js9
-rw-r--r--lib/generators/json.js24
-rw-r--r--lib/generators/website.js9
-rw-r--r--lib/template.js35
-rw-r--r--lib/utils/batch.js52
-rw-r--r--lib/utils/i18n.js3
-rw-r--r--lib/utils/images.js6
-rw-r--r--lib/utils/page.js63
11 files changed, 151 insertions, 67 deletions
diff --git a/lib/book.js b/lib/book.js
index f5d52e9..650931f 100644
--- a/lib/book.js
+++ b/lib/book.js
@@ -261,9 +261,6 @@ Book.prototype.generateMultiLingual = function(generator) {
return book.generate(that.options.generator);
});
}, Q());
- })
- .then(function() {
- return generator.langsIndex(that.langs);
});
};
@@ -651,6 +648,11 @@ Book.prototype.isSubBook = function() {
return !!this.parent;
};
+// Test if the file is the entry point
+Book.prototype.isEntryPoint = function(fp) {
+ return fp == this.readmeFile;
+};
+
// Resolve a path in book
Book.prototype.resolve = function(p) {
return path.resolve(this.root, p);
diff --git a/lib/configuration.js b/lib/configuration.js
index bedf266..7316dd8 100644
--- a/lib/configuration.js
+++ b/lib/configuration.js
@@ -220,8 +220,8 @@ Configuration.DEFAULT = {
"margin": {
"right": 62,
"left": 62,
- "top": 36,
- "bottom": 36
+ "top": 56,
+ "bottom": 56
},
//Header HTML template. Available variables: _PAGENUM_, _TITLE_, _AUTHOR_ and _SECTION_.
diff --git a/lib/generator.js b/lib/generator.js
index f7f319d..c809de2 100644
--- a/lib/generator.js
+++ b/lib/generator.js
@@ -70,11 +70,6 @@ BaseGenerator.prototype.copyCover = function() {
});
};
-// Generate the langs index
-BaseGenerator.prototype.langsIndex = function(langs) {
- return Q.reject(new Error("Langs index is not supported in this generator"));
-};
-
// At teh end of the generation
BaseGenerator.prototype.finish = function() {
return Q.reject(new Error("Could not finish generation"));
diff --git a/lib/generators/ebook.js b/lib/generators/ebook.js
index eb84df8..09215c4 100644
--- a/lib/generators/ebook.js
+++ b/lib/generators/ebook.js
@@ -41,11 +41,6 @@ Generator.prototype.writeSummary = function() {
return this._writeTemplate(this.templates["summary"], {}, path.join(this.options.output, "SUMMARY.html"));
};
-
-Generator.prototype.langsIndex = function(langs) {
- return Q();
-};
-
Generator.prototype.finish = function() {
var that = this;
@@ -92,8 +87,8 @@ Generator.prototype.finish = function() {
"--pdf-mono-font-size": String(pdfOptions.fontSize),
"--paper-size": String(pdfOptions.paperSize),
"--pdf-page-numbers": Boolean(pdfOptions.pageNumbers),
- "--pdf-header-template": String(pdfOptions.headerTemplate),
- "--pdf-footer-template": String(pdfOptions.footerTemplate)
+ "--pdf-header-template": String(pdfOptions.headerTemplate) || "<p class='header'><span>"+that.options.title+"</span></p>",
+ "--pdf-footer-template": String(pdfOptions.footerTemplate) || "<p class='footer'><span>_SECTION_</span> <span style='float:right;'>_PAGENUM_</span></p>"
});
}
diff --git a/lib/generators/json.js b/lib/generators/json.js
index f1af395..6c9439d 100644
--- a/lib/generators/json.js
+++ b/lib/generators/json.js
@@ -14,7 +14,6 @@ util.inherits(Generator, BaseGenerator);
// Ignore some methods
Generator.prototype.transferFile = function(input) { };
-Generator.prototype.finish = function() { };
// Convert an input file
Generator.prototype.convertFile = function(input) {
@@ -37,21 +36,26 @@ Generator.prototype.convertFile = function(input) {
});
};
-// Generate languages index
-// Contains the first languages readme and langs infos
-Generator.prototype.langsIndex = function(langs) {
- var that = this;
-
- if (langs.length == 0) return Q.reject("Need at least one language");
+// Finish generation
+Generator.prototype.finish = function() {
+ return this.writeReadme();
+};
- var mainLang = _.first(langs).lang;
- var readme = links.changeExtension(that.book.readmeFile, ".json");
+// Write README.json
+Generator.prototype.writeReadme = function() {
+ var that = this;
+ var mainlang, langs;
return Q()
.then(function() {
+ langs = that.book.langs;
+ mainLang = langs.length > 0? _.first(langs).lang : null;
+
+ readme = links.changeExtension(that.book.readmeFile, ".json");
+
// Read readme from main language
return fs.readFile(
- path.join(that.options.output, mainLang, readme)
+ mainLang? path.join(that.options.output, mainLang, readme) : path.join(that.options.output, readme)
);
})
.then(function(content) {
diff --git a/lib/generators/website.js b/lib/generators/website.js
index 01df98c..d40ffa8 100644
--- a/lib/generators/website.js
+++ b/lib/generators/website.js
@@ -118,7 +118,8 @@ Generator.prototype.finish = function() {
return this.copyAssets()
.then(this.copyCover)
.then(this.writeGlossary)
- .then(this.writeSearchIndex);
+ .then(this.writeSearchIndex)
+ .then(this.writeLangsIndex)
};
// Convert an input file
@@ -156,11 +157,11 @@ Generator.prototype.convertFile = function(input) {
};
// Write the index for langs
-Generator.prototype.langsIndex = function(langs) {
+Generator.prototype.writeLangsIndex = function() {
var that = this;
-
+ if (!this.book.langs.length) return Q();
return this._writeTemplate(this.templates["langs"], {
- langs: langs
+ langs: this.book.langs
}, path.join(this.options.output, "index.html"));
};
diff --git a/lib/template.js b/lib/template.js
index f1d9fc0..f43073b 100644
--- a/lib/template.js
+++ b/lib/template.js
@@ -6,6 +6,7 @@ var nunjucks = require("nunjucks");
var git = require("./utils/git");
var stringUtils = require("./utils/string");
var fs = require("./utils/fs");
+var batch = require("./utils/batch");
var pkg = require("../package.json");
@@ -87,18 +88,21 @@ TemplateEngine.prototype.processBlock = function(blk) {
if (_.isString(blk)) blk = { body: blk };
blk = _.defaults(blk, {
- parse: false
+ parse: false,
+ post: undefined
});
blk.id = _.uniqueId("blk");
+ var toAdd = (!blk.parse) || (blk.post != undefined);
+
+ // Add to global map
+ if (toAdd) this.blocks[blk.id] = blk;
+
//Parsable block, just return it
if (blk.parse) {
return blk.body;
}
- // Add to global map
- this.blocks[blk.id] = blk;
-
// Return it as a macro
return "%+%"+blk.id+"%+%";
};
@@ -114,9 +118,6 @@ TemplateEngine.prototype.replaceBlocks = function(content) {
var body = blk.body;
- // Remove it from map
- delete that.blocks[key];
-
return body;
});
};
@@ -367,8 +368,26 @@ TemplateEngine.prototype.renderPage = function(page) {
// Post process content
TemplateEngine.prototype.postProcess = function(content) {
+ var that = this;
+
return Q(content)
- .then(this.replaceBlocks);
+ .then(that.replaceBlocks)
+ .then(function(_content) {
+ return batch.execEach(that.blocks, {
+ max: 20,
+ fn: function(blk, blkId) {
+ return Q()
+ .then(function() {
+ if (!blk.post) return Q();
+ return blk.post();
+ })
+ .then(function() {
+ delete that.blocks[blkId];
+ });
+ }
+ })
+ .thenResolve(_content);
+ });
};
module.exports = TemplateEngine;
diff --git a/lib/utils/batch.js b/lib/utils/batch.js
new file mode 100644
index 0000000..bd3b80f
--- /dev/null
+++ b/lib/utils/batch.js
@@ -0,0 +1,52 @@
+var Q = require("q");
+var _ = require("lodash");
+
+// Execute a method for all element
+function execEach(items, options) {
+ if (_.size(items) == 0) return Q();
+ var concurrents = 0, d = Q.defer(), pending = [];
+
+ options = _.defaults(options || {}, {
+ max: 100,
+ fn: function(item) {}
+ });
+
+
+ function startItem(item, i) {
+ if (concurrents >= options.max) {
+ pending.push([item, i]);
+ return;
+ }
+
+ concurrents++;
+ Q()
+ .then(function() {
+ return options.fn(item, i);
+ })
+ .then(function() {
+ concurrents--;
+
+ // Next pending
+ var next = pending.shift();
+
+ if (concurrents == 0 && !next) {
+ d.resolve();
+ } else if (next) {
+ startItem.apply(null, next);
+ }
+ })
+ .fail(function(err) {
+ pending = [];
+ d.reject(err);
+ })
+ }
+
+ _.each(items, startItem);
+
+ return d.promise;
+}
+
+module.exports = {
+ execEach: execEach
+};
+
diff --git a/lib/utils/i18n.js b/lib/utils/i18n.js
index 3ef1557..dcd9150 100644
--- a/lib/utils/i18n.js
+++ b/lib/utils/i18n.js
@@ -48,6 +48,9 @@ var normalizeLanguage = _.memoize(function(lang) {
score: compareLocales(lang, locale)
}
})
+ .filter(function(lang) {
+ return lang.score > 0;
+ })
.sortBy("score")
.pluck("locale")
.last()
diff --git a/lib/utils/images.js b/lib/utils/images.js
index 61a52dc..1e90317 100644
--- a/lib/utils/images.js
+++ b/lib/utils/images.js
@@ -22,7 +22,11 @@ var convertSVG = function(source, dest, options) {
if (error.code == 127) error = new Error("Need to install 'svgexport' using 'npm install svgexport -g'");
return d.reject(error);
}
- d.resolve();
+ if (fs.existsSync(dest)) {
+ d.resolve();
+ } else {
+ d.reject(new Error("Error converting "+source));
+ }
});
return d.promise;
diff --git a/lib/utils/page.js b/lib/utils/page.js
index ccf5dfa..0714958 100644
--- a/lib/utils/page.js
+++ b/lib/utils/page.js
@@ -4,10 +4,12 @@ var path = require('path');
var cheerio = require('cheerio');
var domSerializer = require('dom-serializer');
var request = require('request');
+var crc = require("crc");
var links = require('./links');
var imgUtils = require('./images');
var fs = require('./fs');
+var batch = require('./batch');
// Render a cheerio dom as html
var renderDom = function($, dom, options) {
@@ -111,6 +113,7 @@ function normalizeHtml(src, options) {
$("img").each(function() {
var origin = undefined;
var src = $(this).attr("src");
+ if (!src) return;
var isExternal = links.isExternal(src);
// Transform as relative to the bases
@@ -123,7 +126,7 @@ function normalizeHtml(src, options) {
// If image is external and ebook, then downlaod the images
if (isExternal) {
origin = src;
- src = "/"+fs.getUniqueFilename(outputRoot, path.basename(src));
+ src = "/"+crc.crc32(origin).toString(16)+path.extname(origin);
src = links.toAbsolute(src, options.base, options.output);
isExternal = false;
}
@@ -181,6 +184,7 @@ function normalizeHtml(src, options) {
$("a").each(function() {
var href = $(this).attr("href");
+ if (!href) return;
if (links.isRelative(href)) {
var absolutePath = path.join(options.base, href);
@@ -226,34 +230,39 @@ function normalizeHtml(src, options) {
function convertImages(images, options) {
if (!options.convertImages) return Q();
+ var downloaded = [];
options.book.log.debug.ln("convert ", images.length, "images to png");
- return _.reduce(images, function(prev, image) {
- var imgin = path.resolve(options.book.options.output, image.source);
-
- return prev
-
- // Write image if need to be download
- .then(function() {
- if (!image.origin) return;
- options.book.log.debug("download image", image.origin, "...");
- return options.book.log.debug.promise(fs.writeStream(imgin, request(image.origin)));
- })
-
- // Write svg if content
- .then(function() {
- if (!image.content) return;
- return fs.writeFile(imgin, image.content);
- })
-
- // Convert
- .then(function(){
- if (!image.dest) return;
- var imgout = path.resolve(options.book.options.output, image.dest);
- options.book.log.debug("convert image", image.source, "to", image.dest, "...");
- return options.book.log.debug.promise(imgUtils.convertSVG(imgin, imgout));
- });
- }, Q())
+ return batch.execEach(images, {
+ max: 100,
+ fn: function(image) {
+ var imgin = path.resolve(options.book.options.output, image.source);
+
+ return Q()
+
+ // Write image if need to be download
+ .then(function() {
+ if (!image.origin && !_.contains(downloaded, image.origin)) return;
+ options.book.log.debug("download image", image.origin, "...");
+ downloaded.push(image.origin);
+ return options.book.log.debug.promise(fs.writeStream(imgin, request(image.origin)));
+ })
+
+ // Write svg if content
+ .then(function() {
+ if (!image.content) return;
+ return fs.writeFile(imgin, image.content);
+ })
+
+ // Convert
+ .then(function() {
+ if (!image.dest) return;
+ var imgout = path.resolve(options.book.options.output, image.dest);
+ options.book.log.debug("convert image", image.source, "to", image.dest, "...");
+ return options.book.log.debug.promise(imgUtils.convertSVG(imgin, imgout));
+ });
+ }
+ })
.then(function() {
options.book.log.debug.ok(images.length+" images converted with success");
});