Scripts: create script to import layers from studio into official mapcomplete

This commit is contained in:
Pieter Vander Vennet 2025-07-05 04:39:30 +02:00
parent c7b905d1fb
commit db685dc05f
5 changed files with 186 additions and 81 deletions

View file

@ -10,7 +10,7 @@ import { ConversionContext } from "./ConversionContext"
export class ExtractImages extends Conversion<
ThemeConfigJson,
{ path: string; context: string }[]
{ path: string; context: string; location: (string|number)[] }[]
> {
private static readonly layoutMetaPaths = metapaths.filter((mp) => {
const typeHint = mp.hints.typehint
@ -24,11 +24,11 @@ export class ExtractImages extends Conversion<
)
})
private static readonly tagRenderingMetaPaths = tagrenderingmetapaths
private _isOfficial: boolean
private readonly _isOfficial: boolean
private _sharedTagRenderings: Set<string>
constructor(isOfficial: boolean, sharedTagRenderings: Set<string>) {
super("ExctractImages", "Extract all images from a layoutConfig using the meta paths.", [])
constructor(isOfficial: boolean, sharedTagRenderings: Set<string> = new Set()) {
super("ExtractImages", "Extract all images from a themeConfig using the meta paths.")
this._isOfficial = isOfficial
this._sharedTagRenderings = sharedTagRenderings
}
@ -111,8 +111,8 @@ export class ExtractImages extends Conversion<
convert(
json: ThemeConfigJson,
context: ConversionContext
): { path: string; context: string }[] {
const allFoundImages: { path: string; context: string }[] = []
): { path: string; context: string, location: (string | number)[] }[] {
const allFoundImages: { path: string; context: string, location: (string | number)[] }[] = []
for (const metapath of ExtractImages.layoutMetaPaths) {
const mightBeTr = ExtractImages.mightBeTagRendering(<any>metapath)
@ -143,7 +143,7 @@ export class ExtractImages extends Conversion<
continue
}
allFoundImages.push({ path: foundImage, context: context + "." + path })
allFoundImages.push({ path: foundImage, context: context + "." + path, location: path })
} else {
// This is a tagRendering.
// Either every rendered value might be an icon
@ -177,6 +177,7 @@ export class ExtractImages extends Conversion<
allFoundImages.push({
path: img.leaf,
context: context + "." + path,
location: img.path
})
}
}
@ -191,6 +192,7 @@ export class ExtractImages extends Conversion<
.map((path) => ({
path,
context: context + "." + path,
location:img.path
}))
)
}
@ -211,12 +213,13 @@ export class ExtractImages extends Conversion<
allFoundImages.push({
context: context.path.join(".") + "." + foundElement.path.join("."),
path: foundElement.leaf,
location: foundElement.path
})
}
}
}
const cleanedImages: { path: string; context: string }[] = []
const cleanedImages: { path: string; context: string, location: (string | number)[] }[] = []
for (const foundImage of allFoundImages) {
if (foundImage.path.startsWith("<") && foundImage.path.endsWith(">")) {
@ -225,7 +228,8 @@ export class ExtractImages extends Conversion<
const images = Array.from(doc.getElementsByTagName("img"))
const paths = images.map((i) => i.getAttribute("src"))
cleanedImages.push(
...paths.map((path) => ({ path, context: foundImage.context + " (in html)" }))
...paths.map((path) => ({ path, context: foundImage.context + " (in html)",
location: foundImage.location }))
)
continue
}
@ -242,7 +246,7 @@ export class ExtractImages extends Conversion<
)
)
for (const path of allPaths) {
cleanedImages.push({ path, context: foundImage.context })
cleanedImages.push({ path, context: foundImage.context , location:foundImage.location})
}
}
@ -255,9 +259,8 @@ export class FixImages extends DesugaringStep<ThemeConfigJson> {
constructor(knownImages: Set<string>) {
super(
"fixImages",
"Walks over the entire theme and replaces images to the relative URL. Only works if the ID of the theme is an URL",
[],
"fixImages"
)
this._knownImages = knownImages
}