Add ability to get any file from the Epub + access raw HTML content
This commit is contained in:
parent
18f50d4ba2
commit
7386d4ff9a
68
epub.js
68
epub.js
@ -216,7 +216,7 @@ EPub.prototype.getRootFiles = function () {
|
|||||||
/**
|
/**
|
||||||
* EPub#handleRootFile() -> undefined
|
* EPub#handleRootFile() -> undefined
|
||||||
*
|
*
|
||||||
* Parser the rootfile XML and calls rootfile parser
|
* Parses the rootfile XML and calls rootfile parser
|
||||||
**/
|
**/
|
||||||
EPub.prototype.handleRootFile = function () {
|
EPub.prototype.handleRootFile = function () {
|
||||||
|
|
||||||
@ -527,22 +527,14 @@ EPub.prototype.walkNavMap = function (branch, path, id_list, level) {
|
|||||||
* <head> etc. elements. Return only chapters with mime type application/xhtml+xml
|
* <head> etc. elements. Return only chapters with mime type application/xhtml+xml
|
||||||
**/
|
**/
|
||||||
EPub.prototype.getChapter = function (id, callback) {
|
EPub.prototype.getChapter = function (id, callback) {
|
||||||
var i, len, path = this.rootFile.split("/"), keys = Object.keys(this.manifest);
|
this.getChapterRaw(id, (function (err, str) {
|
||||||
path.pop();
|
|
||||||
|
|
||||||
if (this.manifest[id]) {
|
|
||||||
|
|
||||||
if ((this.manifest[id]['media-type'] || "").toLowerCase().trim() != "application/xhtml+xml") {
|
|
||||||
return callback(new Error("Inavlid mime type for chapter"));
|
|
||||||
}
|
|
||||||
|
|
||||||
this.zip.readFile(this.manifest[id].href, (function (err, data) {
|
|
||||||
if (err) {
|
if (err) {
|
||||||
callback(new Error("Reading archive failed"));
|
callback(err);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
var str = data.toString("utf-8");
|
var i, len, path = this.rootFile.split("/"), keys = Object.keys(this.manifest);
|
||||||
|
path.pop();
|
||||||
|
|
||||||
// remove linebreaks (no multi line matches in JS regex!)
|
// remove linebreaks (no multi line matches in JS regex!)
|
||||||
str = str.replace(/\r?\n/g, "\u0000");
|
str = str.replace(/\r?\n/g, "\u0000");
|
||||||
@ -617,6 +609,33 @@ EPub.prototype.getChapter = function (id, callback) {
|
|||||||
// bring back linebreaks
|
// bring back linebreaks
|
||||||
str = str.replace(/\u0000/g, "\n").trim();
|
str = str.replace(/\u0000/g, "\n").trim();
|
||||||
|
|
||||||
|
callback(null, str);
|
||||||
|
}).bind(this));
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* EPub#getChapterRaw(id, callback) -> undefined
|
||||||
|
* - id (String): Manifest id value for a chapter
|
||||||
|
* - callback (Function): callback function
|
||||||
|
*
|
||||||
|
* Returns the raw chapter text for an id.
|
||||||
|
**/
|
||||||
|
EPub.prototype.getChapterRaw = function (id, callback) {
|
||||||
|
if (this.manifest[id]) {
|
||||||
|
|
||||||
|
if ((this.manifest[id]['media-type'] || "").toLowerCase().trim() != "application/xhtml+xml") {
|
||||||
|
return callback(new Error("Invalid mime type for chapter"));
|
||||||
|
}
|
||||||
|
|
||||||
|
this.zip.readFile(this.manifest[id].href, (function (err, data) {
|
||||||
|
if (err) {
|
||||||
|
callback(new Error("Reading archive failed"));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
var str = data.toString("utf-8");
|
||||||
|
|
||||||
callback(null, str);
|
callback(null, str);
|
||||||
|
|
||||||
}).bind(this));
|
}).bind(this));
|
||||||
@ -631,7 +650,7 @@ EPub.prototype.getChapter = function (id, callback) {
|
|||||||
* - id (String): Manifest id value for an image
|
* - id (String): Manifest id value for an image
|
||||||
* - callback (Function): callback function
|
* - callback (Function): callback function
|
||||||
*
|
*
|
||||||
* Finds an image an id. Returns the image as Buffer. Callback gets
|
* Finds an image for an id. Returns the image as Buffer. Callback gets
|
||||||
* an error object, image buffer and image content-type.
|
* an error object, image buffer and image content-type.
|
||||||
* Return only images with mime type image
|
* Return only images with mime type image
|
||||||
**/
|
**/
|
||||||
@ -639,9 +658,27 @@ EPub.prototype.getImage = function (id, callback) {
|
|||||||
if (this.manifest[id]) {
|
if (this.manifest[id]) {
|
||||||
|
|
||||||
if ((this.manifest[id]['media-type'] || "").toLowerCase().trim().substr(0, 6) != "image/") {
|
if ((this.manifest[id]['media-type'] || "").toLowerCase().trim().substr(0, 6) != "image/") {
|
||||||
return callback(new Error("Inavlid mime type for image"));
|
return callback(new Error("Invalid mime type for image"));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
this.getFile(id, callback);
|
||||||
|
} else {
|
||||||
|
callback(new Error("File not found"));
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* EPub#getFile(id, callback) -> undefined
|
||||||
|
* - id (String): Manifest id value for a file
|
||||||
|
* - callback (Function): callback function
|
||||||
|
*
|
||||||
|
* Finds a file for an id. Returns the file as Buffer. Callback gets
|
||||||
|
* an error object, file contents buffer and file content-type.
|
||||||
|
**/
|
||||||
|
EPub.prototype.getFile = function (id, callback) {
|
||||||
|
if (this.manifest[id]) {
|
||||||
|
|
||||||
this.zip.readFile(this.manifest[id].href, (function (err, data) {
|
this.zip.readFile(this.manifest[id].href, (function (err, data) {
|
||||||
if (err) {
|
if (err) {
|
||||||
callback(new Error("Reading archive failed"));
|
callback(new Error("Reading archive failed"));
|
||||||
@ -655,5 +692,6 @@ EPub.prototype.getImage = function (id, callback) {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
// Expose to the world
|
// Expose to the world
|
||||||
module.exports = EPub;
|
module.exports = EPub;
|
Reference in New Issue
Block a user