Comply with es6 and remove dependency of utils

This commit is contained in:
Logan Stucki 2018-08-27 23:01:10 +00:00 committed by Julien Chaumond
parent 3730d37743
commit 446bf3d5d8

111
epub.js
View File

@ -1,6 +1,5 @@
var xml2js = require('xml2js');
var xml2jsOptions = xml2js.defaults['0.1'];
var util = require('util');
var EventEmitter = require('events').EventEmitter;
try {
@ -53,8 +52,10 @@ try {
*
* /images/logo_img/OPT/logo.jpg
**/
function EPub(fname, imageroot, linkroot) {
EventEmitter.call(this);
class EPub extends EventEmitter {
constructor(fname, imageroot, linkroot) {
super();
this.filename = fname;
this.imageroot = (imageroot || "/images/").trim();
@ -66,16 +67,14 @@ function EPub(fname, imageroot, linkroot) {
if (this.linkroot.substr(-1) != "/") {
this.linkroot += "/";
}
}
util.inherits(EPub, EventEmitter);
}
/**
/**
* EPub#parse() -> undefined
*
* Starts the parser, needs to be called by the script
**/
EPub.prototype.parse = function () {
parse() {
this.containerFile = false;
this.mimeFile = false;
this.rootFile = false;
@ -87,15 +86,16 @@ EPub.prototype.parse = function () {
this.toc = [];
this.open();
};
}
/**
/**
* EPub#open() -> undefined
*
* Opens the epub file with Zip unpacker, retrieves file listing
* and runs mime type check
**/
EPub.prototype.open = function () {
open() {
try {
this.zip = new ZipFile(this.filename);
} catch (E) {
@ -109,15 +109,15 @@ EPub.prototype.open = function () {
}
this.checkMimeType();
};
};
/**
/**
* EPub#checkMimeType() -> undefined
*
* Checks if there's a file called "mimetype" and that it's contents
* are "application/epub+zip". On success runs root file check.
**/
EPub.prototype.checkMimeType = function () {
checkMimeType() {
var i, len;
for (i = 0, len = this.zip.names.length; i < len; i++) {
@ -144,16 +144,16 @@ EPub.prototype.checkMimeType = function () {
this.getRootFiles();
}).bind(this));
};
};
/**
/**
* EPub#getRootFiles() -> undefined
*
* Looks for a "meta-inf/container.xml" file and searches for a
* rootfile element with mime type "application/oebps-package+xml".
* On success calls the rootfile parser
**/
EPub.prototype.getRootFiles = function () {
getRootFiles() {
var i, len;
for (i = 0, len = this.zip.names.length; i < len; i++) {
if (this.zip.names[i].toLowerCase() == "meta-inf/container.xml") {
@ -235,14 +235,14 @@ EPub.prototype.getRootFiles = function () {
}).bind(this));
};
};
/**
/**
* EPub#handleRootFile() -> undefined
*
* Parses the rootfile XML and calls rootfile parser
**/
EPub.prototype.handleRootFile = function () {
handleRootFile() {
this.zip.readFile(this.rootFile, (function (err, data) {
if (err) {
@ -262,15 +262,15 @@ EPub.prototype.handleRootFile = function () {
xmlparser.parseString(xml);
}).bind(this));
};
};
/**
/**
* EPub#parseRootFile() -> undefined
*
* Parses elements "metadata," "manifest," "spine" and TOC.
* Emits "end" if no TOC
**/
EPub.prototype.parseRootFile = function (rootfile) {
parseRootFile(rootfile) {
this.version = rootfile['@'].version || '2.0';
@ -300,14 +300,14 @@ EPub.prototype.parseRootFile = function (rootfile) {
} else {
this.emit("end");
}
};
};
/**
/**
* EPub#parseMetadata() -> undefined
*
* Parses "metadata" block (book metadata, title, author etc.)
**/
EPub.prototype.parseMetadata = function (metadata) {
parseMetadata(metadata) {
var i, j, len, keys, keyparts, key;
keys = Object.keys(metadata);
@ -401,14 +401,14 @@ EPub.prototype.parseMetadata = function (metadata) {
this.metadata[meta.name] = meta.content;
}
}, this);
};
};
/**
/**
* EPub#parseManifest() -> undefined
*
* Parses "manifest" block (all items included, html files, images, styles)
**/
EPub.prototype.parseManifest = function (manifest) {
parseManifest(manifest) {
var i, len, path = this.rootFile.split("/"), element, path_str;
path.pop();
path_str = path.join("/");
@ -427,14 +427,14 @@ EPub.prototype.parseManifest = function (manifest) {
}
}
}
};
};
/**
/**
* EPub#parseSpine() -> undefined
*
* Parses "spine" block (all html elements that are shown to the reader)
**/
EPub.prototype.parseSpine = function (spine) {
parseSpine(spine) {
var i, len, path = this.rootFile.split("/"), element;
path.pop();
@ -455,14 +455,14 @@ EPub.prototype.parseSpine = function (spine) {
}
}
this.flow = this.spine.contents;
};
};
/**
/**
* EPub#parseTOC() -> undefined
*
* Parses ncx file for table of contents (title, html file)
**/
EPub.prototype.parseTOC = function () {
parseTOC() {
var i, len, path = this.spine.toc.href.split("/"), id_list = {}, keys;
path.pop();
@ -495,9 +495,9 @@ EPub.prototype.parseTOC = function () {
xmlparser.parseString(xml);
}).bind(this));
};
};
/**
/**
* EPub#walkNavMap(branch, path, id_list,[, level]) -> Array
* - branch (Array | Object): NCX NavPoint object
* - path (Array): Base path
@ -507,7 +507,7 @@ EPub.prototype.parseTOC = function () {
* Walks the NavMap object through all levels and finds elements
* for TOC
**/
EPub.prototype.walkNavMap = function (branch, path, id_list, level) {
walkNavMap(branch, path, id_list, level) {
level = level || 0;
// don't go too far
@ -568,9 +568,9 @@ EPub.prototype.walkNavMap = function (branch, path, id_list, level) {
}
}
return output;
};
};
/**
/**
* EPub#getChapter(id, callback) -> undefined
* - id (String): Manifest id value for a chapter
* - callback (Function): callback function
@ -578,7 +578,7 @@ EPub.prototype.walkNavMap = function (branch, path, id_list, level) {
* Finds a chapter text for an id. Replaces image and link URL's, removes
* <head> etc. elements. Return only chapters with mime type application/xhtml+xml
**/
EPub.prototype.getChapter = function (id, callback) {
getChapter(id, callback) {
this.getChapterRaw(id, (function (err, str) {
if (err) {
callback(err);
@ -663,17 +663,17 @@ EPub.prototype.getChapter = function (id, callback) {
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) {
getChapterRaw(id, callback) {
if (this.manifest[id]) {
if (!(this.manifest[id]['media-type'] == "application/xhtml+xml" || this.manifest[id]['media-type'] == "image/svg+xml")) {
@ -694,10 +694,10 @@ EPub.prototype.getChapterRaw = function (id, callback) {
} else {
callback(new Error("File not found"));
}
};
};
/**
/**
* EPub#getImage(id, callback) -> undefined
* - id (String): Manifest id value for an image
* - callback (Function): callback function
@ -706,7 +706,7 @@ EPub.prototype.getChapterRaw = function (id, callback) {
* an error object, image buffer and image content-type.
* Return only images with mime type image
**/
EPub.prototype.getImage = function (id, callback) {
getImage(id, callback) {
if (this.manifest[id]) {
if ((this.manifest[id]['media-type'] || "").toLowerCase().trim().substr(0, 6) != "image/") {
@ -717,10 +717,10 @@ EPub.prototype.getImage = function (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
@ -728,7 +728,7 @@ EPub.prototype.getImage = function (id, callback) {
* 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) {
getFile(id, callback) {
if (this.manifest[id]) {
this.zip.readFile(this.manifest[id].href, (function (err, data) {
@ -742,15 +742,15 @@ EPub.prototype.getFile = function (id, callback) {
} else {
callback(new Error("File not found"));
}
};
};
EPub.prototype.readFile = function(filename, options, callback_) {
readFile(filename, options, callback_) {
var callback = arguments[arguments.length - 1];
if (util.isFunction(options) || !options) {
if (typeof options === 'function' || !options) {
this.zip.readFile(filename, callback);
} else if (util.isString(options)) {
} else if (typeof options === 'string') {
// options is an encoding
this.zip.readFile(filename, function(err, data) {
if (err) {
@ -762,8 +762,9 @@ EPub.prototype.readFile = function(filename, options, callback_) {
} else {
throw new TypeError('Bad arguments');
}
};
};
}
// Expose to the world
module.exports = EPub;