updates
This commit is contained in:
parent
b4f333ca81
commit
30bb1357aa
19
epub.js
19
epub.js
@ -514,13 +514,18 @@ EPub.prototype.walkNavMap = function(branch, path, id_list, level){
|
||||
* - callback (Function): callback function
|
||||
*
|
||||
* Finds a chapter text for an id. Replaces image and link URL's, removes
|
||||
* <head> etc. elements
|
||||
* <head> etc. elements. Return only chapters with mime type application/xhtml+xml
|
||||
**/
|
||||
EPub.prototype.getChapter = function(id, callback){
|
||||
var path = this.rootFile.split("/"), keys = Object.keys(this.manifest);
|
||||
path.pop();
|
||||
|
||||
if(this.manifest[id]){
|
||||
|
||||
if((this.manifest[id]['media-type'] || "").toLowerCase().trim() != "application/xhtml+xml"){
|
||||
return callback(new Error("Inavlid mime type for chapter"));
|
||||
}
|
||||
|
||||
this.zip.readFile(this.manifest[id].href, (function(err, data){
|
||||
if(err){
|
||||
callback(new Error("Reading archive failed"));
|
||||
@ -605,6 +610,8 @@ EPub.prototype.getChapter = function(id, callback){
|
||||
callback(null, str);
|
||||
|
||||
}).bind(this));
|
||||
}else{
|
||||
callback(new Error("File not found"));
|
||||
}
|
||||
}
|
||||
|
||||
@ -615,10 +622,16 @@ EPub.prototype.getChapter = function(id, callback){
|
||||
* - 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
|
||||
* an error object, image buffer and image content-type.
|
||||
* Return only images with mime type image/*
|
||||
**/
|
||||
EPub.prototype.getImage = function(id, callback){
|
||||
if(this.manifest[id]){
|
||||
|
||||
if((this.manifest[id]['media-type'] || "").toLowerCase().trim().substr(0,6) != "image/"){
|
||||
return callback(new Error("Inavlid mime type for image"));
|
||||
}
|
||||
|
||||
this.zip.readFile(this.manifest[id].href, (function(err, data){
|
||||
if(err){
|
||||
callback(new Error("Reading archive failed"));
|
||||
@ -627,5 +640,7 @@ EPub.prototype.getImage = function(id, callback){
|
||||
|
||||
callback(null, data, this.manifest[id]['media-type']);
|
||||
}).bind(this));
|
||||
}else{
|
||||
callback(new Error("File not found"));
|
||||
}
|
||||
}
|
101
index.html
Normal file
101
index.html
Normal file
@ -0,0 +1,101 @@
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<meta charset="utf-8" />
|
||||
<title>EPUB reader</title>
|
||||
|
||||
|
||||
<style type="text/css">
|
||||
body{
|
||||
font-family: "luxi-serif-1","luxi-serif-2",serif;
|
||||
}
|
||||
h1, h2, h3, h4, h5{
|
||||
text-align: center;
|
||||
}
|
||||
|
||||
#contents{
|
||||
width: 640px;
|
||||
margin:10px auto;
|
||||
padding: 20px;
|
||||
background: #D3CDAC;
|
||||
}
|
||||
|
||||
p{
|
||||
text-align: justify;
|
||||
}
|
||||
|
||||
img{
|
||||
display: block;
|
||||
margin: 5px 0;
|
||||
max-width: 640px;
|
||||
}
|
||||
|
||||
hr{
|
||||
width: 90%;
|
||||
height: 0px;
|
||||
border:0;
|
||||
border-top: 1px dashed #B75F14;
|
||||
margin: 10px auto;
|
||||
}
|
||||
|
||||
.chapter{
|
||||
border-bottom: 5px dotted #604328;
|
||||
padding-bottom: 10px;
|
||||
margin-bottom: 10px;
|
||||
}
|
||||
</style>
|
||||
|
||||
|
||||
|
||||
<script src="https://ajax.googleapis.com/ajax/libs/prototype/1.7.0.0/prototype.js" type="text/javascript"></script>
|
||||
|
||||
<script type="text/javascript">
|
||||
|
||||
$(document).observe("dom:loaded", function(){
|
||||
|
||||
new Ajax.Request("/contents",{
|
||||
method:"post",
|
||||
onComplete: function(response){
|
||||
var contents = {toc:[], flow:[]};
|
||||
try{
|
||||
contents = response.responseText.evalJSON();
|
||||
}catch(E){}
|
||||
|
||||
renderPage(contents);
|
||||
|
||||
}
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
function renderPage(contents){
|
||||
|
||||
if(!contents.flow.length)return;
|
||||
|
||||
var chapter = contents.flow.shift();;
|
||||
|
||||
new Ajax.Request("/chapter/"+chapter.id,{
|
||||
method:"post",
|
||||
onComplete: function(response){
|
||||
$("contents").innerHTML += "<div class=\"chapter\">"+response.responseText+"</div>";
|
||||
if(contents.flow.length){
|
||||
renderPage(contents);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
}
|
||||
</script>
|
||||
|
||||
<script type="text/javascript" src="http://use.typekit.com/qbv6mfk.js"></script>
|
||||
<script type="text/javascript">try{Typekit.load();}catch(e){}</script>
|
||||
|
||||
</head>
|
||||
<body>
|
||||
|
||||
<div id="toc"></div>
|
||||
|
||||
<div id="contents"></div>
|
||||
|
||||
</body>
|
||||
</html>
|
2
index.js
2
index.js
@ -1,6 +1,6 @@
|
||||
var EPub = require("./epub");
|
||||
|
||||
var epub = new EPub("img.epub", "tere", "vana");
|
||||
var epub = new EPub("tasuja.epub", "tere", "vana");
|
||||
epub.on("error", function(err){
|
||||
console.log("ERROR\n-----");
|
||||
throw err;
|
||||
|
79
server.js
Normal file
79
server.js
Normal file
@ -0,0 +1,79 @@
|
||||
var http = require('http'),
|
||||
EPub = require("./epub"),
|
||||
fs = require("fs");
|
||||
|
||||
var EPUBFILE = "tasuja.epub";
|
||||
|
||||
var epub = new EPub(EPUBFILE, "/epubimg", "/chapter");
|
||||
epub.on("error", function(err){
|
||||
console.log("ERROR\n-----");
|
||||
throw err;
|
||||
});
|
||||
|
||||
epub.on("end", function(){
|
||||
|
||||
startserver();
|
||||
|
||||
});
|
||||
|
||||
epub.parse();
|
||||
|
||||
|
||||
function startserver(){
|
||||
http.createServer(function (req, res) {
|
||||
|
||||
if(req.url.match(/^\/contents/)){
|
||||
res.writeHead(200, {'Content-Type': 'text/javascript'});
|
||||
res.end(JSON.stringify({toc: epub.toc, flow: epub.spine.contents}));
|
||||
return;
|
||||
}
|
||||
|
||||
if(req.url.match(/^\/chapter/)){
|
||||
var parts = req.url.split("/");
|
||||
epub.getChapter(parts[2], function(err, data){
|
||||
if(err){
|
||||
res.writeHead(500, {'Content-Type': 'text/html'});
|
||||
res.end("<h1>Error</h1><p>"+err.message+"</p>");
|
||||
return;
|
||||
}
|
||||
res.writeHead(200, {'Content-Type': 'text/html'});
|
||||
res.end(data);
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
if(req.url.match(/^\/epubimg/)){
|
||||
var parts = req.url.split("/");
|
||||
epub.getImage(parts[2], function(err, data, mimeType){
|
||||
if(err){
|
||||
res.writeHead(500, {'Content-Type': 'text/html'});
|
||||
res.end("<h1>Error</h1><p>"+err.message+"</p>");
|
||||
return;
|
||||
}
|
||||
res.writeHead(200, {'Content-Type': mimeType});
|
||||
res.end(data);
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
if(req.url == "/"){
|
||||
fs.readFile("index.html", function(err, data){
|
||||
if(err){
|
||||
res.writeHead(500, {'Content-Type': 'text/html'});
|
||||
res.end("<h1>Error</h1><p>"+err.message+"</p>");
|
||||
return;
|
||||
}
|
||||
res.writeHead(200, {'Content-Type': 'text/html'});
|
||||
res.end(data);
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
res.writeHead(200, {'Content-Type': 'text/plain'});
|
||||
res.end('Hello World\n');
|
||||
|
||||
}).listen(8080);
|
||||
console.log('Server running');
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user