Docs: add 'used in layers'-information for builtingQuestions + various small refactorings

This commit is contained in:
Pieter Vander Vennet 2025-06-13 02:39:27 +02:00
parent 2545982dbd
commit bc2ea7841f
9 changed files with 83 additions and 47 deletions

View file

@ -4,7 +4,7 @@ import * as known_layers from "../assets/generated/known_layers.json"
import { LayerConfigJson } from "../Models/ThemeConfig/Json/LayerConfigJson"
export class AllSharedLayers {
public static sharedLayers: Map<string, LayerConfig> = AllSharedLayers.getSharedLayers()
public static sharedLayers: ReadonlyMap<string, LayerConfig> = AllSharedLayers.getSharedLayers()
public static getSharedLayersConfigs(): Map<string, LayerConfigJson> {
const sharedLayers = new Map<string, LayerConfigJson>()
for (const layer of known_layers["layers"]) {

View file

@ -9,7 +9,7 @@ export interface DesugaringContext {
* Order of appearance in questions.json
*/
tagRenderingOrder: string[]
sharedLayers: Map<string, LayerConfigJson>
sharedLayers: Map<string, Readonly<LayerConfigJson>>
publicLayers?: Set<string>
}

View file

@ -3,9 +3,38 @@ import { ExtraFuncParams, ExtraFunctions } from "../../Logic/ExtraFunctions"
import LayerConfig from "./LayerConfig"
import { SpecialVisualization } from "../../UI/SpecialVisualization"
import SpecialVisualizations from "../../UI/SpecialVisualizations"
import { LayerConfigJson } from "./Json/LayerConfigJson"
export default class DependencyCalculator {
public static GetTagRenderingDependencies(tr: TagRenderingConfig): {
/**
* For every tagRendering in the listed layers, determines in what layers they end up
*/
public static tagRenderingImportedBy(questionedLayer: LayerConfig, layers: LayerConfig[]): Map<string, {
layer: string
}[]> {
const result: Map<string, { layer: string }[]> = new Map()
for (const layer of layers) {
const hasRightContext = layer.tagRenderings.filter(tr => tr._definedIn !== undefined && tr?._definedIn?.[0] === questionedLayer.id)
for (const tr of hasRightContext) {
const id = tr._definedIn[1]
if (!result.has(id)) {
result.set(id, [])
}
result.get(id).push({ layer: layer.id })
}
}
return result
}
/**
* Calculates what layers are introduced by a tagRenderingConfig
* @param tr
* @private
*/
private static getTagRenderingDependencies(tr: TagRenderingConfig): {
id: string
minzoom?: number
neededBy: string
@ -83,7 +112,7 @@ export default class DependencyCalculator {
}
for (const tr of layer.AllTagRenderings()) {
for (const dep of DependencyCalculator.GetTagRenderingDependencies(tr)) {
for (const dep of DependencyCalculator.getTagRenderingDependencies(tr)) {
deps.push({
neededLayer: dep.id,
reason: `tagrendering ${dep.neededBy} needs this layer`,

View file

@ -439,15 +439,15 @@ export default class LayerConfig extends WithContextLoader {
}
public generateDocumentation(
usedInThemes: string[],
layerIsNeededBy?: Map<string, string[]>,
dependencies: {
context?: string
reason: string
neededLayer: string
}[] = [],
addedByDefault = false,
canBeIncluded = true
{ usedInThemes = [], layerIsNeededBy, dependencies = [], addedByDefault = false, canBeIncluded = true, lang = "en", reusedTagRenderings }: {
usedInThemes?: string[],
layerIsNeededBy?: Map<string, string[]>,
dependencies?: { context?: string; reason: string; neededLayer: string }[],
addedByDefault?: boolean,
canBeIncluded?: boolean,
reusedTagRenderings?: Map<string, {layer: string}[]>,
lang?: string
}
): string {
const extraProps: string[] = []
extraProps.push("This layer is shown at zoomlevel **" + this.minzoom + "** and higher")
@ -669,7 +669,7 @@ export default class LayerConfig extends WithContextLoader {
this.generateDocumentationQuickTable(),
...this.tagRenderings
.filter((tr) => tr.labels.indexOf("ignore_docs") < 0)
.map((tr) => tr.GenerateDocumentation()),
.map((tr) => tr.generateDocumentation(lang, reusedTagRenderings?.get(tr.id)?.map(l => l.layer))),
...filterDocs,
].join("\n\n")
}

View file

@ -941,7 +941,7 @@ export default class TagRenderingConfig {
}
}
GenerateDocumentation(lang: string = "en"): string {
generateDocumentation(lang: string = "en", usedInLayers?: string[]): string {
let freeform: string = undefined
if (this.render) {
freeform = "\n*" + this.render.textFor(lang) + "*"
@ -1016,12 +1016,18 @@ export default class TagRenderingConfig {
let labels: string = undefined
if (this.labels?.length > 0) {
labels = [
"This tagrendering has labels ",
"This tagrendering has labels",
...this.labels.map((label) => "`" + label + "`"),
].join("\n")
].join(" ")
}
let reuse : string = undefined
if(usedInLayers?.length > 0){
reuse = [`This tagRendering is used in ${usedInLayers.length} layers:`,
...usedInLayers.map(l => `[${l}](./Layers/${l}.md)`)
].join(" ")
}
return [
return Utils.NoNull([
"### " + this.id,
this.description,
this.question !== undefined
@ -1031,7 +1037,9 @@ export default class TagRenderingConfig {
mappings,
condition,
labels,
].join("\n")
"",
reuse
]).join("\n")
}
public usedTags(): TagsFilter[] {

View file

@ -283,8 +283,7 @@ class ContextRewritingStep<T> extends Conversion<LayerConfigJson, T> {
) {
super(
"ContextRewritingStep",
"When validating a layer, the tagRenderings are first expanded. Some builtin tagRendering-calls (e.g. `contact`) will introduce _multiple_ tagRenderings, causing the count to be off. This class rewrites the error messages to fix this",
[]
"When validating a layer, the tagRenderings are first expanded. Some builtin tagRendering-calls (e.g. `contact`) will introduce _multiple_ tagRenderings, causing the count to be off. This class rewrites the error messages to fix this"
)
this._state = state
this._step = step
@ -460,7 +459,7 @@ export default class EditLayerState extends EditJsonState<LayerConfigJson> {
}
const state: DesugaringContext = {
tagRenderings: sharedQuestions,
sharedLayers: layers,
sharedLayers: new Map(layers),
tagRenderingOrder: [],
}
const prepare = this.buildValidation(state)