From d3895800f3d2451f0087e41c325a257605be788a Mon Sep 17 00:00:00 2001 From: Pieter Vander Vennet Date: Tue, 5 Dec 2023 00:04:29 +0100 Subject: [PATCH] Favourites: some fixes to rendering the title --- .../climbing_opportunity.json | 3 +- scripts/generateFavouritesLayer.ts | 12 +++++-- src/Logic/Tags/RegexTag.ts | 33 +++++++++++++++++-- src/Logic/Tags/TagUtils.ts | 4 +-- .../TagRendering/TagRenderingAnswer.svelte | 1 + 5 files changed, 44 insertions(+), 9 deletions(-) diff --git a/assets/layers/climbing_opportunity/climbing_opportunity.json b/assets/layers/climbing_opportunity/climbing_opportunity.json index 89fd5d6795..11d7727910 100644 --- a/assets/layers/climbing_opportunity/climbing_opportunity.json +++ b/assets/layers/climbing_opportunity/climbing_opportunity.json @@ -29,7 +29,8 @@ "natural=stone" ] }, - "climbing=" + "climbing=", + "sport!=climbing" ] } }, diff --git a/scripts/generateFavouritesLayer.ts b/scripts/generateFavouritesLayer.ts index 5c48e0ef00..29b25d17d2 100644 --- a/scripts/generateFavouritesLayer.ts +++ b/scripts/generateFavouritesLayer.ts @@ -1,6 +1,6 @@ import Script from "./Script" import { LayerConfigJson } from "../src/Models/ThemeConfig/Json/LayerConfigJson" -import { readFileSync, writeFileSync } from "fs" +import { existsSync, readFileSync, writeFileSync } from "fs" import { AllSharedLayers } from "../src/Customizations/AllSharedLayers" import { AllKnownLayoutsLazy } from "../src/Customizations/AllKnownLayouts" import { Utils } from "../src/Utils" @@ -47,7 +47,6 @@ export class GenerateFavouritesLayer extends Script { const bTag = TagUtils.Tag(b.if) const aPop = TagUtils.GetPopularity(aTag) const bPop = TagUtils.GetPopularity(bTag) - console.log("Comparing", a.if, "with", b.if, { aPop, bPop }) return aPop - bPop }) @@ -282,7 +281,14 @@ export class GenerateFavouritesLayer extends Script { this.addTagRenderings(proto) this.addTitle(proto) this.addTitleIcons(proto) - writeFileSync("./assets/layers/favourite/favourite.json", JSON.stringify(proto, null, " ")) + const targetContent = JSON.stringify(proto, null, " ") + const path = "./assets/layers/favourite/favourite.json" + if (existsSync(path)) { + if (readFileSync(path, "utf8") === targetContent) { + return // No need to actually write the file, it is identical + } + } + writeFileSync(path, targetContent) } private readLayer(path: string): LayerConfigJson { diff --git a/src/Logic/Tags/RegexTag.ts b/src/Logic/Tags/RegexTag.ts index 436a9175f9..f024f82517 100644 --- a/src/Logic/Tags/RegexTag.ts +++ b/src/Logic/Tags/RegexTag.ts @@ -12,6 +12,9 @@ export class RegexTag extends TagsFilter { super() this.key = key this.value = value + if (this.value instanceof RegExp && ("" + this.value).startsWith("^(^(")) { + throw "Detected a duplicate start marker ^(^( in a regextag:" + this.value + } this.invert = invert this.matchesEmpty = RegexTag.doesMatch("", this.value) } @@ -42,11 +45,21 @@ export class RegexTag extends TagsFilter { return possibleRegex.test(fromTag) } - private static source(r: string | RegExp) { + private static source(r: string | RegExp, includeStartMarker: boolean = true) { if (typeof r === "string") { return r } - return r.source + if (r === undefined) { + return undefined + } + const src = r.source + if (includeStartMarker) { + return src + } + if (src.startsWith("^(") && src.endsWith(")$")) { + return src.substring(2, src.length - 2) + } + return src } /** @@ -83,8 +96,22 @@ export class RegexTag extends TagsFilter { } } + /** + * import { TagUtils } from "./TagUtils"; + * + * const t = TagUtils.Tag("a~b") + * t.asJson() // => "a~b" + * + * const t = TagUtils.Tag("a=") + * t.asJson() // => "a=" + */ asJson(): TagConfigJson { - return this.asHumanString() + const v = RegexTag.source(this.value, false) + if (typeof this.key === "string") { + const oper = typeof this.value === "string" ? "=" : "~" + return `${this.key}${this.invert ? "!" : ""}${oper}${v}` + } + return `${this.key.source}${this.invert ? "!" : ""}~~${v}` } isUsableAsAnswer(): boolean { diff --git a/src/Logic/Tags/TagUtils.ts b/src/Logic/Tags/TagUtils.ts index 50bf9a93b8..1893399cec 100644 --- a/src/Logic/Tags/TagUtils.ts +++ b/src/Logic/Tags/TagUtils.ts @@ -871,10 +871,10 @@ export class TagUtils { public static GetPopularity(tag: TagsFilter): number | undefined { if (tag instanceof And) { - return Math.min(...Utils.NoNull(tag.and.map((t) => TagUtils.GetPopularity(t)))) + return Math.min(...Utils.NoNull(tag.and.map((t) => TagUtils.GetPopularity(t)))) - 1 } if (tag instanceof Or) { - return Math.max(...Utils.NoNull(tag.or.map((t) => TagUtils.GetPopularity(t)))) + return Math.max(...Utils.NoNull(tag.or.map((t) => TagUtils.GetPopularity(t)))) + 1 } if (tag instanceof Tag) { return TagUtils.GetCount(tag.key, tag.value) diff --git a/src/UI/Popup/TagRendering/TagRenderingAnswer.svelte b/src/UI/Popup/TagRendering/TagRenderingAnswer.svelte index 243413fafa..62fdee263b 100644 --- a/src/UI/Popup/TagRendering/TagRenderingAnswer.svelte +++ b/src/UI/Popup/TagRendering/TagRenderingAnswer.svelte @@ -26,6 +26,7 @@ onDestroy( tags.addCallbackAndRun((tags) => { _tags = tags + console.log("Getting render value for", _tags,config) trs = Utils.NoNull(config?.GetRenderValues(_tags)) }) )