2022-07-02 01:59:26 +02:00
|
|
|
/***
|
|
|
|
* Parses presets from the iD repository and extracts some usefull tags from them
|
|
|
|
*/
|
2022-09-08 21:40:48 +02:00
|
|
|
import ScriptUtils from "../ScriptUtils"
|
|
|
|
import { existsSync, readFileSync, writeFileSync } from "fs"
|
2023-02-08 01:14:21 +01:00
|
|
|
import known_languages from "../../assets/language_native.json"
|
2022-09-08 21:40:48 +02:00
|
|
|
import { LayerConfigJson } from "../../Models/ThemeConfig/Json/LayerConfigJson"
|
|
|
|
import { MappingConfigJson } from "../../Models/ThemeConfig/Json/QuestionableTagRenderingConfigJson"
|
|
|
|
import SmallLicense from "../../Models/smallLicense"
|
2022-07-02 01:59:26 +02:00
|
|
|
|
|
|
|
interface IconThief {
|
|
|
|
steal(iconName: string): boolean
|
|
|
|
}
|
|
|
|
|
2022-07-02 02:45:59 +02:00
|
|
|
interface IdPresetJson {
|
2022-09-08 21:40:48 +02:00
|
|
|
icon: string
|
2022-07-02 01:59:26 +02:00
|
|
|
geometry: ("point" | "line" | "area")[]
|
|
|
|
/**
|
|
|
|
* Extra search terms
|
|
|
|
*/
|
2022-09-08 21:40:48 +02:00
|
|
|
terms: string[]
|
2022-07-02 01:59:26 +02:00
|
|
|
tags: Record<string, string>
|
2022-09-08 21:40:48 +02:00
|
|
|
name: string
|
|
|
|
searchable?: boolean
|
2022-07-02 01:59:26 +02:00
|
|
|
}
|
|
|
|
|
2022-07-02 02:45:59 +02:00
|
|
|
class IdPreset implements IdPresetJson {
|
2022-09-08 21:40:48 +02:00
|
|
|
private _preset: IdPresetJson
|
2022-07-02 02:45:59 +02:00
|
|
|
|
|
|
|
constructor(preset: IdPresetJson) {
|
2022-09-08 21:40:48 +02:00
|
|
|
this._preset = preset
|
2022-07-02 02:45:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public get searchable(): boolean {
|
|
|
|
return this._preset.searchable
|
|
|
|
}
|
|
|
|
|
|
|
|
public get name() {
|
|
|
|
return this._preset.name
|
|
|
|
}
|
|
|
|
|
|
|
|
public get terms() {
|
|
|
|
return this._preset.terms
|
|
|
|
}
|
|
|
|
|
|
|
|
public get tags() {
|
|
|
|
return this._preset.tags
|
|
|
|
}
|
|
|
|
|
|
|
|
public get geometry() {
|
|
|
|
return this._preset.geometry
|
|
|
|
}
|
|
|
|
|
|
|
|
public get icon(): string {
|
|
|
|
return this._preset.icon
|
|
|
|
}
|
|
|
|
|
|
|
|
static fromFile(file: string): IdPreset {
|
2022-09-08 21:40:48 +02:00
|
|
|
return new IdPreset(JSON.parse(readFileSync(file, "utf8")))
|
2022-07-02 02:45:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public parseTags(): string | { and: string[] } {
|
2022-09-08 21:40:48 +02:00
|
|
|
const preset = this._preset
|
2022-07-02 02:45:59 +02:00
|
|
|
const tagKeys = Object.keys(preset.tags)
|
|
|
|
if (tagKeys.length === 1) {
|
|
|
|
return tagKeys[0] + "=" + preset.tags[tagKeys[0]]
|
|
|
|
} else {
|
|
|
|
return {
|
2022-09-08 21:40:48 +02:00
|
|
|
and: tagKeys.map((key) => key + "=" + preset.tags[key]),
|
2022-07-02 02:45:59 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class MakiThief implements IconThief {
|
2022-09-08 21:40:48 +02:00
|
|
|
public readonly _prefix: string
|
|
|
|
private readonly _directory: string
|
|
|
|
private readonly _license: SmallLicense
|
|
|
|
private readonly _targetDir: string
|
|
|
|
|
|
|
|
constructor(
|
|
|
|
directory: string,
|
|
|
|
targetDir: string,
|
|
|
|
license: SmallLicense,
|
|
|
|
prefix: string = "maki-"
|
|
|
|
) {
|
|
|
|
this._license = license
|
|
|
|
this._directory = directory
|
|
|
|
this._targetDir = targetDir
|
|
|
|
this._prefix = prefix
|
2022-07-02 01:59:26 +02:00
|
|
|
}
|
2022-07-02 02:45:59 +02:00
|
|
|
|
|
|
|
public steal(iconName: string): boolean {
|
|
|
|
const target = this._targetDir + iconName + ".svg"
|
|
|
|
if (existsSync(target)) {
|
|
|
|
return true
|
2022-07-02 01:59:26 +02:00
|
|
|
}
|
2022-07-02 02:45:59 +02:00
|
|
|
try {
|
|
|
|
const file = readFileSync(this._directory + iconName + ".svg", "utf8")
|
2022-09-08 21:40:48 +02:00
|
|
|
writeFileSync(target, file, "utf8")
|
2022-07-02 01:59:26 +02:00
|
|
|
|
2022-09-08 21:40:48 +02:00
|
|
|
writeFileSync(
|
|
|
|
target + ".license_info.json",
|
|
|
|
JSON.stringify({ ...this._license, path: this._prefix + iconName + ".svg" }),
|
|
|
|
"utf8"
|
|
|
|
)
|
2022-07-02 02:45:59 +02:00
|
|
|
console.log("Successfully stolen " + iconName)
|
2022-07-02 01:59:26 +02:00
|
|
|
return true
|
2022-07-02 02:45:59 +02:00
|
|
|
} catch (e) {
|
|
|
|
console.log("Could not steal " + iconName + " due to " + e.message)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class AggregateIconThief implements IconThief {
|
2022-09-08 21:40:48 +02:00
|
|
|
private readonly makiThiefs: MakiThief[]
|
2022-07-02 02:45:59 +02:00
|
|
|
|
|
|
|
constructor(makiThiefs: MakiThief[]) {
|
2022-09-08 21:40:48 +02:00
|
|
|
this.makiThiefs = makiThiefs
|
2022-07-02 02:45:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public steal(iconName: string): boolean {
|
|
|
|
for (const makiThief1 of this.makiThiefs) {
|
|
|
|
if (iconName.startsWith(makiThief1._prefix)) {
|
|
|
|
return makiThief1.steal(iconName.substr(makiThief1._prefix.length))
|
|
|
|
}
|
2022-07-02 01:59:26 +02:00
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class IdThief {
|
2022-09-08 21:40:48 +02:00
|
|
|
private readonly _idPresetsRepository: string
|
2022-07-02 01:59:26 +02:00
|
|
|
|
|
|
|
private readonly _tranlationFiles: Record<string, object> = {}
|
|
|
|
private readonly _knownLanguages: string[]
|
2022-09-08 21:40:48 +02:00
|
|
|
private readonly _iconThief: IconThief
|
2022-07-02 01:59:26 +02:00
|
|
|
|
|
|
|
public constructor(idPresetsRepository: string, iconThief: IconThief) {
|
2022-09-08 21:40:48 +02:00
|
|
|
this._idPresetsRepository = idPresetsRepository
|
|
|
|
this._iconThief = iconThief
|
|
|
|
const knownById = ScriptUtils.readDirRecSync(
|
|
|
|
`${this._idPresetsRepository}/dist/translations/`
|
|
|
|
)
|
|
|
|
.map((pth) => pth.substring(pth.lastIndexOf("/") + 1, pth.length - ".json".length))
|
|
|
|
.filter((lng) => !lng.endsWith(".min"))
|
|
|
|
const missing = Object.keys(known_languages).filter(
|
|
|
|
(lng) => knownById.indexOf(lng.replace("-", "_")) < 0
|
|
|
|
)
|
|
|
|
this._knownLanguages = knownById.filter((lng) => known_languages[lng] !== undefined)
|
|
|
|
console.log(
|
|
|
|
"Id knows following languages:",
|
|
|
|
this._knownLanguages.join(", "),
|
|
|
|
"missing:",
|
|
|
|
missing
|
|
|
|
)
|
2022-07-02 01:59:26 +02:00
|
|
|
}
|
|
|
|
|
2022-07-03 13:18:34 +02:00
|
|
|
public getTranslation(language: string, ...path: string[]): string {
|
2022-07-02 01:59:26 +02:00
|
|
|
let obj = this.loadTranslationFile(language)[language]
|
|
|
|
for (const p of path) {
|
|
|
|
obj = obj[p]
|
|
|
|
if (obj === undefined) {
|
2022-09-08 21:40:48 +02:00
|
|
|
return undefined
|
2022-07-02 01:59:26 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return obj
|
|
|
|
}
|
|
|
|
|
2022-07-02 02:45:59 +02:00
|
|
|
/**
|
|
|
|
* Creates a mapRendering-mapping for the 'shop' theme
|
|
|
|
*/
|
2022-09-08 21:40:48 +02:00
|
|
|
public readShopIcons(): { if: string | { and: string[] }; then: string }[] {
|
2022-07-02 02:45:59 +02:00
|
|
|
const dir = this._idPresetsRepository + "/data/presets/shop"
|
|
|
|
|
2022-09-08 21:40:48 +02:00
|
|
|
const mappings: {
|
|
|
|
if: string | { and: string[] }
|
|
|
|
then: string
|
|
|
|
}[] = []
|
|
|
|
const files = ScriptUtils.readDirRecSync(dir, 1)
|
2022-07-02 02:45:59 +02:00
|
|
|
for (const file of files) {
|
2022-09-08 21:40:48 +02:00
|
|
|
const preset = IdPreset.fromFile(file)
|
2022-07-02 02:45:59 +02:00
|
|
|
|
|
|
|
if (!this._iconThief.steal(preset.icon)) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
const mapping = {
|
|
|
|
if: preset.parseTags(),
|
2022-09-08 21:40:48 +02:00
|
|
|
then: "circle:white;./assets/layers/id_presets/" + preset.icon + ".svg",
|
2022-07-02 02:45:59 +02:00
|
|
|
}
|
|
|
|
mappings.push(mapping)
|
|
|
|
}
|
|
|
|
|
|
|
|
return mappings
|
|
|
|
}
|
|
|
|
|
2022-07-02 01:59:26 +02:00
|
|
|
/**
|
|
|
|
* Creates a tagRenderingConfigJson for the 'shop' theme
|
|
|
|
*/
|
2022-07-03 13:18:34 +02:00
|
|
|
public readShopPresets(): MappingConfigJson[] {
|
2022-07-02 01:59:26 +02:00
|
|
|
const dir = this._idPresetsRepository + "/data/presets/shop"
|
|
|
|
|
2022-07-03 13:18:34 +02:00
|
|
|
const mappings: MappingConfigJson[] = []
|
2022-09-08 21:40:48 +02:00
|
|
|
const files = ScriptUtils.readDirRecSync(dir, 1)
|
2022-07-02 01:59:26 +02:00
|
|
|
for (const file of files) {
|
2022-09-08 21:40:48 +02:00
|
|
|
const name = file.substring(file.lastIndexOf("/") + 1, file.length - ".json".length)
|
2022-07-02 02:45:59 +02:00
|
|
|
const preset = IdPreset.fromFile(file)
|
2022-07-02 01:59:26 +02:00
|
|
|
|
2022-07-02 02:45:59 +02:00
|
|
|
if (preset.searchable === false) {
|
2022-07-02 01:59:26 +02:00
|
|
|
continue
|
|
|
|
}
|
2022-07-02 02:45:59 +02:00
|
|
|
|
|
|
|
console.log(` ${name} (shop=${preset.tags["shop"]}), ${preset.icon}`)
|
|
|
|
|
|
|
|
const thenClause: Record<string, string> = {
|
2022-09-08 21:40:48 +02:00
|
|
|
en: preset.name,
|
2022-07-02 01:59:26 +02:00
|
|
|
}
|
2022-07-03 13:18:34 +02:00
|
|
|
const terms: Record<string, string[]> = {
|
2022-09-08 21:40:48 +02:00
|
|
|
en: preset.terms,
|
2022-07-03 13:18:34 +02:00
|
|
|
}
|
2022-07-02 01:59:26 +02:00
|
|
|
for (const lng of this._knownLanguages) {
|
2022-09-08 21:40:48 +02:00
|
|
|
const lngMc = lng.replace("-", "_")
|
2022-07-02 02:45:59 +02:00
|
|
|
const tr = this.getTranslation(lng, "presets", "presets", "shop/" + name, "name")
|
2022-07-03 13:18:34 +02:00
|
|
|
if (tr !== undefined) {
|
|
|
|
thenClause[lngMc] = tr
|
2022-07-02 01:59:26 +02:00
|
|
|
}
|
2022-07-03 13:18:34 +02:00
|
|
|
|
2022-09-08 21:40:48 +02:00
|
|
|
const termsTr = this.getTranslation(
|
|
|
|
lng,
|
|
|
|
"presets",
|
|
|
|
"presets",
|
|
|
|
"shop/" + name,
|
|
|
|
"terms"
|
|
|
|
)
|
2022-07-03 13:18:34 +02:00
|
|
|
if (termsTr !== undefined) {
|
|
|
|
terms[lngMc] = termsTr.split(",")
|
|
|
|
}
|
2022-07-02 01:59:26 +02:00
|
|
|
}
|
2022-07-02 02:45:59 +02:00
|
|
|
|
2022-09-08 21:40:48 +02:00
|
|
|
let tag = preset.parseTags()
|
|
|
|
const mapping: MappingConfigJson = {
|
2022-07-02 01:59:26 +02:00
|
|
|
if: tag,
|
2022-07-03 13:18:34 +02:00
|
|
|
then: thenClause,
|
2022-09-08 21:40:48 +02:00
|
|
|
searchTerms: terms,
|
2022-07-02 01:59:26 +02:00
|
|
|
}
|
2022-07-02 02:45:59 +02:00
|
|
|
if (preset.tags["shop"] == "yes") {
|
2022-07-02 01:59:26 +02:00
|
|
|
mapping["hideInAnswer"] = true
|
|
|
|
mapping.if["en"] = "Unspecified shop"
|
|
|
|
}
|
2022-07-02 02:45:59 +02:00
|
|
|
|
|
|
|
if (this._iconThief.steal(preset.icon)) {
|
2022-07-02 01:59:26 +02:00
|
|
|
mapping["icon"] = {
|
2022-07-03 13:18:34 +02:00
|
|
|
path: "./assets/layers/id_presets/" + preset.icon + ".svg",
|
2022-09-08 21:40:48 +02:00
|
|
|
class: "medium",
|
2022-07-02 01:59:26 +02:00
|
|
|
}
|
2022-07-02 02:45:59 +02:00
|
|
|
} else {
|
|
|
|
console.log(preset.icon + " could not be stolen :(")
|
2022-07-02 01:59:26 +02:00
|
|
|
}
|
2022-07-02 02:45:59 +02:00
|
|
|
|
2022-07-02 01:59:26 +02:00
|
|
|
mappings.push(mapping)
|
|
|
|
}
|
|
|
|
|
|
|
|
return mappings
|
|
|
|
}
|
|
|
|
|
|
|
|
private loadTranslationFile(language: string): object {
|
|
|
|
const cached = this._tranlationFiles[language]
|
|
|
|
if (cached) {
|
|
|
|
return cached
|
|
|
|
}
|
2022-09-08 21:40:48 +02:00
|
|
|
return (this._tranlationFiles[language] = JSON.parse(
|
|
|
|
readFileSync(`${this._idPresetsRepository}/dist/translations/${language}.json`, "utf8")
|
|
|
|
))
|
2022-07-02 01:59:26 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-03 13:18:34 +02:00
|
|
|
const targetDir = "./assets/layers/id_presets/"
|
2022-07-02 02:45:59 +02:00
|
|
|
|
2022-09-08 21:40:48 +02:00
|
|
|
const makiThief = new MakiThief(
|
|
|
|
"../maki/icons/",
|
|
|
|
targetDir + "maki-",
|
|
|
|
{
|
|
|
|
authors: ["Maki icon set"],
|
|
|
|
license: "CC0",
|
|
|
|
path: null,
|
|
|
|
sources: ["https://github.com/mapbox/maki"],
|
|
|
|
},
|
|
|
|
"maki-"
|
|
|
|
)
|
|
|
|
|
|
|
|
const temakiThief = new MakiThief(
|
|
|
|
"../temaki/icons/",
|
|
|
|
targetDir + "temaki-",
|
|
|
|
{
|
|
|
|
authors: ["Temaki icon set"],
|
|
|
|
license: "CC0",
|
|
|
|
path: null,
|
|
|
|
sources: ["https://github.com/ideditor/temaki"],
|
|
|
|
},
|
|
|
|
"temaki-"
|
|
|
|
)
|
|
|
|
const fasThief = new MakiThief(
|
|
|
|
"../Font-Awesome/svgs/solid/",
|
|
|
|
targetDir + "fas-",
|
|
|
|
{
|
|
|
|
authors: ["Font-Awesome icon set"],
|
|
|
|
license: "CC-BY 4.0",
|
|
|
|
path: null,
|
|
|
|
sources: ["https://github.com/FortAwesome/Font-Awesome"],
|
|
|
|
},
|
|
|
|
"fas-"
|
2022-07-02 01:59:26 +02:00
|
|
|
)
|
2022-09-08 21:40:48 +02:00
|
|
|
const iconThief = new AggregateIconThief([makiThief, temakiThief, fasThief])
|
2022-07-02 01:59:26 +02:00
|
|
|
|
2022-07-02 02:45:59 +02:00
|
|
|
const thief = new IdThief("../id-tagging-schema/", iconThief)
|
2022-07-02 01:59:26 +02:00
|
|
|
|
2022-07-20 22:57:39 +02:00
|
|
|
const id_presets_path = targetDir + "id_presets.json"
|
2022-09-08 21:40:48 +02:00
|
|
|
const idPresets = <LayerConfigJson>JSON.parse(readFileSync(id_presets_path, "utf8"))
|
2022-07-03 13:18:34 +02:00
|
|
|
idPresets.tagRenderings = [
|
|
|
|
{
|
|
|
|
id: "shop_types",
|
2022-09-08 21:40:48 +02:00
|
|
|
mappings: thief.readShopPresets(),
|
2022-07-03 13:18:34 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
id: "shop_rendering",
|
2022-09-08 21:40:48 +02:00
|
|
|
mappings: thief.readShopIcons(),
|
|
|
|
},
|
2022-07-03 13:18:34 +02:00
|
|
|
]
|
|
|
|
|
2022-09-08 21:40:48 +02:00
|
|
|
writeFileSync(id_presets_path, JSON.stringify(idPresets, null, " "), "utf8")
|