diff --git a/scripts/generateLayouts.ts b/scripts/generateLayouts.ts
index 58b716f42..adf069f5b 100644
--- a/scripts/generateLayouts.ts
+++ b/scripts/generateLayouts.ts
@@ -17,403 +17,420 @@ import * as eli_global from "../src/assets/global-raster-layers.json"
import ValidationUtils from "../src/Models/ThemeConfig/Conversion/ValidationUtils"
import { LayerConfigJson } from "../src/Models/ThemeConfig/Json/LayerConfigJson"
import { QuestionableTagRenderingConfigJson } from "../src/Models/ThemeConfig/Json/QuestionableTagRenderingConfigJson"
+import Script from "./Script"
+import crypto from "crypto"
const sharp = require("sharp")
-const template = readFileSync("theme.html", "utf8")
-let codeTemplate = readFileSync("src/index_theme.ts.template", "utf8")
-function enc(str: string): string {
- return encodeURIComponent(str.toLowerCase())
-}
+class GenerateLayouts extends Script {
+ private readonly template = readFileSync("theme.html", "utf8")
+ private readonly codeTemplate = readFileSync("src/index_theme.ts.template", "utf8")
+ private readonly removeOtherLanguages = readFileSync("src/UI/RemoveOtherLanguages.ts", "utf8")
+ .split("\n")
+ .slice(1)
+ .map((s) => s.trim())
+ .filter((s) => s !== "")
+ .join("\n")
+ private readonly removeOtherLanguagesHash =
+ "sha256-" + crypto.createHash("sha256").update(this.removeOtherLanguages).digest("base64")
+ private previousSrc: Set = new Set()
+ private eliUrlsCached: string[]
+ private date = new Date().toISOString()
-async function createIcon(iconPath: string, size: number, alreadyWritten: string[]) {
- let name = iconPath.split(".").slice(0, -1).join(".") // drop svg suffix
- if (name.startsWith("./")) {
- name = name.substr(2)
+ constructor() {
+ super("Generates an '.html' and 'index_.ts' for every theme")
}
- const newname = `assets/generated/images/${name.replace(/\//g, "_")}${size}.png`
- const targetpath = `public/${newname}`
- if (alreadyWritten.indexOf(newname) >= 0) {
- return newname
+ enc(str: string): string {
+ return encodeURIComponent(str.toLowerCase())
}
- alreadyWritten.push(newname)
- if (existsSync(targetpath)) {
+
+ async createIcon(iconPath: string, size: number, alreadyWritten: string[]) {
+ let name = iconPath.split(".").slice(0, -1).join(".") // drop svg suffix
+ if (name.startsWith("./")) {
+ name = name.substring(2)
+ }
+
+ const newname = `assets/generated/images/${name.replace(/\//g, "_")}${size}.png`
+ const targetpath = `public/${newname}`
+ if (alreadyWritten.indexOf(newname) >= 0) {
+ return newname
+ }
+ alreadyWritten.push(newname)
+ if (existsSync(targetpath)) {
+ return newname
+ }
+
+ if (!existsSync(iconPath)) {
+ throw "No file at " + iconPath
+ }
+
+ try {
+ // We already read to file, in order to crash here if the file is not found
+ let img = await sharp(iconPath)
+ let resized = await img.resize(size)
+ await resized.toFile(targetpath)
+ console.log("Created png version at ", newname)
+ } catch (e) {
+ console.error("Could not read icon", iconPath, " to create a PNG due to", e)
+ }
+
return newname
}
- if (!existsSync(iconPath)) {
- throw "No file at " + iconPath
- }
-
- try {
- // We already read to file, in order to crash here if the file is not found
- let img = await sharp(iconPath)
- let resized = await img.resize(size)
- await resized.toFile(targetpath)
- console.log("Created png version at ", newname)
- } catch (e) {
- console.error("Could not read icon", iconPath, " to create a PNG due to", e)
- }
-
- return newname
-}
-
-async function createSocialImage(layout: LayoutConfig, template: "" | "Wide"): Promise {
- if (!layout.icon.endsWith(".svg")) {
- console.warn(
- "Not creating a social image for " +
- layout.id +
- " as it is _not_ a .svg: " +
- layout.icon
+ async createSocialImage(layout: LayoutConfig, template: "" | "Wide"): Promise {
+ if (!layout.icon.endsWith(".svg")) {
+ console.warn(
+ "Not creating a social image for " +
+ layout.id +
+ " as it is _not_ a .svg: " +
+ layout.icon
+ )
+ return undefined
+ }
+ const path = `./public/assets/generated/images/social_image_${layout.id}_${template}.svg`
+ if (existsSync(path)) {
+ return path
+ }
+ const svg = await ScriptUtils.ReadSvg(layout.icon)
+ let width: string = svg.$.width
+ if (width === undefined) {
+ throw "The logo at " + layout.icon + " does not have a defined width"
+ }
+ if (width?.endsWith("px")) {
+ width = width.substring(0, width.length - 2)
+ }
+ if (width?.endsWith("%")) {
+ throw "The logo at " + layout.icon + " has a relative width; this is not supported"
+ }
+ delete svg["defs"]
+ delete svg["$"]
+ let templateSvg = await ScriptUtils.ReadSvg(
+ "./public/assets/SocialImageTemplate" + template + ".svg"
)
- return undefined
- }
- const path = `./public/assets/generated/images/social_image_${layout.id}_${template}.svg`
- if (existsSync(path)) {
+ templateSvg = Utils.WalkJson(
+ templateSvg,
+ (leaf) => {
+ const { cx, cy, r } = leaf["circle"][0].$
+ return {
+ $: {
+ id: "icon",
+ transform: `translate(${cx - r},${cy - r}) scale(${
+ (r * 2) / Number(width)
+ }) `,
+ },
+ g: [svg],
+ }
+ },
+ (mightBeTokenToReplace) => {
+ if (mightBeTokenToReplace?.circle === undefined) {
+ return false
+ }
+ return mightBeTokenToReplace.circle[0]?.$?.style?.indexOf("fill:#ff00ff") >= 0
+ }
+ )
+
+ const builder = new xml2js.Builder()
+ const xml = builder.buildObject({ svg: templateSvg })
+ writeFileSync(path, xml)
+ console.log("Created social image at ", path)
return path
}
- const svg = await ScriptUtils.ReadSvg(layout.icon)
- let width: string = svg.$.width
- if (width === undefined) {
- throw "The logo at " + layout.icon + " does not have a defined width"
- }
- if (width?.endsWith("px")) {
- width = width.substring(0, width.length - 2)
- }
- if (width?.endsWith("%")) {
- throw "The logo at " + layout.icon + " has a relative width; this is not supported"
- }
- delete svg["defs"]
- delete svg["$"]
- let templateSvg = await ScriptUtils.ReadSvg(
- "./public/assets/SocialImageTemplate" + template + ".svg"
- )
- templateSvg = Utils.WalkJson(
- templateSvg,
- (leaf) => {
- const { cx, cy, r } = leaf["circle"][0].$
- return {
- $: {
- id: "icon",
- transform: `translate(${cx - r},${cy - r}) scale(${(r * 2) / Number(width)}) `,
- },
- g: [svg],
+
+ async createManifest(
+ layout: LayoutConfig,
+ alreadyWritten: string[]
+ ): Promise<{
+ manifest: any
+ whiteIcons: string[]
+ }> {
+ Translation.forcedLanguage = "en"
+ const icons = []
+
+ const whiteIcons: string[] = []
+ let icon = layout.icon
+ if (icon.endsWith(".svg") || icon.startsWith("
-
+
-
+
-