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