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

49
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();
@ -67,15 +68,13 @@ function EPub(fname, imageroot, linkroot) {
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,7 +86,8 @@ EPub.prototype.parse = function () {
this.toc = [];
this.open();
};
}
/**
* EPub#open() -> undefined
@ -95,7 +95,7 @@ EPub.prototype.parse = function () {
* 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) {
@ -117,7 +117,7 @@ EPub.prototype.open = function () {
* 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++) {
@ -153,7 +153,7 @@ EPub.prototype.checkMimeType = function () {
* 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") {
@ -242,7 +242,7 @@ EPub.prototype.getRootFiles = function () {
*
* Parses the rootfile XML and calls rootfile parser
**/
EPub.prototype.handleRootFile = function () {
handleRootFile() {
this.zip.readFile(this.rootFile, (function (err, data) {
if (err) {
@ -270,7 +270,7 @@ EPub.prototype.handleRootFile = function () {
* 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';
@ -307,7 +307,7 @@ EPub.prototype.parseRootFile = function (rootfile) {
*
* 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);
@ -408,7 +408,7 @@ EPub.prototype.parseMetadata = function (metadata) {
*
* 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("/");
@ -434,7 +434,7 @@ EPub.prototype.parseManifest = function (manifest) {
*
* 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();
@ -462,7 +462,7 @@ EPub.prototype.parseSpine = function (spine) {
*
* 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();
@ -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
@ -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);
@ -673,7 +673,7 @@ EPub.prototype.getChapter = function (id, callback) {
*
* 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")) {
@ -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/") {
@ -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) {
@ -745,12 +745,12 @@ EPub.prototype.getFile = function (id, callback) {
};
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) {
@ -764,6 +764,7 @@ EPub.prototype.readFile = function(filename, options, callback_) {
}
};
}
// Expose to the world
module.exports = EPub;