Skip to content

Commit

Permalink
Continued converting functions
Browse files Browse the repository at this point in the history
Issue #676
Stopped at parsedLayouts
  • Loading branch information
Christopher-Shiu committed Nov 7, 2024
1 parent cce2e51 commit e74a996
Showing 1 changed file with 40 additions and 24 deletions.
64 changes: 40 additions & 24 deletions convert/convertConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { convertMarkdownsToHTML } from './convertMarkdown';
import { splitVersion } from './stringUtils';
import type { ConfigData, BookCollectionData, BookCollectionAudioData, StyleData } from '$config';
import { ConvertFirebase } from 'convertFirebase';
import { transferableAbortSignal } from 'util';

const data: ConfigData = {};
const fontFamilies: string[] = [];
Expand Down Expand Up @@ -228,23 +229,23 @@ function convertConfig(dataDir: string, verbose: number) {
(bc) => bc.books.filter((b) => b.type === 'glossary').length > 0
).length > 0;

parseInterfaceLanguages(document, data, verbose);
data.interfaceLanguages = parseInterfaceLanguages(document, verbose);

parseMenuLocalizations(document, data, verbose);
data.translationMappings = parseMenuLocalizations(document, verbose);

parseKeys(document, data, verbose);
data.keys = parseKeys(document, verbose);

/* about?: string; */

parseAnalytics(document, data, verbose);
parseAnalytics(document, verbose);

parseFirebase(document, data, verbose);
parseFirebase(document, verbose);

parseAudioSources(document, data, verbose);
parseAudioSources(document, verbose);

parseVideos(document, data, verbose);
parseVideos(document);

parseIllustrations(document, data, verbose);
parseIllustrations(document, verbose);

parseLayouts(document, data, verbose);

Expand Down Expand Up @@ -584,9 +585,13 @@ function parseBookCollections(document: Document, verbose: number) {
return bookCollections;
}

function parseInterfaceLanguages(document: Document, data: any, verbose: number): void {
function parseInterfaceLanguages(document: Document,verbose: number) {
const interfaceLanguagesTag = document.getElementsByTagName('interface-languages')[0];
const useSystemLanguage = parseTrait(interfaceLanguagesTag, 'use-system-language') === 'true';
let interfaceLanguages = {
useSystemLanguage,
writingSystems: {}
};

data.interfaceLanguages = { useSystemLanguage, writingSystems: {} };

Expand All @@ -612,11 +617,12 @@ function parseInterfaceLanguages(document: Document, data: any, verbose: number)
}

if (verbose) console.log(`Converted ${Object.keys(data.interfaceLanguages.writingSystems).length} writing systems`);
return interfaceLanguages;
}

function parseMenuLocalizations(document: Document, data: any, verbose: number): void {
function parseMenuLocalizations(document: Document, verbose: number) {
const translationMappingsTags = document.getElementsByTagName('translation-mappings');

let translationMappings = { defaultLang: "", mappings: {} };
for (const translationMappingsTag of translationMappingsTags) {
const defaultLang = translationMappingsTag.attributes.getNamedItem('default-lang')!.value;
data.translationMappings = { defaultLang, mappings: {} };
Expand All @@ -642,20 +648,23 @@ function parseMenuLocalizations(document: Document, data: any, verbose: number):
);
}
}
return translationMappings;
}

function parseKeys(document: Document, data: any, verbose: number): void {
function parseKeys(document: Document, verbose: number){
let keys: string[] = [];
if (document.getElementsByTagName('keys').length > 0) {
data.keys = Array.from(
document.getElementsByTagName('keys')[0].getElementsByTagName('key')
).map((key) => key.innerHTML);
if (verbose) console.log(`Converted ${data.keys.length} keys`);
}
return keys;
}

function parseAnalytics(document: Document, data: any, verbose: number): void {
function parseAnalytics(document: Document, verbose: number) {
const analyticsElements = document.getElementsByTagName('analytics');

let analytics = { enabled: false, providers: [] };
// Initialize data.analytics if it doesn't exist
if (!data.analytics) {
data.analytics = {
Expand Down Expand Up @@ -702,11 +711,12 @@ function parseAnalytics(document: Document, data: any, verbose: number): void {
}

if (verbose) console.log(`Converted ${analyticsElements.length} analytics elements`);
return analytics;
}

function parseFirebase(document: Document, data: any, verbose: number): void {
function parseFirebase(document: Document, verbose: number){
const firebaseElements = document.getElementsByTagName('firebase');

let firebase = { features: {} };
// Initialize data.firebase if it doesn't exist
if (!data.firebase) {
data.firebase = {
Expand Down Expand Up @@ -734,12 +744,13 @@ function parseFirebase(document: Document, data: any, verbose: number): void {
}

if (verbose) console.log(`Converted ${firebaseElements.length} firebase elements`);
return firebase;
}

function parseAudioSources(document: Document, data: any, verbose: number): void {
function parseAudioSources(document: Document, verbose: number) {
const audioSources = document.getElementsByTagName('audio-sources')[0]
?.getElementsByTagName('audio-source');

let audio = { sources: {}, files: [] };
if (audioSources?.length > 0) {
data.audio = { sources: {} };

Expand Down Expand Up @@ -787,10 +798,12 @@ function parseAudioSources(document: Document, data: any, verbose: number): void
}
}
if (verbose) console.log(`Converted ${audioSources?.length} audio sources`);
return audio;
}

function parseVideos(document: Document, data: any, verbose: number): void {
function parseVideos(document: Document){
const videoTags = document.getElementsByTagName('videos')[0]?.getElementsByTagName('video');
let videos: any[] = [];
if (videoTags?.length > 0) {
data.videos = [];

Expand All @@ -813,7 +826,7 @@ function parseVideos(document: Document, data: any, verbose: number): void {

data.videos.push({
id: tag.getAttribute('id')!,
src: tag.getAttribute('src'),
src: tag.getAttribute('src') || '',
width,
height,
title: tag.getElementsByTagName('title')[0]?.innerHTML,
Expand All @@ -825,10 +838,12 @@ function parseVideos(document: Document, data: any, verbose: number): void {
}
}
data.traits['has-video'] = !!data.videos?.length;
return videos;
}

function parseIllustrations(document: Document, data: any, verbose: number): void {
function parseIllustrations(document: Document,verbose: number) {
const imagesTags = document.getElementsByTagName('images');
let illustrations: any[] = [];
if (imagesTags?.length > 0) {
data.illustrations = [];

Expand All @@ -843,10 +858,10 @@ function parseIllustrations(document: Document, data: any, verbose: number): voi

const placementTag = image.getElementsByTagName('placement')[0];
const placement = placementTag ? {
pos: placementTag.getAttribute('pos')!,
ref: placementTag.getAttribute('ref')?.split('|')[1],
pos: placementTag.getAttribute('pos')! || '',
ref: placementTag.getAttribute('ref')?.split('|')[1] || '',
caption: placementTag.getAttribute('caption') || '',
collection: placementTag.getAttribute('ref')?.split('|')[0]
collection: placementTag.getAttribute('ref')?.split('|')[0] || '',
} : undefined;

data.illustrations.push({ filename, width, height, placement });
Expand All @@ -855,6 +870,7 @@ function parseIllustrations(document: Document, data: any, verbose: number): voi
}
}
if (verbose) console.log(`Converted ${imagesTags?.length} illustrations`);
return illustrations;
}

function parseLayouts(document: Document, data: any, verbose: number): void {
Expand Down

0 comments on commit e74a996

Please sign in to comment.