From b87c939836a640839a10ee0517df7d64b351e31f Mon Sep 17 00:00:00 2001 From: Mikayla Date: Tue, 23 Jul 2024 15:37:59 -0400 Subject: [PATCH 1/2] parse plans in convertConfig starting implementation of issue #622 converts the plans section in appdef.xml to json --- convert/convertConfig.ts | 135 ++++++++++++++++++++++++++++++--------- 1 file changed, 105 insertions(+), 30 deletions(-) diff --git a/convert/convertConfig.ts b/convert/convertConfig.ts index 0f2f9fa36..dc4194860 100644 --- a/convert/convertConfig.ts +++ b/convert/convertConfig.ts @@ -221,6 +221,26 @@ export type ConfigData = { file: string; }[]; }[]; + + plans?: { + features: { + [key: string]: string; + }; + plans: { + id: string; + days: number; + title: { + [lang: string]: string; + }; + filename: string; + image?: { + width: number; + height: number; + file: string; + } + }[]; + }; + security?: { // TODO features?: { @@ -282,11 +302,11 @@ function parseStyles(stylesTag: Element, verbose: number) { if (verbose) console.log( 'Parsing ' + - propName + - ' = ' + - propValue + - ' -> ' + - properties[propName] + propName + + ' = ' + + propValue + + ' -> ' + + properties[propName] ); } else { //Not a sp value @@ -599,8 +619,8 @@ function convertConfig(dataDir: string, verbose: number) { const fontChoiceTag = book.querySelector('font-choice'); const fonts = fontChoiceTag ? Array.from(fontChoiceTag.getElementsByTagName('font-choice-family')) - .filter((x) => fontFamilies.includes(x.innerHTML)) - .map((x) => x.innerHTML) + .filter((x) => fontFamilies.includes(x.innerHTML)) + .map((x) => x.innerHTML) : []; const bkAdditionalNames = book.querySelector('additional-names'); const additionalNames = bkAdditionalNames @@ -648,8 +668,8 @@ function convertConfig(dataDir: string, verbose: number) { if (verbose >= 3) console.log(`.... fontChoice: `, JSON.stringify(fontChoiceTag)); const fonts = fontChoiceTag ? Array.from(fontChoiceTag.getElementsByTagName('font-choice-family')) - .filter((x) => fontFamilies.includes(x.innerHTML)) - .map((x) => x.innerHTML) + .filter((x) => fontFamilies.includes(x.innerHTML)) + .map((x) => x.innerHTML) : []; const writingSystem = tag.getElementsByTagName('writing-system')[0]; @@ -750,8 +770,7 @@ function convertConfig(dataDir: string, verbose: number) { } if (verbose) console.log( - `Converted ${ - Object.keys(data.translationMappings.mappings).length + `Converted ${Object.keys(data.translationMappings.mappings).length } translation mappings` ); } @@ -824,12 +843,12 @@ function convertConfig(dataDir: string, verbose: number) { placementTag == undefined ? undefined : { - pos: placementTag.attributes.getNamedItem('pos')!.value, - ref: placementTag.attributes.getNamedItem('ref')!.value.split('|')[1], - collection: placementTag.attributes - .getNamedItem('ref')! - .value.split('|')[0] - }; + pos: placementTag.attributes.getNamedItem('pos')!.value, + ref: placementTag.attributes.getNamedItem('ref')!.value.split('|')[1], + collection: placementTag.attributes + .getNamedItem('ref')! + .value.split('|')[0] + }; const tagWidth = tag.attributes.getNamedItem('width') ? parseInt(tag.attributes.getNamedItem('width')!.value) : 0; @@ -885,17 +904,17 @@ function convertConfig(dataDir: string, verbose: number) { placementTag == undefined ? undefined : { - pos: placementTag.attributes.getNamedItem('pos')!.value, - ref: placementTag.attributes - .getNamedItem('ref')! - .value.split('|')[1], - caption: placementTag.attributes.getNamedItem('caption') - ? placementTag.attributes.getNamedItem('caption')!.value - : '', - collection: placementTag.attributes - .getNamedItem('ref')! - .value.split('|')[0] - }; + pos: placementTag.attributes.getNamedItem('pos')!.value, + ref: placementTag.attributes + .getNamedItem('ref')! + .value.split('|')[1], + caption: placementTag.attributes.getNamedItem('caption') + ? placementTag.attributes.getNamedItem('caption')!.value + : '', + collection: placementTag.attributes + .getNamedItem('ref')! + .value.split('|')[0] + }; data.illustrations.push({ filename: filename, width: imageWidth, @@ -932,8 +951,8 @@ function convertConfig(dataDir: string, verbose: number) { const layoutCollections = layoutCollectionElements.length > 0 ? Array.from(layoutCollectionElements).map((element) => { - return element.attributes.getNamedItem('id')!.value; - }) + return element.attributes.getNamedItem('id')!.value; + }) : [data.bookCollections[0].id]; data.layouts.push({ @@ -1035,6 +1054,62 @@ function convertConfig(dataDir: string, verbose: number) { } } + //plans + const plansTags = document + .getElementsByTagName('plans'); + if (plansTags?.length > 0) { + const plansTag = plansTags[0]; + const featuresTag = plansTag.getElementsByTagName('features')[0]; + const features: { [key: string]: string } = {}; + if (featuresTag) { + for (const feature of featuresTag.getElementsByTagName('e')) { + const name = feature.attributes.getNamedItem('name')!.value; + const value = feature.attributes.getNamedItem('value')!.value; + if (verbose >= 2) + console.log(`.. Converting feature: name=${name}, value=${value}`); + features[name] = value; + } + } + + const planTags = plansTag.getElementsByTagName('plan'); + const plans = []; + if (planTags?.length > 0) { + for (const tag of planTags) { + const titleTags = tag.getElementsByTagName('title')[0].getElementsByTagName('t'); + const title: { [lang: string]: string } = {}; + for (const titleTag of titleTags) { + title[titleTag.attributes.getNamedItem('lang')!.value] = titleTag.innerHTML; + } + //image + const imageTag = tag + .getElementsByTagName('image')[0]; + + let image = undefined; + if (imageTag.innerHTML) { + image = { + width: Number(imageTag.attributes.getNamedItem('width')!.value), + height: Number(imageTag.attributes.getNamedItem('height')!.value), + file: imageTag.innerHTML + } + } + + + const plan = { + id: tag.attributes.getNamedItem('id')!.value, + days: Number(tag.attributes.getNamedItem('days')!.value), + title, + filename: tag.getElementsByTagName('filename')[0].innerHTML, + image + } + plans.push(plan); + } + data.plans = { + features, + plans + } + } + } + /* security?: { features?: { From 82f1164f0599f81969edf2ba4b32d95e4dc9b905 Mon Sep 17 00:00:00 2001 From: Mikayla Date: Tue, 23 Jul 2024 15:51:12 -0400 Subject: [PATCH 2/2] fix lint errors --- convert/convertConfig.ts | 74 +++++++++++++++++++--------------------- 1 file changed, 36 insertions(+), 38 deletions(-) diff --git a/convert/convertConfig.ts b/convert/convertConfig.ts index dc4194860..ef640862e 100644 --- a/convert/convertConfig.ts +++ b/convert/convertConfig.ts @@ -237,7 +237,7 @@ export type ConfigData = { width: number; height: number; file: string; - } + }; }[]; }; @@ -302,11 +302,11 @@ function parseStyles(stylesTag: Element, verbose: number) { if (verbose) console.log( 'Parsing ' + - propName + - ' = ' + - propValue + - ' -> ' + - properties[propName] + propName + + ' = ' + + propValue + + ' -> ' + + properties[propName] ); } else { //Not a sp value @@ -619,8 +619,8 @@ function convertConfig(dataDir: string, verbose: number) { const fontChoiceTag = book.querySelector('font-choice'); const fonts = fontChoiceTag ? Array.from(fontChoiceTag.getElementsByTagName('font-choice-family')) - .filter((x) => fontFamilies.includes(x.innerHTML)) - .map((x) => x.innerHTML) + .filter((x) => fontFamilies.includes(x.innerHTML)) + .map((x) => x.innerHTML) : []; const bkAdditionalNames = book.querySelector('additional-names'); const additionalNames = bkAdditionalNames @@ -668,8 +668,8 @@ function convertConfig(dataDir: string, verbose: number) { if (verbose >= 3) console.log(`.... fontChoice: `, JSON.stringify(fontChoiceTag)); const fonts = fontChoiceTag ? Array.from(fontChoiceTag.getElementsByTagName('font-choice-family')) - .filter((x) => fontFamilies.includes(x.innerHTML)) - .map((x) => x.innerHTML) + .filter((x) => fontFamilies.includes(x.innerHTML)) + .map((x) => x.innerHTML) : []; const writingSystem = tag.getElementsByTagName('writing-system')[0]; @@ -770,7 +770,8 @@ function convertConfig(dataDir: string, verbose: number) { } if (verbose) console.log( - `Converted ${Object.keys(data.translationMappings.mappings).length + `Converted ${ + Object.keys(data.translationMappings.mappings).length } translation mappings` ); } @@ -843,12 +844,12 @@ function convertConfig(dataDir: string, verbose: number) { placementTag == undefined ? undefined : { - pos: placementTag.attributes.getNamedItem('pos')!.value, - ref: placementTag.attributes.getNamedItem('ref')!.value.split('|')[1], - collection: placementTag.attributes - .getNamedItem('ref')! - .value.split('|')[0] - }; + pos: placementTag.attributes.getNamedItem('pos')!.value, + ref: placementTag.attributes.getNamedItem('ref')!.value.split('|')[1], + collection: placementTag.attributes + .getNamedItem('ref')! + .value.split('|')[0] + }; const tagWidth = tag.attributes.getNamedItem('width') ? parseInt(tag.attributes.getNamedItem('width')!.value) : 0; @@ -904,17 +905,17 @@ function convertConfig(dataDir: string, verbose: number) { placementTag == undefined ? undefined : { - pos: placementTag.attributes.getNamedItem('pos')!.value, - ref: placementTag.attributes - .getNamedItem('ref')! - .value.split('|')[1], - caption: placementTag.attributes.getNamedItem('caption') - ? placementTag.attributes.getNamedItem('caption')!.value - : '', - collection: placementTag.attributes - .getNamedItem('ref')! - .value.split('|')[0] - }; + pos: placementTag.attributes.getNamedItem('pos')!.value, + ref: placementTag.attributes + .getNamedItem('ref')! + .value.split('|')[1], + caption: placementTag.attributes.getNamedItem('caption') + ? placementTag.attributes.getNamedItem('caption')!.value + : '', + collection: placementTag.attributes + .getNamedItem('ref')! + .value.split('|')[0] + }; data.illustrations.push({ filename: filename, width: imageWidth, @@ -951,8 +952,8 @@ function convertConfig(dataDir: string, verbose: number) { const layoutCollections = layoutCollectionElements.length > 0 ? Array.from(layoutCollectionElements).map((element) => { - return element.attributes.getNamedItem('id')!.value; - }) + return element.attributes.getNamedItem('id')!.value; + }) : [data.bookCollections[0].id]; data.layouts.push({ @@ -1055,8 +1056,7 @@ function convertConfig(dataDir: string, verbose: number) { } //plans - const plansTags = document - .getElementsByTagName('plans'); + const plansTags = document.getElementsByTagName('plans'); if (plansTags?.length > 0) { const plansTag = plansTags[0]; const featuresTag = plansTag.getElementsByTagName('features')[0]; @@ -1081,8 +1081,7 @@ function convertConfig(dataDir: string, verbose: number) { title[titleTag.attributes.getNamedItem('lang')!.value] = titleTag.innerHTML; } //image - const imageTag = tag - .getElementsByTagName('image')[0]; + const imageTag = tag.getElementsByTagName('image')[0]; let image = undefined; if (imageTag.innerHTML) { @@ -1090,23 +1089,22 @@ function convertConfig(dataDir: string, verbose: number) { width: Number(imageTag.attributes.getNamedItem('width')!.value), height: Number(imageTag.attributes.getNamedItem('height')!.value), file: imageTag.innerHTML - } + }; } - const plan = { id: tag.attributes.getNamedItem('id')!.value, days: Number(tag.attributes.getNamedItem('days')!.value), title, filename: tag.getElementsByTagName('filename')[0].innerHTML, image - } + }; plans.push(plan); } data.plans = { features, plans - } + }; } }