Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Parsed Dictionary Attributes for writing systems #725

Merged
merged 5 commits into from
Nov 18, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 2 additions & 6 deletions config/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ export type AudioConfig = {
};

export type WritingSystemConfig = {
type: string;
displayNames: {
[key: string]: string;
};
Expand All @@ -100,12 +101,7 @@ export type WritingSystemConfig = {
};

export type DictionaryWritingSystemConfig = WritingSystemConfig & {
code: string;
type: string;
trait?: {
[key: string]: string;
};
sortingMethod: {
sortMethod: {
type: string;
ignoreChars?: string[];
};
Expand Down
157 changes: 104 additions & 53 deletions convert/convertConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@ import type {
BookCollectionConfig,
BookCollectionAudioConfig,
StyleConfig,
DictionaryConfig
DictionaryConfig,
AppConfig,
WritingSystemConfig,
DictionaryWritingSystemConfig
} from '$config';

const fontFamilies: string[] = [];
Expand Down Expand Up @@ -255,6 +258,18 @@ function convertConfig(dataDir: string, verbose: number) {
(bc) => bc.books.filter((b) => b.type === 'glossary').length > 0
).length > 0;
}
if(isDictionaryConfig(data)){
const writingSystems: {[key: string] : DictionaryWritingSystemConfig} = {};
const writingSystemsTag = document.getElementsByTagName('writing-systems')[0];
const writingSystemTags = writingSystemsTag.getElementsByTagName('writing-system');
for(const tag of writingSystemTags){
const writingSystem = parseDictionaryWritingSystem(tag, verbose);
const code : string = tag.attributes.getNamedItem('code')!.value;
writingSystems[code] = writingSystem;
}

data.writingSystems = writingSystems;
}

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

Expand Down Expand Up @@ -651,71 +666,25 @@ function parseBookCollections(document: Document, verbose: number) {

function parseInterfaceLanguages(
document: Document,
data: ScriptureConfig | DictionaryConfig,
data: AppConfig,
verbose: number
) {
const interfaceLanguagesTag = document.getElementsByTagName('interface-languages')[0];
const useSystemLanguage = parseTrait(interfaceLanguagesTag, 'use-system-language') === 'true';
const interfaceLanguages: {
useSystemLanguage: boolean;
writingSystems: { [key: string]: any };
writingSystems: { [key: string]: WritingSystemConfig };
} = { useSystemLanguage, writingSystems: {} };

const writingSystemsTags = interfaceLanguagesTag
.getElementsByTagName('writing-systems')[0]
.getElementsByTagName('writing-system');

for (const tag of writingSystemsTags) {
const code: string = tag.attributes.getNamedItem('code')!.value;
const fontFamily = tag.getElementsByTagName('font-family')[0].innerHTML;
const textDirection = parseTrait(tag, 'text-direction');
const displaynamesTag = tag.getElementsByTagName('display-names')[0];
const displayNames: Record<string, string> = {};

for (const form of displaynamesTag.getElementsByTagName('form')) {
displayNames[form.attributes.getNamedItem('lang')!.value] = form.innerHTML;
}

const writingSystemConfig: any = {
fontFamily,
textDirection,
displayNames
};

if (isDictionaryConfig(data)) {
const sortMethodTag = tag.getElementsByTagName('sort-method')[0];
const sortMethod = {
ignoreChars: Array.from(sortMethodTag?.getElementsByTagName('ignore') || []).map(
(ignore) => ignore.innerHTML
)
};

const alphabetTag = tag.getElementsByTagName('alphabet')[0];
const alphabet = Array.from(alphabetTag?.getElementsByTagName('letter') || []).map(
(letter) => letter.innerHTML
);

const inputButtonsTag = tag.getElementsByTagName('input-buttons')[0];
const inputButtons = Array.from(
inputButtonsTag?.getElementsByTagName('button') || []
).map((button) => button.innerHTML);
// Optional features
const featuresTag = tag.getElementsByTagName('features')[0];
const features: Record<string, any> = {};
if (featuresTag) {
for (const feature of featuresTag.getElementsByTagName('feature')) {
features[feature.getAttribute('name')!] = feature.getAttribute('value')!;
}
}

// Add DAB-specific fields to the writing system config
writingSystemConfig.sortMethod = sortMethod;
writingSystemConfig.alphabet = alphabet;
writingSystemConfig.inputButtons = inputButtons;
writingSystemConfig.features = features;
}
interfaceLanguages.writingSystems[code] = writingSystemConfig;

const code : string = tag.attributes.getNamedItem('code')!.value;
const writingSystem = parseWritingSystem(tag, verbose);
interfaceLanguages.writingSystems[code] = writingSystem;

if (verbose >= 2) {
console.log(`.. writing system ${code}`);
}
Expand All @@ -727,6 +696,88 @@ function parseInterfaceLanguages(
return interfaceLanguages;
}

function parseWritingSystem (element: Element, verbose: number) : WritingSystemConfig
{
const type = element.attributes.getNamedItem('type')!.value;
const fontFamily = element.getElementsByTagName('font-family')[0].innerHTML;
const textDirection = parseTrait(element, 'text-direction');
const displaynamesTag = element.getElementsByTagName('display-names')[0];
const displayNames: Record<string, string> = {};
for (const form of displaynamesTag.getElementsByTagName('form')) {
displayNames[form.attributes.getNamedItem('lang')!.value] = form.innerHTML;
}
const writingSystem: WritingSystemConfig = {
type,
fontFamily,
textDirection,
displayNames
};

return writingSystem;
}
function parseDictionaryWritingSystem(
element: Element,
verbose: number
): DictionaryWritingSystemConfig {
const writingSystemConfig = parseWritingSystem(element, verbose);

let sortMethod: { type: string; ignoreChars?: string[] };
const sortMethodTag = element.getElementsByTagName('sort-method')[0];
if (sortMethodTag) {
const type = sortMethodTag.attributes.getNamedItem('type')?.value;
const ignoreCharsTag = sortMethodTag.getElementsByTagName('ignore-chars')[0];
const ignoreChars = ignoreCharsTag
? ignoreCharsTag.textContent?.split(/\s+/).filter((char) => char)
: undefined;
sortMethod = { type: type || "default", ignoreChars };
} else {
sortMethod = { type: "default" };
}

let alphabet: string[] | undefined;
const alphabetTag = element.getElementsByTagName('alphabet')[0];
if (alphabetTag) {
alphabet = alphabetTag.textContent?.split(/\s+/).filter((letter) => letter);
}

let inputButtons: string[] | undefined;
const inputButtonsTag = element.getElementsByTagName('input-buttons')[0];
if (inputButtonsTag) {
inputButtons = inputButtonsTag.textContent
?.split(/\s+/)
.filter((button) => button);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is the .filter there?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These .filter calls are not needed.

}

// Parse the features
let features: Record<string, boolean> | undefined;
const featuresTag = element.getElementsByTagName('features')[0];
if (featuresTag) {
features = {};
for (const feature of featuresTag.getElementsByTagName('e')) {
const name = feature.getAttribute('name');
const value = feature.getAttribute('value');
if (name && value) {
features[name] = value === 'true';
}
}
}
let reversalFilename: string | undefined;
const reversalFilenameTag = element.getElementsByTagName('reversal-filename')[0];
if (reversalFilenameTag) {
reversalFilename = reversalFilenameTag.textContent?.trim();
}

return {
...writingSystemConfig,
sortMethod,
alphabet,
inputButtons,
features,
reversalFilename,
};
}


function parseMenuLocalizations(document: Document, verbose: number) {
const translationMappingsTags = document.getElementsByTagName('translation-mappings');
let translationMappings: {
Expand Down
Loading