From 902b766410fed38d7acaea7781b48e70497e7b49 Mon Sep 17 00:00:00 2001 From: pietervdvn Date: Fri, 18 Feb 2022 03:15:37 +0100 Subject: [PATCH] Add small script to autoconvert images to use tags instead --- UI/i18n/Translation.ts | 7 +++- scripts/fixImagesInTagRenderings.ts | 65 +++++++++++++++++++++++++++++ 2 files changed, 70 insertions(+), 2 deletions(-) create mode 100644 scripts/fixImagesInTagRenderings.ts diff --git a/UI/i18n/Translation.ts b/UI/i18n/Translation.ts index a068be199..30a0f74cb 100644 --- a/UI/i18n/Translation.ts +++ b/UI/i18n/Translation.ts @@ -130,13 +130,16 @@ export class Translation extends BaseUIElement { } public Subs(text: any): Translation { + return this.OnEveryLanguage((template, lang) => Utils.SubstituteKeys(template, text, lang)) + } + + public OnEveryLanguage(f: (s: string, language: string) => string): Translation { const newTranslations = {}; for (const lang in this.translations) { if (!this.translations.hasOwnProperty(lang)) { continue; } - let template: string = this.translations[lang]; - newTranslations[lang] = Utils.SubstituteKeys(template, text, lang); + newTranslations[lang] = f(this.translations[lang], lang); } return new Translation(newTranslations); diff --git a/scripts/fixImagesInTagRenderings.ts b/scripts/fixImagesInTagRenderings.ts new file mode 100644 index 000000000..be5e58e43 --- /dev/null +++ b/scripts/fixImagesInTagRenderings.ts @@ -0,0 +1,65 @@ +import {readFileSync, writeFileSync} from "fs"; +import {DesugaringStep} from "../Models/ThemeConfig/Conversion/Conversion"; +import {LayerConfigJson} from "../Models/ThemeConfig/Json/LayerConfigJson"; +import {Utils} from "../Utils"; +import Translations from "../UI/i18n/Translations"; + +class ConvertImagesToIcon extends DesugaringStep { + private _iconClass: string; + + constructor(iconClass: string) { + super("Searches for images in the 'then' path, removes the block and extracts the image itself a 'icon'", + [], "ConvertImagesToIcon") + this._iconClass = iconClass; + } + + convert(json: LayerConfigJson, context: string): { result: LayerConfigJson; errors?: string[]; warnings?: string[]; information?: string[] } { + const information = [] + const errors = [] + json = Utils.Clone(json) + Utils.WalkPath( + ["tagRenderings", "mappings"], + json, + mapping => { + const then = Translations.T(mapping.then) + const images = Utils.Dedup(then.ExtractImages()) + if (images.length == 0) { + return mapping + } + if (images.length > 1) { + errors.push("The mapping " + mapping.then + " has multiple images: " + images.join(", ")) + } + information.push("Replaced image " + images[0]) + const replaced = then.OnEveryLanguage((s) => { + return s.replace(/(
]*>)?]*> ?/, "").replace(/<\/div>$/, "") + }) + + mapping.then = replaced.translations + mapping.icon = {path: images[0], class: this._iconClass} + return mapping + } + ) + + return { + information, + result: json + }; + } +} + +/** + * One-of script to load one layer.json-file and rewrite all tagrenderings + */ +function main() { + let args = [...process.argv] + args.splice(0, 2) + const path = args[0] + const iconClass = args[1] ?? "small" + console.log("Fixing images in " + path) + const parsed = JSON.parse(readFileSync(path, "UTF8")) + const converted = new ConvertImagesToIcon(iconClass).convertStrict(parsed, "While running the fixImagesInTagRenderings-script") + writeFileSync(path + ".autoconverted.json", JSON.stringify(converted, null, " ")) + console.log("Written fixed version to " + path + ".autoconverted.json") +} + +main(); \ No newline at end of file