@daisy/epub-utils
Advanced tools
Comparing version
@@ -39,2 +39,3 @@ // input: unzipped book directory | ||
this.metadata = {}; | ||
this.opfLang = undefined; | ||
this.contentDocMediaType = "application/xhtml+xml"; | ||
@@ -63,3 +64,3 @@ } | ||
const arr1 = select('descendant::html:a/@href', sPageList[0]); | ||
pageListHrefs = arr1.map(o => decodeURI(o.nodeValue)); | ||
pageListHrefs = arr1.map(o => decodeURIComponent(o.nodeValue)); | ||
// console.log(arr1.length, JSON.stringify(pageListHrefs, null, 4)); | ||
@@ -72,3 +73,3 @@ } | ||
const arr2 = select('descendant::html:a/@href', sTOC[0]); | ||
tocHrefs = arr2.map(o => decodeURI(o.nodeValue)); | ||
tocHrefs = arr2.map(o => decodeURIComponent(o.nodeValue)); | ||
// console.log(arr2.length, JSON.stringify(tocHrefs, null, 4)); | ||
@@ -113,5 +114,16 @@ } | ||
} | ||
function parseMetadata(doc, select) { | ||
function parseMetadata(doc, select, epub) { | ||
const isEPUB3 = epub.version && epub.version.startsWith("3"); | ||
const result = {}; | ||
let dcSourceAdded = false; | ||
let dcSource = undefined; | ||
let dcSourceId = undefined; | ||
select('/opf:package/opf:metadata/dc:*', doc).forEach(dcElem => { | ||
if (isEPUB3 && dcElem.localName === "source") { | ||
// if (!!dcElem.getAttribute("id")) { | ||
dcSource = dcElem.textContent; | ||
dcSourceId = dcElem.getAttribute("id"); | ||
// } | ||
return; | ||
} | ||
addMeta(`dc:${dcElem.localName}`, dcElem.textContent, result); | ||
@@ -124,2 +136,3 @@ }); | ||
// implies isEPUB3 | ||
let refines = meta.getAttribute('refines'); | ||
@@ -133,2 +146,8 @@ if (refines) { | ||
if (refines) { | ||
// isEPUB3 implied | ||
if (md === "source-of" && meta.textContent === "pagination" && refines === dcSourceId) { | ||
dcSourceAdded = true; | ||
addMeta(`dc:source`, dcSource, result); | ||
return; | ||
} | ||
// we exclude most refined metadata such as audio duration for individual spine items, or title-type=subtitle for dc:title, etc. | ||
@@ -156,2 +175,12 @@ // ... but we preserve known global metadata: | ||
}); | ||
if (isEPUB3 && dcSource && !dcSourceAdded) { | ||
// console.log(JSON.stringify(result)); | ||
const confTo = result["dcterms:conformsTo"] || epub.links && epub.links["dcterms:conformsTo"]; | ||
if (confTo && confTo.startsWith("http://www.idpf.org/epub/a11y/")) { | ||
// EPUB a11y 1.0 | ||
addMeta(`dc:source`, dcSource, result); | ||
} | ||
} | ||
return result; | ||
@@ -197,3 +226,3 @@ } | ||
addLink(rel, decodeURI(link.getAttribute('href')), result); | ||
addLink(rel, decodeURIComponent(link.getAttribute('href')), result); | ||
}); | ||
@@ -240,4 +269,7 @@ return result; | ||
this.metadata = parseMetadata(doc, select); | ||
const versionAttr = select('/opf:package/@version', doc)[0]; | ||
this.version = versionAttr ? versionAttr.nodeValue : undefined; | ||
this.links = parseLinks(doc, select); | ||
this.metadata = parseMetadata(doc, select, this); | ||
@@ -253,3 +285,4 @@ var spineContainsNavDoc = undefined; | ||
var spineItem = new SpineItem(); | ||
spineItem.relpath = decodeURI(manifestItem[0].getAttribute('href')); | ||
spineItem.relpath = decodeURIComponent(manifestItem[0].getAttribute('href')); | ||
// if (manifestItem[0].getAttribute('href').includes("%")) console.log(`${manifestItem[0].getAttribute('href')} ===> ${spineItem.relpath}`); | ||
spineItem.filepath = path.join(path.dirname(packageDocPath), spineItem.relpath); | ||
@@ -278,3 +311,3 @@ | ||
spineItem.mediaOverlay = {}; | ||
spineItem.mediaOverlay.smilRelPath = decodeURI(smilManifestItem[0].getAttribute('href')); | ||
spineItem.mediaOverlay.smilRelPath = decodeURIComponent(smilManifestItem[0].getAttribute('href')); | ||
spineItem.mediaOverlay.smilFilePath = path.join(path.dirname(packageDocPath), spineItem.mediaOverlay.smilRelPath); | ||
@@ -293,3 +326,3 @@ // spineItem.mediaOverlay.smilUrl = fileUrl(spineItem.mediaOverlay.smilFilePath); | ||
if (navDocRef.length > 0) { | ||
const navDocPath = decodeURI(navDocRef[0].nodeValue); | ||
const navDocPath = decodeURIComponent(navDocRef[0].nodeValue); | ||
const navDocFullPath = path.join(path.dirname(packageDocPath), navDocPath); | ||
@@ -375,5 +408,14 @@ this.navDoc = parseNavDoc(navDocFullPath, epubDir); | ||
} | ||
let role = o.getAttribute('role'); | ||
if (role) { | ||
role = role.trim(); | ||
} | ||
if (!role) { | ||
role = undefined; | ||
} | ||
return { | ||
id: o.getAttribute("id"), | ||
epubType | ||
epubType, | ||
role | ||
}; | ||
@@ -396,3 +438,3 @@ }); | ||
if (rootfiles.length > 0) { | ||
return path.join(epubDir, decodeURI(rootfiles[0].nodeValue)); | ||
return path.join(epubDir, decodeURIComponent(rootfiles[0].nodeValue)); | ||
} | ||
@@ -402,3 +444,11 @@ return ''; | ||
function encodeURIComponent_RFC3986(str) { | ||
return encodeURIComponent(str).replace(/[!'()*]/g, c => { | ||
return "%" + c.charCodeAt(0).toString(16); | ||
}); | ||
} | ||
module.exports.SpineItem = SpineItem; | ||
module.exports.EpubParser = EpubParser; | ||
module.exports.EpubParser = EpubParser; | ||
module.exports.encodeURIComponent_RFC3986 = encodeURIComponent_RFC3986; |
@@ -72,3 +72,3 @@ 'use strict'; | ||
async function retryUnzip(unzipDir, epub, error) { | ||
if (error.message === undefined) throw error; | ||
if (!error.message) throw error; | ||
winston.info('Trying to repair the archive and unzip again...'); | ||
@@ -130,3 +130,3 @@ try { | ||
async extract(unzipDir) { | ||
if (this.basedir !== undefined) { | ||
if (this.basedir) { | ||
return this; | ||
@@ -133,0 +133,0 @@ } else if (this.expanded) { |
'use strict'; | ||
const EPUB = require('./epub'); | ||
const EPUB = require('./epub.js'); | ||
const epubParse = require('./epub-parse.js'); | ||
module.exports = { | ||
EPUB | ||
EPUB, | ||
encodeURIComponent_RFC3986: epubParse.encodeURIComponent_RFC3986 | ||
}; |
{ | ||
"name": "@daisy/epub-utils", | ||
"version": "1.3.3-alpha.2", | ||
"version": "1.3.3-alpha.3", | ||
"engines": { | ||
@@ -30,6 +30,6 @@ "node": ">=12.0.0", | ||
"node-stream-zip": "^1.15.0", | ||
"tmp": "^0.2.1", | ||
"winston": "^3.10.0", | ||
"tmp": "^0.2.3", | ||
"winston": "^3.17.0", | ||
"xmldom": "^0.6.0", | ||
"xpath": "^0.0.32" | ||
"xpath": "^0.0.34" | ||
}, | ||
@@ -36,0 +36,0 @@ "publishConfig": { |
22229
8.88%550
9.13%+ Added
- Removed
Updated
Updated
Updated