Comparing version 0.0.3 to 0.0.4
@@ -9,5 +9,5 @@ | ||
var filters = [ | ||
function (data) { | ||
function (filename, data) { | ||
return data; | ||
} | ||
} | ||
] | ||
@@ -32,6 +32,6 @@ | ||
function runFilterChain(data) | ||
function runFilterChain(filename, data) | ||
{ | ||
for(var i = 0; i < filters.length; i++) { | ||
data = filters[i].apply(this, [data]); | ||
data = filters[i].apply(this, [filename, data]); | ||
} | ||
@@ -41,2 +41,6 @@ return data; | ||
function getPathJson(cacheId) { | ||
return cacheDir+'/'+cacheId+'.json'; | ||
} | ||
module.exports.init = function init(config) | ||
@@ -57,6 +61,40 @@ { | ||
{ | ||
// stub | ||
var rmDir = function(dirPath) { | ||
try { var files = fs.readdirSync(dirPath); } | ||
catch(e) { return; } | ||
if (files.length > 0) { | ||
for (var i = 0; i < files.length; i++) { | ||
var filePath = dirPath + '/' + files[i]; | ||
if (fs.statSync(filePath).isFile()) { | ||
fs.unlinkSync(filePath); | ||
} else { | ||
rmDir(filePath); | ||
} | ||
} | ||
} | ||
fs.rmdirSync(dirPath); | ||
}; | ||
try { var files = fs.readdirSync(cacheDir); } | ||
catch(e) { return; } | ||
if (files.length > 0) { | ||
for (var i = 0; i < files.length; i++) { | ||
var filePath = cacheDir + '/' + files[i]; | ||
if (fs.statSync(filePath).isFile()) { | ||
console.log('removing file '+filePath); | ||
fs.unlinkSync(filePath); | ||
} else { | ||
console.log('removing '+filePath); | ||
rmDir(filePath); | ||
} | ||
} | ||
} | ||
} | ||
module.exports.getBundle = function getBundle(cacheId) { | ||
var bundleJson = fs.readFileSync(getPathJson(cacheId)).toString(); | ||
var bundle = JSON.parse(bundleJson); | ||
return bundle; | ||
} | ||
module.exports.cache = function cache (epubfile, cacheId, cb) | ||
@@ -77,2 +115,3 @@ { | ||
var files = converter.getFiles(); | ||
var parser = converter.getParser(); | ||
@@ -92,43 +131,62 @@ var hashid = epubData.easy.md5; //crypto.createHash('md5').update(fs.readFileSync(epubfile)).digest("hex"); | ||
var pathjson = cacheDir+'/'+cacheId+'.json'; | ||
var pathjson = getPathJson(cacheId); | ||
try { | ||
console.log(basepath); | ||
fs.mkdirSync(basepath); | ||
} catch (e) { // already exists? | ||
; | ||
} | ||
for(file in files) { | ||
for(file in files) { | ||
//console.log(files[file]); | ||
var path = basepath + files[file].name; | ||
//console.log(files[file]); | ||
var path = basepath + files[file].name; | ||
try { | ||
if(files[file].data.length==0) { | ||
fs.mkdirSync(path); | ||
} else { | ||
try { | ||
if(files[file].options.dir) { | ||
fs.mkdirSync(path); | ||
} else { | ||
var data = runFilterChain(files[file].data); | ||
fs.writeFileSync(path, data); | ||
if(file.match(/\.(html|xml|htm|css|js|txt|xhtml)$/)) { | ||
var data = runFilterChain(file, parser.extractText(file)); | ||
fs.writeFileSync(path, data); | ||
} else { | ||
var data = parser.extractBinary(file); | ||
fs.writeFileSync(path, data, 'binary'); | ||
} | ||
} | ||
} catch(e) { | ||
//console.log(e); | ||
} | ||
} | ||
} catch(e) { | ||
console.log(e); | ||
} | ||
} | ||
delete epubData.raw.xml; | ||
var bundle = { | ||
epub: epubData, | ||
html: htmlData | ||
}; | ||
fs.writeFileSync(pathjson, JSON.stringify(bundle)); | ||
} catch (e) { // already exists? | ||
console.log('path exists in cache, pulling json data...'); | ||
var bundle = module.exports.getBundle(cacheId); | ||
} | ||
var bundle = { | ||
json: epubData.raw.json, | ||
html: htmlData | ||
}; | ||
fs.writeFileSync(pathjson, JSON.stringify(bundle)); | ||
cb(cacheId, bundle); | ||
cb(cacheId); | ||
}); | ||
} |
{ | ||
"name": "epub-cache", | ||
"version": "0.0.3", | ||
"version": "0.0.4", | ||
"description": "Creates web-friendly caches of browser-friendly content from Epub files", | ||
@@ -10,7 +10,8 @@ "main": "index.js", | ||
"scripts": { | ||
"test": "node test/test.js test/testfile.epub" | ||
"test": "node test/test.js test/testbook.epub" | ||
}, | ||
"repository": "", | ||
"dependencies": { | ||
"epub2html": ">=0.0.1" | ||
"epub-parser": ">=0.2.0", | ||
"epub2html": ">=0.0.6" | ||
}, | ||
@@ -17,0 +18,0 @@ "author": "vaporbook", |
@@ -0,7 +1,8 @@ | ||
epub-cache | ||
=========== | ||
# What It Does | ||
=Epub-cache | ||
Makes web-friendly caches of Epub file content available to browsers | ||
==Makes web-friendly caches of Epub file content available to browsers | ||
@@ -16,3 +16,6 @@ var cacher = require('../lib/epub-cache'); | ||
console.log('clearing cache...'); | ||
cacher.clear(); | ||
// 2. cache epub contents and get a cache id back | ||
@@ -19,0 +22,0 @@ |
1067298
7
158
9
2
+ Addedepub-parser@>=0.2.0
Updatedepub2html@>=0.0.6