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