r2-shared-js
Version:
Readium 2 'shared' for NodeJS (TypeScript)
1,108 lines (1,107 loc) • 88.1 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.updateDurations = exports.lazyLoadMediaOverlays = exports.flattenDaisy2SmilAudioSeq = exports.addMediaOverlaySMIL = exports.fillTOC = exports.loadFileBufferFromZipPath = exports.loadFileStrFromZipPath = exports.addOtherMetadata = exports.getOpf_ = exports.getOpf = exports.getNcx_ = exports.getNcx = exports.langStringIsRTL = exports.setPublicationDirection = exports.addTitle = exports.addIdentifier = exports.addLanguage = exports.fillSpineAndResource = exports.findInManifestByID = exports.findInSpineByHref = exports.findAllMetaByRefineAndProperty = exports.findMetaByRefineAndProperty = exports.addContributor = exports.findContributorInMeta = exports.fillSubject = exports.fillPublicationDate = exports.isEpub3OrMore = exports.parseSpaceSeparatedString = exports.BCP47_UNKNOWN_LANG = exports.mediaOverlayURLParam = exports.mediaOverlayURLPath = void 0;
const tslib_1 = require("tslib");
const debug_ = require("debug");
const mime = require("mime-types");
const moment = require("moment");
const path = require("path");
const xmldom = require("@xmldom/xmldom");
const media_overlay_1 = require("../models/media-overlay");
const metadata_1 = require("../models/metadata");
const metadata_contributor_1 = require("../models/metadata-contributor");
const metadata_media_overlay_1 = require("../models/metadata-media-overlay");
const metadata_properties_1 = require("../models/metadata-properties");
const metadata_subject_1 = require("../models/metadata-subject");
const publication_link_1 = require("../models/publication-link");
const ta_json_string_tokens_converter_1 = require("../models/ta-json-string-tokens-converter");
const UrlUtils_1 = require("r2-utils-js/dist/es7-es2016/src/_utils/http/UrlUtils");
const BufferUtils_1 = require("r2-utils-js/dist/es7-es2016/src/_utils/stream/BufferUtils");
const xml_js_mapper_1 = require("r2-utils-js/dist/es7-es2016/src/_utils/xml-js-mapper");
const transformer_1 = require("../transform/transformer");
const zipHasEntry_1 = require("../_utils/zipHasEntry");
const ncx_1 = require("./epub/ncx");
const opf_1 = require("./epub/opf");
const opf_author_1 = require("./epub/opf-author");
const smil_1 = require("./epub/smil");
const smil_seq_1 = require("./epub/smil-seq");
const bom_1 = require("r2-utils-js/dist/es7-es2016/src/_utils/bom");
const debug = debug_("r2:shared#parser/epub-daisy-common");
const epub3 = "3.0";
const epub301 = "3.0.1";
const epub31 = "3.1";
exports.mediaOverlayURLPath = "media-overlay.json";
exports.mediaOverlayURLParam = "resource";
exports.BCP47_UNKNOWN_LANG = "und";
const parseSpaceSeparatedString = (str) => {
return str ? str.trim().split(" ").map((role) => {
return role.trim();
}).filter((role) => {
return role.length > 0;
}) : [];
};
exports.parseSpaceSeparatedString = parseSpaceSeparatedString;
const getEpubVersion = (rootfile, opf) => {
if (rootfile.Version) {
return rootfile.Version;
}
else if (opf.Version) {
return opf.Version;
}
return undefined;
};
const isEpub3OrMore = (rootfile, opf) => {
const version = getEpubVersion(rootfile, opf);
return (version === epub3 || version === epub301 || version === epub31);
};
exports.isEpub3OrMore = isEpub3OrMore;
const fillPublicationDate = (publication, rootfile, opf) => {
var _a, _b, _c, _d, _e, _f, _g;
let publishedDateStr;
let modifiedDateStr;
if ((_b = (_a = opf.Metadata) === null || _a === void 0 ? void 0 : _a.Meta) === null || _b === void 0 ? void 0 : _b.length) {
for (const m of opf.Metadata.Meta) {
if (m.Name === "dcterms:modified" && m.Content) {
modifiedDateStr = m.Content;
break;
}
if (m.Property === "dcterms:modified" && m.Data) {
modifiedDateStr = m.Data;
break;
}
}
}
const opfMetadataDateArray = [].concat(((_e = (_d = (_c = opf.Metadata) === null || _c === void 0 ? void 0 : _c.DCMetadata) === null || _d === void 0 ? void 0 : _d.Date) === null || _e === void 0 ? void 0 : _e.length) ? opf.Metadata.DCMetadata.Date : [], ((_g = (_f = opf.Metadata) === null || _f === void 0 ? void 0 : _f.Date) === null || _g === void 0 ? void 0 : _g.length) ? opf.Metadata.Date : []);
if (opfMetadataDateArray === null || opfMetadataDateArray === void 0 ? void 0 : opfMetadataDateArray.length) {
for (const metaDate of opfMetadataDateArray) {
if (!modifiedDateStr &&
(metaDate.Event === "modification" || metaDate.Event === "ops-modification")) {
modifiedDateStr = metaDate.Data;
}
if (!publishedDateStr &&
(metaDate.Event === "publication" || metaDate.Event === "ops-publication")) {
publishedDateStr = metaDate.Data;
}
if (modifiedDateStr && publishedDateStr) {
break;
}
}
if (!publishedDateStr) {
for (const metaDate of opfMetadataDateArray) {
if (!metaDate.Event) {
publishedDateStr = metaDate.Data;
break;
}
}
}
if (!publishedDateStr) {
if (!rootfile || (0, exports.isEpub3OrMore)(rootfile, opf)) {
publishedDateStr = opfMetadataDateArray[0].Data;
}
}
}
if (publishedDateStr) {
try {
const mom = moment(publishedDateStr);
if (mom.isValid()) {
publication.Metadata.PublicationDate = mom.toDate();
}
}
catch (_err) {
debug("INVALID published DATE/TIME? " + publishedDateStr);
}
}
if (modifiedDateStr) {
try {
const mom = moment(modifiedDateStr);
if (mom.isValid()) {
publication.Metadata.Modified = mom.toDate();
}
}
catch (_err) {
debug("INVALID modified DATE/TIME? " + modifiedDateStr);
}
}
};
exports.fillPublicationDate = fillPublicationDate;
const fillSubject = (publication, opf) => {
var _a, _b, _c, _d, _e;
const opfMetadataSubject = ((_c = (_b = (_a = opf.Metadata) === null || _a === void 0 ? void 0 : _a.DCMetadata) === null || _b === void 0 ? void 0 : _b.Subject) === null || _c === void 0 ? void 0 : _c.length) ?
opf.Metadata.DCMetadata.Subject :
(((_e = (_d = opf.Metadata) === null || _d === void 0 ? void 0 : _d.Subject) === null || _e === void 0 ? void 0 : _e.length) ?
opf.Metadata.Subject :
undefined);
if (opfMetadataSubject) {
opfMetadataSubject.forEach((s) => {
const sub = new metadata_subject_1.Subject();
const xmlLang = s.Lang || opf.Lang;
const isLangOverride = s.Lang && opf.Lang && s.Lang !== opf.Lang;
if (xmlLang && (isLangOverride || (0, exports.langStringIsRTL)(xmlLang.toLowerCase()))) {
sub.Name = {};
sub.Name[xmlLang.toLowerCase()] = s.Data;
}
else {
sub.Name = s.Data;
}
sub.Code = s.Term;
sub.Scheme = s.Authority;
if (!publication.Metadata.Subject) {
publication.Metadata.Subject = [];
}
publication.Metadata.Subject.push(sub);
});
}
};
exports.fillSubject = fillSubject;
const findContributorInMeta = (publication, rootfile, opf) => {
var _a, _b, _c, _d, _e;
if (!rootfile || (0, exports.isEpub3OrMore)(rootfile, opf)) {
const func = (meta) => {
if (meta.Property === "dcterms:creator" || meta.Property === "dcterms:contributor") {
const cont = new opf_author_1.Author();
cont.Data = meta.Data;
cont.ID = meta.ID;
(0, exports.addContributor)(publication, rootfile, opf, cont, undefined);
}
};
if ((_c = (_b = (_a = opf.Metadata) === null || _a === void 0 ? void 0 : _a.XMetadata) === null || _b === void 0 ? void 0 : _b.Meta) === null || _c === void 0 ? void 0 : _c.length) {
opf.Metadata.XMetadata.Meta.forEach(func);
}
if ((_e = (_d = opf.Metadata) === null || _d === void 0 ? void 0 : _d.Meta) === null || _e === void 0 ? void 0 : _e.length) {
opf.Metadata.Meta.forEach(func);
}
}
};
exports.findContributorInMeta = findContributorInMeta;
const addContributor = (publication, rootfile, opf, cont, forcedRole) => {
const contributor = new metadata_contributor_1.Contributor();
let role;
if (rootfile && (0, exports.isEpub3OrMore)(rootfile, opf)) {
if (cont.FileAs) {
contributor.SortAs = cont.FileAs;
}
else {
const metaFileAs = (0, exports.findMetaByRefineAndProperty)(opf, cont.ID, "file-as");
if (metaFileAs && metaFileAs.Property === "file-as") {
contributor.SortAs = metaFileAs.Data;
}
}
const metaRole = (0, exports.findMetaByRefineAndProperty)(opf, cont.ID, "role");
if (metaRole && metaRole.Property === "role") {
role = metaRole.Data;
}
if (!role && forcedRole) {
role = forcedRole;
}
const metaAlt = (0, exports.findAllMetaByRefineAndProperty)(opf, cont.ID, "alternate-script");
if (metaAlt && metaAlt.length) {
contributor.Name = {};
metaAlt.forEach((m) => {
if (m.Lang) {
contributor.Name[m.Lang.toLowerCase()] = m.Data;
}
});
const xmlLang = cont.Lang || opf.Lang;
if (xmlLang) {
contributor.Name[xmlLang.toLowerCase()] = cont.Data;
}
else if (publication.Metadata &&
publication.Metadata.Language &&
publication.Metadata.Language.length &&
!contributor.Name[publication.Metadata.Language[0].toLowerCase()]) {
contributor.Name[publication.Metadata.Language[0].toLowerCase()] = cont.Data;
}
else {
contributor.Name[exports.BCP47_UNKNOWN_LANG] = cont.Data;
}
}
else {
const xmlLang = cont.Lang || opf.Lang;
const isLangOverride = cont.Lang && opf.Lang && cont.Lang !== opf.Lang;
if (xmlLang && (isLangOverride || (0, exports.langStringIsRTL)(xmlLang.toLowerCase()))) {
contributor.Name = {};
contributor.Name[xmlLang.toLowerCase()] = cont.Data;
}
else {
contributor.Name = cont.Data;
}
}
}
else {
const xmlLang = cont.Lang || opf.Lang;
const isLangOverride = cont.Lang && opf.Lang && cont.Lang !== opf.Lang;
if (xmlLang && (isLangOverride || (0, exports.langStringIsRTL)(xmlLang.toLowerCase()))) {
contributor.Name = {};
contributor.Name[xmlLang.toLowerCase()] = cont.Data;
}
else {
contributor.Name = cont.Data;
}
role = cont.Role;
if (!role && forcedRole) {
role = forcedRole;
}
}
if (role) {
switch (role) {
case "aut": {
if (!publication.Metadata.Author) {
publication.Metadata.Author = [];
}
publication.Metadata.Author.push(contributor);
break;
}
case "trl": {
if (!publication.Metadata.Translator) {
publication.Metadata.Translator = [];
}
publication.Metadata.Translator.push(contributor);
break;
}
case "art": {
if (!publication.Metadata.Artist) {
publication.Metadata.Artist = [];
}
publication.Metadata.Artist.push(contributor);
break;
}
case "edt": {
if (!publication.Metadata.Editor) {
publication.Metadata.Editor = [];
}
publication.Metadata.Editor.push(contributor);
break;
}
case "ill": {
if (!publication.Metadata.Illustrator) {
publication.Metadata.Illustrator = [];
}
publication.Metadata.Illustrator.push(contributor);
break;
}
case "ltr": {
if (!publication.Metadata.Letterer) {
publication.Metadata.Letterer = [];
}
publication.Metadata.Letterer.push(contributor);
break;
}
case "pen": {
if (!publication.Metadata.Penciler) {
publication.Metadata.Penciler = [];
}
publication.Metadata.Penciler.push(contributor);
break;
}
case "clr": {
if (!publication.Metadata.Colorist) {
publication.Metadata.Colorist = [];
}
publication.Metadata.Colorist.push(contributor);
break;
}
case "ink": {
if (!publication.Metadata.Inker) {
publication.Metadata.Inker = [];
}
publication.Metadata.Inker.push(contributor);
break;
}
case "nrt": {
if (!publication.Metadata.Narrator) {
publication.Metadata.Narrator = [];
}
publication.Metadata.Narrator.push(contributor);
break;
}
case "pbl": {
if (!publication.Metadata.Publisher) {
publication.Metadata.Publisher = [];
}
publication.Metadata.Publisher.push(contributor);
break;
}
default: {
contributor.Role = [role];
if (!publication.Metadata.Contributor) {
publication.Metadata.Contributor = [];
}
publication.Metadata.Contributor.push(contributor);
}
}
}
};
exports.addContributor = addContributor;
const findMetaByRefineAndProperty = (opf, ID, property) => {
const ret = (0, exports.findAllMetaByRefineAndProperty)(opf, ID, property);
if (ret.length) {
return ret[0];
}
return undefined;
};
exports.findMetaByRefineAndProperty = findMetaByRefineAndProperty;
const findAllMetaByRefineAndProperty = (opf, ID, property) => {
var _a, _b, _c, _d, _e;
const metas = [];
const refineID = "#" + ID;
const func = (metaTag) => {
if (metaTag.Refine === refineID && metaTag.Property === property) {
metas.push(metaTag);
}
};
if ((_c = (_b = (_a = opf.Metadata) === null || _a === void 0 ? void 0 : _a.XMetadata) === null || _b === void 0 ? void 0 : _b.Meta) === null || _c === void 0 ? void 0 : _c.length) {
opf.Metadata.XMetadata.Meta.forEach(func);
}
if ((_e = (_d = opf.Metadata) === null || _d === void 0 ? void 0 : _d.Meta) === null || _e === void 0 ? void 0 : _e.length) {
opf.Metadata.Meta.forEach(func);
}
return metas;
};
exports.findAllMetaByRefineAndProperty = findAllMetaByRefineAndProperty;
const findInSpineByHref = (publication, href) => {
if (publication.Spine && publication.Spine.length) {
const ll = publication.Spine.find((l) => {
if (l.HrefDecoded === href) {
return true;
}
return false;
});
if (ll) {
return ll;
}
}
return undefined;
};
exports.findInSpineByHref = findInSpineByHref;
const findInManifestByID = (publication, rootfile, opf, ID, zip, addLinkData) => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
if (opf.Manifest && opf.Manifest.length) {
const item = opf.Manifest.find((manItem) => {
if (manItem.ID === ID) {
return true;
}
return false;
});
if (item && opf.ZipPath) {
const linkItem = new publication_link_1.Link();
linkItem.TypeLink = item.MediaType;
const itemHrefDecoded = item.HrefDecoded;
if (!itemHrefDecoded) {
return Promise.reject("item.Href?!");
}
linkItem.setHrefDecoded(path.join(path.dirname(opf.ZipPath), itemHrefDecoded)
.replace(/\\/g, "/"));
yield addLinkData(publication, rootfile, opf, zip, linkItem, item);
return linkItem;
}
}
return Promise.reject(`ID ${ID} not found`);
});
exports.findInManifestByID = findInManifestByID;
const fillSpineAndResource = (publication, rootfile, opf, zip, addLinkData) => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
var _a, _b;
if (!opf.ZipPath) {
return;
}
if (opf.Spine && opf.Spine.Items && opf.Spine.Items.length) {
for (const item of opf.Spine.Items) {
if (!item.Linear || item.Linear === "yes" ||
(item.Linear === "no" && ((_b = (_a = publication.Metadata) === null || _a === void 0 ? void 0 : _a.Rendition) === null || _b === void 0 ? void 0 : _b.Layout) === metadata_properties_1.LayoutEnum.Fixed)) {
let linkItem;
try {
linkItem = yield (0, exports.findInManifestByID)(publication, rootfile, opf, item.IDref, zip, addLinkData);
}
catch (err) {
debug(err);
continue;
}
if (linkItem && linkItem.Href) {
if (!publication.Spine) {
publication.Spine = [];
}
publication.Spine.push(linkItem);
}
}
}
}
if (opf.Manifest && opf.Manifest.length) {
for (const item of opf.Manifest) {
const itemHrefDecoded = item.HrefDecoded;
if (!itemHrefDecoded) {
debug("!? item.Href", JSON.stringify(item, null, 4));
continue;
}
const zipPath = path.join(path.dirname(opf.ZipPath), itemHrefDecoded)
.replace(/\\/g, "/");
const linkSpine = (0, exports.findInSpineByHref)(publication, zipPath);
if (!linkSpine || !linkSpine.Href) {
const linkItem = new publication_link_1.Link();
linkItem.TypeLink = item.MediaType;
linkItem.setHrefDecoded(zipPath);
if (!publication.Resources) {
publication.Resources = [];
}
publication.Resources.push(linkItem);
yield addLinkData(publication, rootfile, opf, zip, linkItem, item);
}
}
}
});
exports.fillSpineAndResource = fillSpineAndResource;
const addLanguage = (publication, opf) => {
var _a, _b, _c, _d, _e;
const opfMetadataLanguage = ((_c = (_b = (_a = opf.Metadata) === null || _a === void 0 ? void 0 : _a.DCMetadata) === null || _b === void 0 ? void 0 : _b.Language) === null || _c === void 0 ? void 0 : _c.length) ?
opf.Metadata.DCMetadata.Language :
(((_e = (_d = opf.Metadata) === null || _d === void 0 ? void 0 : _d.Language) === null || _e === void 0 ? void 0 : _e.length) ?
opf.Metadata.Language :
undefined);
if (opfMetadataLanguage) {
publication.Metadata.Language = opfMetadataLanguage;
}
};
exports.addLanguage = addLanguage;
const addIdentifier = (publication, opf) => {
var _a, _b, _c, _d, _e;
const opfMetadataIdentifier = ((_c = (_b = (_a = opf.Metadata) === null || _a === void 0 ? void 0 : _a.DCMetadata) === null || _b === void 0 ? void 0 : _b.Identifier) === null || _c === void 0 ? void 0 : _c.length) ?
opf.Metadata.DCMetadata.Identifier :
(((_e = (_d = opf.Metadata) === null || _d === void 0 ? void 0 : _d.Identifier) === null || _e === void 0 ? void 0 : _e.length) ?
opf.Metadata.Identifier :
undefined);
if (opfMetadataIdentifier) {
if (opf.UniqueIdentifier && opfMetadataIdentifier.length > 1) {
opfMetadataIdentifier.forEach((iden) => {
if (iden.ID === opf.UniqueIdentifier) {
publication.Metadata.Identifier = iden.Data;
}
});
}
else if (opfMetadataIdentifier.length > 0) {
publication.Metadata.Identifier = opfMetadataIdentifier[0].Data;
}
}
};
exports.addIdentifier = addIdentifier;
const addTitle = (publication, rootfile, opf) => {
var _a, _b, _c, _d, _e, _f, _g, _h;
const opfMetadataTitle = ((_c = (_b = (_a = opf.Metadata) === null || _a === void 0 ? void 0 : _a.DCMetadata) === null || _b === void 0 ? void 0 : _b.Title) === null || _c === void 0 ? void 0 : _c.length) ?
opf.Metadata.DCMetadata.Title :
(((_e = (_d = opf.Metadata) === null || _d === void 0 ? void 0 : _d.Title) === null || _e === void 0 ? void 0 : _e.length) ?
opf.Metadata.Title :
undefined);
if (rootfile && (0, exports.isEpub3OrMore)(rootfile, opf)) {
let mainTitle;
let subTitle;
let subTitleDisplaySeq = 0;
if (opfMetadataTitle) {
if (((_f = opf.Metadata) === null || _f === void 0 ? void 0 : _f.Meta) || ((_h = (_g = opf.Metadata) === null || _g === void 0 ? void 0 : _g.XMetadata) === null || _h === void 0 ? void 0 : _h.Meta)) {
const tt = opfMetadataTitle.find((title) => {
var _a, _b, _c;
const refineID = "#" + title.ID;
const func0 = (meta) => {
if (meta.Data === "main" && meta.Refine === refineID) {
return true;
}
return false;
};
let m = ((_a = opf.Metadata) === null || _a === void 0 ? void 0 : _a.Meta) ? opf.Metadata.Meta.find(func0) : undefined;
if (!m && ((_c = (_b = opf.Metadata) === null || _b === void 0 ? void 0 : _b.XMetadata) === null || _c === void 0 ? void 0 : _c.Meta)) {
m = opf.Metadata.XMetadata.Meta.find(func0);
}
if (m) {
return true;
}
return false;
});
if (tt) {
mainTitle = tt;
}
opfMetadataTitle.forEach((title) => {
var _a, _b, _c, _d, _e, _f;
const refineID = "#" + title.ID;
const func1 = (meta) => {
if (meta.Data === "subtitle" && meta.Refine === refineID) {
return true;
}
return false;
};
let m = ((_a = opf.Metadata) === null || _a === void 0 ? void 0 : _a.Meta) ? opf.Metadata.Meta.find(func1) : undefined;
if (!m && ((_c = (_b = opf.Metadata) === null || _b === void 0 ? void 0 : _b.XMetadata) === null || _c === void 0 ? void 0 : _c.Meta)) {
m = opf.Metadata.XMetadata.Meta.find(func1);
}
if (m) {
let titleDisplaySeq = 0;
const func2 = (meta) => {
if (meta.Property === "display-seq" && meta.Refine === refineID) {
return true;
}
return false;
};
let mds = ((_d = opf.Metadata) === null || _d === void 0 ? void 0 : _d.Meta) ? opf.Metadata.Meta.find(func2) : undefined;
if (!mds && ((_f = (_e = opf.Metadata) === null || _e === void 0 ? void 0 : _e.XMetadata) === null || _f === void 0 ? void 0 : _f.Meta)) {
mds = opf.Metadata.XMetadata.Meta.find(func2);
}
if (mds) {
try {
titleDisplaySeq = parseInt(mds.Data, 10);
}
catch (err) {
debug(err);
debug(mds.Data);
titleDisplaySeq = 0;
}
if (isNaN(titleDisplaySeq)) {
debug("NaN");
debug(mds.Data);
titleDisplaySeq = 0;
}
}
else {
titleDisplaySeq = 0;
}
if (!subTitle || titleDisplaySeq < subTitleDisplaySeq) {
subTitle = title;
subTitleDisplaySeq = titleDisplaySeq;
}
}
});
}
if (!mainTitle) {
mainTitle = opfMetadataTitle[0];
}
}
if (mainTitle) {
const metaAlt = (0, exports.findAllMetaByRefineAndProperty)(opf, mainTitle.ID, "alternate-script");
if (metaAlt && metaAlt.length) {
publication.Metadata.Title = {};
metaAlt.forEach((m) => {
if (m.Lang) {
publication.Metadata.Title[m.Lang.toLowerCase()] = m.Data;
}
});
const xmlLang = mainTitle.Lang || opf.Lang;
if (xmlLang) {
publication.Metadata.Title[xmlLang.toLowerCase()] = mainTitle.Data;
}
else if (publication.Metadata.Language &&
publication.Metadata.Language.length &&
!publication.Metadata.Title[publication.Metadata.Language[0].toLowerCase()]) {
publication.Metadata.Title[publication.Metadata.Language[0].toLowerCase()] = mainTitle.Data;
}
else {
publication.Metadata.Title[exports.BCP47_UNKNOWN_LANG] = mainTitle.Data;
}
}
else {
const xmlLang = mainTitle.Lang || opf.Lang;
const isLangOverride = mainTitle.Lang && opf.Lang && mainTitle.Lang !== opf.Lang;
if (xmlLang && (isLangOverride || (0, exports.langStringIsRTL)(xmlLang.toLowerCase()))) {
publication.Metadata.Title = {};
publication.Metadata.Title[xmlLang.toLowerCase()] = mainTitle.Data;
}
else {
publication.Metadata.Title = mainTitle.Data;
}
}
}
if (subTitle) {
const metaAlt = (0, exports.findAllMetaByRefineAndProperty)(opf, subTitle.ID, "alternate-script");
if (metaAlt && metaAlt.length) {
publication.Metadata.SubTitle = {};
metaAlt.forEach((m) => {
if (m.Lang) {
publication.Metadata.SubTitle[m.Lang.toLowerCase()] = m.Data;
}
});
const xmlLang = subTitle.Lang || opf.Lang;
if (xmlLang) {
publication.Metadata.SubTitle[xmlLang.toLowerCase()] = subTitle.Data;
}
else if (publication.Metadata.Language &&
publication.Metadata.Language.length &&
!publication.Metadata.SubTitle[publication.Metadata.Language[0].toLowerCase()]) {
publication.Metadata.SubTitle[publication.Metadata.Language[0].toLowerCase()] = subTitle.Data;
}
else {
publication.Metadata.SubTitle[exports.BCP47_UNKNOWN_LANG] = subTitle.Data;
}
}
else {
const xmlLang = subTitle.Lang || opf.Lang;
const isLangOverride = subTitle.Lang && opf.Lang && subTitle.Lang !== opf.Lang;
if (xmlLang && (isLangOverride || (0, exports.langStringIsRTL)(xmlLang.toLowerCase()))) {
publication.Metadata.SubTitle = {};
publication.Metadata.SubTitle[xmlLang.toLowerCase()] = subTitle.Data;
}
else {
publication.Metadata.SubTitle = subTitle.Data;
}
}
}
}
else {
if (opfMetadataTitle) {
const xmlLang = opfMetadataTitle[0].Lang || opf.Lang;
const isLangOverride = opfMetadataTitle[0].Lang && opf.Lang && opfMetadataTitle[0].Lang !== opf.Lang;
if (xmlLang && (isLangOverride || (0, exports.langStringIsRTL)(xmlLang.toLowerCase()))) {
publication.Metadata.Title = {};
publication.Metadata.Title[xmlLang.toLowerCase()] = opfMetadataTitle[0].Data;
}
else {
publication.Metadata.Title = opfMetadataTitle[0].Data;
}
}
}
};
exports.addTitle = addTitle;
const setPublicationDirection = (publication, opf) => {
if (opf.Spine && opf.Spine.PageProgression) {
switch (opf.Spine.PageProgression) {
case "auto": {
publication.Metadata.Direction = metadata_1.DirectionEnum.Auto;
break;
}
case "ltr": {
publication.Metadata.Direction = metadata_1.DirectionEnum.LTR;
break;
}
case "rtl": {
publication.Metadata.Direction = metadata_1.DirectionEnum.RTL;
break;
}
}
}
if (publication.Metadata.Language && publication.Metadata.Language.length &&
(!publication.Metadata.Direction || publication.Metadata.Direction === metadata_1.DirectionEnum.Auto)) {
const lang = publication.Metadata.Language[0].toLowerCase();
if ((0, exports.langStringIsRTL)(lang)) {
publication.Metadata.Direction = metadata_1.DirectionEnum.RTL;
}
}
};
exports.setPublicationDirection = setPublicationDirection;
const langStringIsRTL = (lang) => {
return lang === "ar" || lang.startsWith("ar-") ||
lang === "he" || lang.startsWith("he-") ||
lang === "fa" || lang.startsWith("fa-");
};
exports.langStringIsRTL = langStringIsRTL;
const getNcx = (ncxManItem, opf, zip) => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
if (!opf.ZipPath) {
return Promise.reject("?!!opf.ZipPath");
}
const dname = path.dirname(opf.ZipPath);
const ncxManItemHrefDecoded = ncxManItem.HrefDecoded;
if (!ncxManItemHrefDecoded) {
return Promise.reject("?!ncxManItem.Href");
}
const ncxFilePath = path.join(dname, ncxManItemHrefDecoded).replace(/\\/g, "/");
const has = yield (0, zipHasEntry_1.zipHasEntry)(zip, ncxFilePath, undefined);
if (!has) {
const err = `NOT IN ZIP (NCX): ${ncxManItem.Href} --- ${ncxFilePath}`;
debug(err);
const zipEntries = yield zip.getEntries();
for (const zipEntry of zipEntries) {
if (zipEntry.startsWith("__MACOSX/")) {
continue;
}
debug(zipEntry);
}
return Promise.reject(err);
}
let ncxZipStream_;
try {
ncxZipStream_ = yield zip.entryStreamPromise(ncxFilePath);
}
catch (err) {
debug(err);
return Promise.reject(err);
}
const ncxZipStream = ncxZipStream_.stream;
let ncxZipData;
try {
ncxZipData = yield (0, BufferUtils_1.streamToBufferPromise)(ncxZipStream);
}
catch (err) {
debug(err);
return Promise.reject(err);
}
const ncxStr = (0, bom_1.removeUTF8BOM)(ncxZipData.toString("utf8"));
return (0, exports.getNcx_)(ncxStr, ncxFilePath);
});
exports.getNcx = getNcx;
const getNcx_ = (ncxStr, ncxFilePath) => {
const iStart = ncxStr.indexOf("<ncx");
if (iStart >= 0) {
const iEnd = ncxStr.indexOf(">", iStart);
if (iEnd > iStart) {
const clip = ncxStr.substr(iStart, iEnd - iStart);
if (clip.indexOf("xmlns") < 0) {
ncxStr = ncxStr.replace(/<ncx/, "<ncx xmlns=\"http://www.daisy.org/z3986/2005/ncx/\" ");
}
}
}
const ncxDoc = new xmldom.DOMParser().parseFromString(ncxStr, "application/xml");
const ncx = xml_js_mapper_1.XML.deserialize(ncxDoc, ncx_1.NCX);
ncx.ZipPath = ncxFilePath;
return ncx;
};
exports.getNcx_ = getNcx_;
const getOpf = (zip, rootfilePathDecoded, rootfilePath) => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
const has = yield (0, zipHasEntry_1.zipHasEntry)(zip, rootfilePathDecoded, rootfilePath);
if (!has) {
const err = `NOT IN ZIP (container OPF rootfile): ${rootfilePath} --- ${rootfilePathDecoded}`;
debug(err);
const zipEntries = yield zip.getEntries();
for (const zipEntry of zipEntries) {
if (zipEntry.startsWith("__MACOSX/")) {
continue;
}
debug(zipEntry);
}
return Promise.reject(err);
}
let opfZipStream_;
try {
opfZipStream_ = yield zip.entryStreamPromise(rootfilePathDecoded);
}
catch (err) {
debug(err);
return Promise.reject(err);
}
const opfZipStream = opfZipStream_.stream;
let opfZipData;
try {
opfZipData = yield (0, BufferUtils_1.streamToBufferPromise)(opfZipStream);
}
catch (err) {
debug(err);
return Promise.reject(err);
}
const opfStr = (0, bom_1.removeUTF8BOM)(opfZipData.toString("utf8"));
return (0, exports.getOpf_)(opfStr, rootfilePathDecoded);
});
exports.getOpf = getOpf;
const getOpf_ = (opfStr, rootfilePathDecoded) => {
const iStart = opfStr.indexOf("<package");
if (iStart >= 0) {
const iEnd = opfStr.indexOf(">", iStart);
if (iEnd > iStart) {
const clip = opfStr.substr(iStart, iEnd - iStart);
if (clip.indexOf("xmlns") < 0) {
opfStr = opfStr.replace(/<package/, "<package xmlns=\"http://openebook.org/namespaces/oeb-package/1.0/\" ");
}
}
}
const opfDoc = new xmldom.DOMParser().parseFromString(opfStr, "application/xml");
const opf = xml_js_mapper_1.XML.deserialize(opfDoc, opf_1.OPF);
opf.ZipPath = rootfilePathDecoded;
return opf;
};
exports.getOpf_ = getOpf_;
const addOtherMetadata = (publication, rootfile, opf) => {
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r, _s, _t, _u, _v, _w, _x, _y, _z, _0, _1, _2, _3, _4, _5, _6, _7, _8, _9;
if (!opf.Metadata) {
return;
}
const opfMetadataRights = ((_c = (_b = (_a = opf.Metadata) === null || _a === void 0 ? void 0 : _a.DCMetadata) === null || _b === void 0 ? void 0 : _b.Rights) === null || _c === void 0 ? void 0 : _c.length) ?
opf.Metadata.DCMetadata.Rights :
(((_e = (_d = opf.Metadata) === null || _d === void 0 ? void 0 : _d.Rights) === null || _e === void 0 ? void 0 : _e.length) ?
opf.Metadata.Rights :
undefined);
if (opfMetadataRights) {
publication.Metadata.Rights = opfMetadataRights.join(" ");
}
const opfMetadataDescription = ((_h = (_g = (_f = opf.Metadata) === null || _f === void 0 ? void 0 : _f.DCMetadata) === null || _g === void 0 ? void 0 : _g.Description) === null || _h === void 0 ? void 0 : _h.length) ?
opf.Metadata.DCMetadata.Description :
(((_k = (_j = opf.Metadata) === null || _j === void 0 ? void 0 : _j.Description) === null || _k === void 0 ? void 0 : _k.length) ?
opf.Metadata.Description :
undefined);
if (opfMetadataDescription) {
publication.Metadata.Description = opfMetadataDescription[0];
}
const opfMetadataPublisher = ((_o = (_m = (_l = opf.Metadata) === null || _l === void 0 ? void 0 : _l.DCMetadata) === null || _m === void 0 ? void 0 : _m.Publisher) === null || _o === void 0 ? void 0 : _o.length) ?
opf.Metadata.DCMetadata.Publisher :
(((_q = (_p = opf.Metadata) === null || _p === void 0 ? void 0 : _p.Publisher) === null || _q === void 0 ? void 0 : _q.length) ?
opf.Metadata.Publisher :
undefined);
if (opfMetadataPublisher) {
publication.Metadata.Publisher = [];
opfMetadataPublisher.forEach((pub) => {
const contrib = new metadata_contributor_1.Contributor();
contrib.Name = pub;
publication.Metadata.Publisher.push(contrib);
});
}
const opfMetadataSource = ((_t = (_s = (_r = opf.Metadata) === null || _r === void 0 ? void 0 : _r.DCMetadata) === null || _s === void 0 ? void 0 : _s.Source) === null || _t === void 0 ? void 0 : _t.length) ?
opf.Metadata.DCMetadata.Source :
(((_v = (_u = opf.Metadata) === null || _u === void 0 ? void 0 : _u.Source) === null || _v === void 0 ? void 0 : _v.length) ?
opf.Metadata.Source :
undefined);
if (opfMetadataSource) {
publication.Metadata.Source = opfMetadataSource[0];
}
const opfMetadataContributor = ((_y = (_x = (_w = opf.Metadata) === null || _w === void 0 ? void 0 : _w.DCMetadata) === null || _x === void 0 ? void 0 : _x.Contributor) === null || _y === void 0 ? void 0 : _y.length) ?
opf.Metadata.DCMetadata.Contributor :
(((_0 = (_z = opf.Metadata) === null || _z === void 0 ? void 0 : _z.Contributor) === null || _0 === void 0 ? void 0 : _0.length) ?
opf.Metadata.Contributor :
undefined);
if (opfMetadataContributor) {
opfMetadataContributor.forEach((cont) => {
(0, exports.addContributor)(publication, rootfile, opf, cont, undefined);
});
}
const opfMetadataCreator = ((_3 = (_2 = (_1 = opf.Metadata) === null || _1 === void 0 ? void 0 : _1.DCMetadata) === null || _2 === void 0 ? void 0 : _2.Creator) === null || _3 === void 0 ? void 0 : _3.length) ?
opf.Metadata.DCMetadata.Creator :
(((_5 = (_4 = opf.Metadata) === null || _4 === void 0 ? void 0 : _4.Creator) === null || _5 === void 0 ? void 0 : _5.length) ?
opf.Metadata.Creator :
undefined);
if (opfMetadataCreator) {
opfMetadataCreator.forEach((cont) => {
(0, exports.addContributor)(publication, rootfile, opf, cont, "aut");
});
}
if ((_6 = opf.Metadata) === null || _6 === void 0 ? void 0 : _6.Link) {
opf.Metadata.Link.forEach((metaLink) => {
if (metaLink.Property === "a11y:certifierCredential" ||
metaLink.Rel === "a11y:certifierCredential") {
let val = metaLink.Href;
if (!val) {
return;
}
val = val.trim();
if (!val) {
return;
}
if (!publication.Metadata.CertifierCredential) {
publication.Metadata.CertifierCredential = [];
}
publication.Metadata.CertifierCredential.push(val);
if (!publication.Metadata.Accessibility) {
publication.Metadata.Accessibility = {};
}
if (!publication.Metadata.Accessibility.Certification) {
publication.Metadata.Accessibility.Certification = {};
}
if (!publication.Metadata.Accessibility.Certification.Credential) {
publication.Metadata.Accessibility.Certification.Credential = [];
}
publication.Metadata.Accessibility.Certification.Credential.push(val);
}
else if (metaLink.Property === "a11y:certifierReport" ||
metaLink.Rel === "a11y:certifierReport") {
let val = metaLink.Href;
if (!val) {
return;
}
val = val.trim();
if (!val) {
return;
}
if (!publication.Metadata.CertifierReport) {
publication.Metadata.CertifierReport = [];
}
publication.Metadata.CertifierReport.push(val);
if (!publication.Metadata.Accessibility) {
publication.Metadata.Accessibility = {};
}
if (!publication.Metadata.Accessibility.Certification) {
publication.Metadata.Accessibility.Certification = {};
}
if (!publication.Metadata.Accessibility.Certification.Report) {
publication.Metadata.Accessibility.Certification.Report = [];
}
publication.Metadata.Accessibility.Certification.Report.push(val);
}
else if (metaLink.Property === "dcterms:conformsTo" ||
metaLink.Rel === "dcterms:conformsTo") {
let val = metaLink.Href;
if (!val) {
return;
}
val = val.trim();
if (!val) {
return;
}
if (!publication.Metadata.ConformsTo) {
publication.Metadata.ConformsTo = [];
}
publication.Metadata.ConformsTo.push(val);
if (!publication.Metadata.Accessibility) {
publication.Metadata.Accessibility = {};
}
if (!publication.Metadata.Accessibility.ConformsTo) {
publication.Metadata.Accessibility.ConformsTo = [];
}
publication.Metadata.Accessibility.ConformsTo.push(val);
}
});
}
if (opf.Metadata.Meta || ((_7 = opf.Metadata.XMetadata) === null || _7 === void 0 ? void 0 : _7.Meta)) {
const AccessibilitySummarys = [];
const metaFunc = (metaTag) => {
if (metaTag.Name === "schema:accessMode" ||
metaTag.Property === "schema:accessMode") {
let val = metaTag.Property ? metaTag.Data : metaTag.Content;
if (!val) {
return;
}
val = val.trim();
if (!val) {
return;
}
if (!publication.Metadata.AccessMode) {
publication.Metadata.AccessMode = [];
}
publication.Metadata.AccessMode.push(val);
if (!publication.Metadata.Accessibility) {
publication.Metadata.Accessibility = {};
}
if (!publication.Metadata.Accessibility.AccessMode) {
publication.Metadata.Accessibility.AccessMode = [];
}
publication.Metadata.Accessibility.AccessMode.push(val);
}
else if (metaTag.Name === "schema:accessibilityFeature" ||
metaTag.Property === "schema:accessibilityFeature") {
let val = metaTag.Property ? metaTag.Data : metaTag.Content;
if (!val) {
return;
}
val = val.trim();
if (!val) {
return;
}
if (!publication.Metadata.AccessibilityFeature) {
publication.Metadata.AccessibilityFeature = [];
}
publication.Metadata.AccessibilityFeature.push(val);
if (!publication.Metadata.Accessibility) {
publication.Metadata.Accessibility = {};
}
if (!publication.Metadata.Accessibility.Feature) {
publication.Metadata.Accessibility.Feature = [];
}
publication.Metadata.Accessibility.Feature.push(val);
}
else if (metaTag.Name === "schema:accessibilityHazard" ||
metaTag.Property === "schema:accessibilityHazard") {
let val = metaTag.Property ? metaTag.Data : metaTag.Content;
if (!val) {
return;
}
val = val.trim();
if (!val) {
return;
}
if (!publication.Metadata.AccessibilityHazard) {
publication.Metadata.AccessibilityHazard = [];
}
publication.Metadata.AccessibilityHazard.push(val);
if (!publication.Metadata.Accessibility) {
publication.Metadata.Accessibility = {};
}
if (!publication.Metadata.Accessibility.Hazard) {
publication.Metadata.Accessibility.Hazard = [];
}
publication.Metadata.Accessibility.Hazard.push(val);
}
else if (metaTag.Name === "schema:accessibilitySummary" ||
metaTag.Property === "schema:accessibilitySummary") {
let val = metaTag.Property ? metaTag.Data : metaTag.Content;
if (!val) {
return;
}
val = val.trim();
if (!val) {
return;
}
AccessibilitySummarys.push({
metaTag,
val,
});
}
else if (metaTag.Name === "schema:accessModeSufficient" ||
metaTag.Property === "schema:accessModeSufficient") {
let val = metaTag.Property ? metaTag.Data : metaTag.Content;
if (!val) {
return;
}
val = val.trim();
if (!val) {
return;
}
const del = (0, ta_json_string_tokens_converter_1.DelinearizeAccessModeSufficient)(val);
if (!publication.Metadata.AccessModeSufficient) {
publication.Metadata.AccessModeSufficient = [];
}
publication.Metadata.AccessModeSufficient.push(del);
if (!publication.Metadata.Accessibility) {
publication.Metadata.Accessibility = {};
}
if (!publication.Metadata.Accessibility.AccessModeSufficient) {
publication.Metadata.Accessibility.AccessModeSufficient = [];
}
publication.Metadata.Accessibility.AccessModeSufficient.push(del);
}
else if (metaTag.Name === "schema:accessibilityAPI" ||
metaTag.Property === "schema:accessibilityAPI") {
let val = metaTag.Property ? metaTag.Data : metaTag.Content;
if (!val) {
return;
}
val = val.trim();
if (!val) {
return;
}
if (!publication.Metadata.AccessibilityAPI) {
publication.Metadata.AccessibilityAPI = [];
}
publication.Metadata.AccessibilityAPI.push(val);
}
else if (metaTag.Name === "schema:accessibilityControl" ||
metaTag.Property === "schema:accessibilityControl") {
let val = metaTag.Property ? metaTag.Data : metaTag.Content;
if (!val) {
return;
}
val = val.trim();
if (!val) {
return;
}
if (!publication.Metadata.AccessibilityControl) {
publication.Metadata.AccessibilityControl = [];
}
publication.Metadata.AccessibilityControl.push(val);
}
else if (metaTag.Name === "a11y:certifiedBy" ||
metaTag.Property === "a11y:certifiedBy") {
let val = metaTag.Property ? metaTag.Data : metaTag.Content;
if (!val) {
return;
}
val = val.trim();
if (!val) {
return;
}
if (!publication.Metadata.CertifiedBy) {
publication.Metadata.CertifiedBy = [];
}
publication.Metadata.CertifiedBy.push(val);
if (!publication.Metadata.Accessibility) {
publication.Metadata.Accessibility = {};
}
if (!publication.Metadata.Accessibility.Certification) {
publication.Metadata.Accessibility.Certification = {};
}
if (!publication.Metadata.Accessibility.Certification.CertifiedBy) {
publication.Metadata.Accessibility.Certification.CertifiedBy = [];
}
publication.Metadata.Accessibility.Certification.CertifiedBy.push(val);
}
else if (metaTag.Name === "a11y:certifierCredential" ||
metaTag.Property === "a11y:certifierCredential") {
let val = metaTag.Property ? metaTag.Data : metaTag.Content;
if (!val) {
return;
}
val = val.trim();
if (!val) {