diff --git a/lib/epub-parser.js b/lib/epub-parser.js index 6b26dea..877063c 100644 --- a/lib/epub-parser.js +++ b/lib/epub-parser.js @@ -31,26 +31,26 @@ EpubParser = (function() { } } - function safeAccess(supposedArray) { - // a quick bandaid to handle undefined lists - // coming back from the parser - poor fix TODO - if(typeof supposedArray === 'undefined') { - return new Array(); - } else { - return supposedArray; - } - } + function safeAccess(supposedArray) { + // a quick bandaid to handle undefined lists + // coming back from the parser - poor fix TODO + if(typeof supposedArray === 'undefined') { + return new Array(); + } else { + return supposedArray; + } + } function open(filename, cb) { /* - "filename" is still called "filename" but now it can be - a full file path, a full URL, or a Buffer object - we should eventually change its name... + "filename" is still called "filename" but now it can be + a full file path, a full URL, or a Buffer object + we should eventually change its name... - */ + */ var epubdata = {}; @@ -58,198 +58,198 @@ EpubParser = (function() { var htmlNav = ''+"\n"; - epubdata = getEpubDataBlock(); - cb(null,epubdata); + var navPoints = ncx[ncxPrefix+"navMap"][0][ncxPrefix+"navPoint"]; - }); - } - }); + for(var i = 0; i < safeAccess(navPoints).length; i++) { + processNavPoint(navPoints[i]); + } + htmlNav += ''+"\n"; + epubdata = getEpubDataBlock(); + cb(null,epubdata); + + }); + } + }); } @@ -283,7 +283,7 @@ EpubParser = (function() { function buildItemHashes() { for(item in itemlist) { - + var href = itemlist[item].$.href; var id = itemlist[item].$.id; var mediaType = itemlist[item].$['media-type']; @@ -301,8 +301,8 @@ EpubParser = (function() { } var itemrefs = itemreflist; - - try { + + try { ncxId = spine.$.toc; } catch(e) { ; @@ -328,22 +328,24 @@ EpubParser = (function() { function buildMetadataLists() { var metas = metadata; for(prop in metas) { - + if(prop == 'meta') { // process a list of meta tags for(var i = 0; i < safeAccess(metas[prop]).length; i++) { - + var m = metas[prop][i].$; if(typeof m.name !== 'undefined') { - var md = {}; - md[m.name] = m.content; - simpleMeta.push(md); + //var md = {}; + //md[m.name] = m.content; + simpleMeta[m.name] = m.content; + //simpleMeta.push(md); } else if (typeof m.property !== 'undefined') { - var md = {}; - md[m.property] = metas[prop][i]._; - simpleMeta.push(md); + //var md = {}; + simpleMeta[m.property] = metas[prop][i]._; + //md[m.property] = metas[prop][i]._; + //simpleMeta.push(md); } if(m.name == 'cover') { @@ -354,11 +356,11 @@ EpubParser = (function() { } - + } else if(prop != '$') { - + var content = ''; var atts = {}; if(metas[prop][0]) { @@ -379,8 +381,9 @@ EpubParser = (function() { } if(typeof prop !== 'undefined') { var md = {}; - md[prop] = content; - simpleMeta.push(md); + //md[prop] = content; + simpleMeta[prop] = content; + //simpleMeta.push(md); } if(prop.match(/identifier$/i)) { @@ -396,108 +399,108 @@ EpubParser = (function() { uniqueIdentifierValue = content; if(typeof metas[prop][0].$.scheme !== 'undefined') { uniqueIdentifierScheme= metas[prop][0].$.scheme; - } + } } } }; } - + } } } function parsePackageElements() { - - // operates on global vars - - - if(typeof opf[opfPrefix+"manifest"] === 'undefined') { - - // it's a problem - // gutenberg files, for example will lead to this condition - // we must assume that tags are not actually namespaced - - opfPrefix = ''; - } - - try { - metadata = opf[opfPrefix+"metadata"][0]; - } catch(e) { - console.log('metadata element error: '+e.message); - console.log('are the tags really namespaced with '+opfPrefix+' or not? file indicates they should be.'); - } - try { - manifest = opf[opfPrefix+"manifest"][0]; - } catch (e) { - console.log('manifest element error: '+e.message); - console.log('are the tags really namespaced with '+opfPrefix+' or not? file indicates they should be.'); - console.log(opfDataXML); - console.log(opf); - console.log('must throw this - unrecoverable'); - throw (e); - } - try { - spine = opf[opfPrefix+"spine"][0]; - } catch(e) { - console.log('spine element error: '+e.message); - console.log('must throw this'); - throw (e); - } - try { - guide = opf[opfPrefix+"guide"][0]; - } catch (e) { - ; - } + + // operates on global vars + + + if(typeof opf[opfPrefix+"manifest"] === 'undefined') { + + // it's a problem + // gutenberg files, for example will lead to this condition + // we must assume that tags are not actually namespaced + + opfPrefix = ''; + } + + try { + metadata = opf[opfPrefix+"metadata"][0]; + } catch(e) { + console.log('metadata element error: '+e.message); + console.log('are the tags really namespaced with '+opfPrefix+' or not? file indicates they should be.'); + } + try { + manifest = opf[opfPrefix+"manifest"][0]; + } catch (e) { + console.log('manifest element error: '+e.message); + console.log('are the tags really namespaced with '+opfPrefix+' or not? file indicates they should be.'); + console.log(opfDataXML); + console.log(opf); + console.log('must throw this - unrecoverable'); + throw (e); + } + try { + spine = opf[opfPrefix+"spine"][0]; + } catch(e) { + console.log('spine element error: '+e.message); + console.log('must throw this'); + throw (e); + } + try { + guide = opf[opfPrefix+"guide"][0]; + } catch (e) { + ; + } } function getEpubDataBlock() { return { - easy: { - primaryID: { - name:uniqueIdentifier, - value:uniqueIdentifierValue, - scheme:uniqueIdentifierScheme - }, - epubVersion: epubVersion, - isEpub3: isEpub3, - md5: md5hash, - epub3NavHtml: epub3NavHtml, - navMapHTML: htmlNav, - linearSpine: linearSpine, - itemHashById: itemHashById, - itemHashByHref: itemHashByHref, - linearSpine: linearSpine, - simpleMeta: simpleMeta, - epub3CoverId: epub3CoverId, - epub3NavId: epub3NavId, - epub2CoverUrl: epub2CoverUrl - }, - paths: { - opfPath: opfPath, - ncxPath: ncxPath, - opsRoot: opsRoot - }, - raw: { - json: { - prefixes: { - opfPrefix:opfPrefix, - dcPrefix:dcPrefix, - ncxPrefix:ncxPrefix - }, - container: container, - opf: opf, - ncx: ncx, - nav: nav - }, - xml: { - opfXML: opfDataXML, - ncxXML: ncxDataXML - } - } - }; + easy: { + primaryID: { + name:uniqueIdentifier, + value:uniqueIdentifierValue, + scheme:uniqueIdentifierScheme + }, + epubVersion: epubVersion, + isEpub3: isEpub3, + md5: md5hash, + epub3NavHtml: epub3NavHtml, + navMapHTML: htmlNav, + linearSpine: linearSpine, + itemHashById: itemHashById, + itemHashByHref: itemHashByHref, + linearSpine: linearSpine, + simpleMeta: simpleMeta, + epub3CoverId: epub3CoverId, + epub3NavId: epub3NavId, + epub2CoverUrl: epub2CoverUrl + }, + paths: { + opfPath: opfPath, + ncxPath: ncxPath, + opsRoot: opsRoot + }, + raw: { + json: { + prefixes: { + opfPrefix:opfPrefix, + dcPrefix:dcPrefix, + ncxPrefix:ncxPrefix + }, + container: container, + opf: opf, + ncx: ncx, + nav: nav + }, + xml: { + opfXML: opfDataXML, + ncxXML: ncxDataXML + } + } + }; } @@ -509,26 +512,26 @@ EpubParser = (function() { } else if(filename.match(/^https?:\/\//i)) { // is a URL - request({ - uri:filename, - encoding:null /* sets the response to be a buffer */ - }, function (error, response, body) { + request({ + uri:filename, + encoding:null /* sets the response to be a buffer */ + }, function (error, response, body) { - if (!error && response.statusCode == 200) { + if (!error && response.statusCode == 200) { - var b = body; - - readAndParseData(b, cb); + var b = body; - } else { + readAndParseData(b, cb); - cb(error,null); + } else { + + cb(error,null); + + } + + }); - } - - }); - } else { // assume local full path to file