more details

This commit is contained in:
Ulf Gebhardt 2023-04-10 02:01:25 +02:00
parent dbfc7fb6f6
commit 60b698bc48
Signed by: ulfgebhardt
GPG Key ID: DA6B843E748679C9

View File

@ -1,50 +1,89 @@
// TODO file handle? // TODO file handle?
export const parse = function(input: string) { import { FileHandle, open } from 'fs/promises';
var lines = input.split(/\r*\n/); //allow Windows newlines // const file = await fsPromise.open('./words.txt', 'r');
console.log('Got input, '+lines.length+' lines'); const TOC_PREFIX_TAG = '## '
var out = { const TOC_PREFIX_COMMENT = '#'
tags: {}, const TOC_TAG_SEPARATOR = ':'
files: [] const TOC_LINE_MAX_LENGTH = 1024
export type TOC = {/*interface: number, title: string, titleLocalized: Record<string,string>, xTags: Record<string, string>,*/ allTags: Record<string, string>,files: string[]}
export const parse = async function(file: FileHandle, strict = true) {
const result: TOC = {
// interface: 0,
// title: '',
// titleLocalized: {},
// xTags: {},
allTags: {},
files: [],
}; };
lines.forEach(function(line) { for await (const line of file.readLines()) {
if (line.length > 1024) { if (strict && line.length > TOC_LINE_MAX_LENGTH) {
//WoW only reads the first 1024 lines throw new Error('Line is longer than TOC_LINE_MAX_LENGTH characters, WoW will truncate!');
console.log('Line is longer than 1024 characters, trunicating to be like WoW');
line = line.substr(0, 1024);
} }
if (line.substr(0, 2) === '##') { //## Title: Recount if (line.slice(0, TOC_PREFIX_TAG.length) === TOC_PREFIX_TAG) {
console.log('Line is tag: '+line); const tagData = line.slice(TOC_PREFIX_TAG.length+1).split(TOC_TAG_SEPARATOR)
var tag = line.substr(2, line.indexOf(':')).trim().replace(':', ''); if(tagData.length !== 2){
var value = line.substr(line.indexOf(':')+1, line.length).trim(); throw new Error(`Tag could not be parsed: ${tagData}`)
console.log('`'+tag+'` : `'+value+'`');
out.tags[tag] = value;
} else if (line.substr(0, 1) === '#') { //#bug not feature
console.log('Line is comment: '+line);
} else { // recount.lua
//file loading
console.log('Line is file: '+line);
if (line.trim() !== '') {
out.files.push(line.trim());
} }
}
}); const tagName = tagData[0].trim()
return out; const tagValue = tagData[1].trim()
if(strict && result.allTags[tagName]){
throw new Error(`Duplicate value: ${tagName}`)
}
result.allTags[tagName] = tagValue;
// specific tags
/*switch(tagName.toLowerCase()){
case 'interface':
result.interface = Number(tagValue)
break;
case 'title':
result.title
break;
}*/
//
} else if (line.slice(0, 1) !== TOC_PREFIX_COMMENT && !line.trim()) {
result.files.push(line.trimEnd()));
}
}
return result;
}; };
export const stringify = function(input) { /*
if (typeof input !== 'object') { https://wowpedia.fandom.com/wiki/TOC_format
throw new TypeError('wowtoc.stringify input must be an object, got '+typeof input); ## Interface: 100007
} ## Title: Waiting for Godot
## Title-frFR: En attendant Godot
## Notes: This word is |cFFFF0000red|r
## IconTexture: Interface\Icons\TEMP
## IconAtlas: TaskPOI-Icon
// ## AddonCompartmentFunc: MyAddon_OnAddonCompartmentClick
// ## AddonCompartmentFuncOnEnter: MyAddon_OnAddonCompartmentEnter
// ## AddonCompartmentFuncOnLeave: MyAddon_OnAddonCompartmentLeave
## LoadOnDemand: 1
## Dependencies: someAddOn, someOtherAddOn
## OptionalDeps: someAddOn, someOtherAddOn
## LoadWith: someAddOn, someOtherAddOn
// ## LoadManagers: someAddOn, someOtherAddOn
## DefaultState: disabled
## SavedVariables: MyAddOnNameFoo, MyAddOnNameBar
## SavedVariablesPerCharacter: MyAddOnNameAnotherVariable
## Author
## Version
## X-_____
var out = []; */
for(var tag in input.tags) {
if(input.tags.hasOwnProperty(tag)) { export const stringify = function(toc: TOC) {
out.push('## '+tag+': '+input.tags[tag]); var result: string[] = [];
} for(const tag in toc.allTags) {
result.push(`${TOC_PREFIX_TAG}${tag}${TOC_TAG_SEPARATOR} ${toc.allTags[tag]}`)
} }
(input.files || []).forEach(function(file) { result.concat(toc.files)
out.push(file); return result.join('\n');
}); };
return out.join('\n');
};