created library

This commit is contained in:
andris9 2011-06-12 22:04:40 +03:00
parent 43311b1868
commit fd06139fcb
2 changed files with 56 additions and 20 deletions

49
epub.js
View File

@ -4,6 +4,11 @@ var ZipFile = require("zipfile").ZipFile,
EventEmitter = require('events').EventEmitter;
//TODO: Cache parsed data to DB
// export
module.exports = EPub;
/**
* new EPub(fname[, imageroot][, linkroot])
* - fname (String): filename for the ebook
@ -13,7 +18,10 @@ var ZipFile = require("zipfile").ZipFile,
* Creates an Event Emitter type object for parsing epub files
*
* var epub = new EPub("book.epub");
* epub.on("end", function(){ ... });
* epub.on("end", function(){
* console.log(epub.spine);
* });
* epub.on("error", function(error){ ... });
* epub.parse();
**/
function EPub(fname, imageroot, linkroot){
@ -458,7 +466,7 @@ EPub.prototype.walkNavMap = function(branch, path, level){
}
}
if(branch[i]["navPoint"]){
output = output.concat(this.walkNavMap(branch[i]["navPoint"], path, level+1));
output = output.concat(this.walkNavMap(branch[i]["navPoint"], path, level + 1));
}
}
return output;
@ -543,23 +551,24 @@ EPub.prototype.getChapter = function(id, callback){
}
}
var epub = new EPub("img.epub", "tere", "vana");
epub.on("error", function(err){
console.log("ERROR\n-----");
throw err;
});
epub.on("end", function(err){
console.log("PARSED\n-----");
console.log(epub.metadata);
console.log(epub.manifest);
console.log(epub.spine);
console.log(epub.toc);
epub.getChapter("item259", function(err, data){
console.log(err || data);
});
});
epub.parse();
/**
* EPub#getImage(id, callback) -> undefined
* - id (String): Manifest id value for an image
* - callback (Function): callback function
*
* Finds an image an id. Returns the image as Buffer. Callback gets
* an error object, image buffer and image content-type
**/
EPub.prototype.getImage = function(id, callback){
if(this.manifest[id]){
this.zip.readFile(this.manifest[id].href, (function(err, data){
if(err){
callback(new Error("Reading archive failed"));
return;
}
callback(null, data, this.manifest[id]['media-type']);
}).bind(this));
}
}

27
index.js Normal file
View File

@ -0,0 +1,27 @@
var EPub = require("./epub");
var epub = new EPub("img.epub", "tere", "vana");
epub.on("error", function(err){
console.log("ERROR\n-----");
throw err;
});
epub.on("end", function(err){
console.log("PARSED\n-----");
console.log(epub.metadata);
console.log(epub.manifest);
console.log(epub.spine);
console.log(epub.toc);
epub.getChapter("item259", function(err, data){
console.log(err || data);
});
epub.getImage("item262", function(err, data, mimeType){
console.log(err || data);
console.log(mimeType)
});
});
epub.parse();