Extract and validate images using a new conversion, regenerate docs

This commit is contained in:
Pieter Vander Vennet 2022-02-09 22:37:21 +01:00
parent 5198f5d310
commit b3c58ae82e
52 changed files with 8611 additions and 3408 deletions

View file

@ -0,0 +1,127 @@
import {Conversion, DesugaringStep} from "./Conversion";
import {LayoutConfigJson} from "../Json/LayoutConfigJson";
import {Utils} from "../../../Utils";
import * as metapaths from "../../../assets/layoutconfigmeta.json";
import * as tagrenderingmetapaths from "../../../assets/tagrenderingconfigmeta.json";
export class ExtractImages extends Conversion<LayoutConfigJson, string[]> {
constructor() {
super("Extract all images from a layoutConfig using the meta paths");
}
convert(json: LayoutConfigJson, context: string): { result: string[] } {
const paths = metapaths["default"] ?? metapaths
const trpaths = tagrenderingmetapaths["default"] ?? tagrenderingmetapaths
const allFoundImages = []
for (const metapath of paths) {
if (metapath.typeHint === undefined) {
continue
}
if (metapath.typeHint !== "image" && metapath.typeHint !== "icon") {
continue
}
const mightBeTr = Array.isArray(metapath.type) && metapath.type.some(t => t["$ref"] == "#/definitions/TagRenderingConfigJson")
const found = Utils.CollectPath(metapath.path, json)
if (mightBeTr) {
// We might have tagRenderingConfigs containing icons here
for (const foundImage of found) {
if (typeof foundImage === "string") {
allFoundImages.push(foundImage)
} else {
// This is a tagRendering where every rendered value might be an icon!
for (const trpath of trpaths) {
if (trpath.typeHint !== "rendered") {
continue
}
Utils.CollectPath(trpath.path, foundImage, allFoundImages)
}
}
}
} else {
allFoundImages.push(...found)
}
}
const splitParts = [].concat(...allFoundImages.map(img => img.split(";")))
.map(img => img.split(":")[0])
return {result: Utils.Dedup(splitParts)};
}
}
export class FixImages extends DesugaringStep<LayoutConfigJson> {
private readonly _knownImages: Set<string>;
constructor(knownImages: Set<string>) {
super("Walks over the entire theme and replaces images to the relative URL. Only works if the ID of the theme is an URL");
this._knownImages = knownImages;
}
convert(json: LayoutConfigJson, context: string): { result: LayoutConfigJson; errors?: string[]; warnings?: string[] } {
let url: URL;
try {
url = new URL(json.id)
} catch (e) {
// Not a URL, we don't rewrite
return {result: json}
}
const absolute = url.protocol + "//" + url.host
let relative = url.protocol + "//" + url.host + url.pathname
relative = relative.substring(0, relative.lastIndexOf("/"))
const self = this;
function replaceString(leaf: string) {
if (self._knownImages.has(leaf)) {
return leaf;
}
if (leaf.startsWith("./")) {
return relative + leaf.substring(1)
}
if (leaf.startsWith("/")) {
return absolute + leaf
}
return leaf;
}
json = Utils.Clone(json)
let paths = metapaths["default"] ?? metapaths
let trpaths = tagrenderingmetapaths["default"] ?? tagrenderingmetapaths
for (const metapath of paths) {
if (metapath.typeHint !== "image" && metapath.typeHint !== "icon") {
continue
}
const mightBeTr = Array.isArray(metapath.type) && metapath.type.some(t => t["$ref"] == "#/definitions/TagRenderingConfigJson")
Utils.WalkPath(metapath.path, json, leaf => {
if (typeof leaf === "string") {
return replaceString(leaf)
}
if (mightBeTr) {
// We might have reached a tagRenderingConfig containing icons
// lets walk every rendered value and fix the images in there
for (const trpath of trpaths) {
if (trpath.typeHint !== "rendered") {
continue
}
Utils.WalkPath(trpath.path, leaf, (rendered => {
return replaceString(rendered)
}))
}
}
return leaf;
})
}
return {
result: json
};
}
}

View file

@ -3,7 +3,6 @@ import {Utils} from "../../../Utils";
import LineRenderingConfigJson from "../Json/LineRenderingConfigJson";
import {LayerConfigJson} from "../Json/LayerConfigJson";
import {DesugaringStep, Fuse, OnEvery} from "./Conversion";
import * as metapaths from "../../../assets/layoutconfigmeta.json"
export class UpdateLegacyLayer extends DesugaringStep<LayerConfigJson | string | { builtin, override }> {
@ -160,101 +159,3 @@ export class FixLegacyTheme extends Fuse<LayoutConfigJson> {
}
export class FixImages extends DesugaringStep<LayoutConfigJson> {
private readonly _knownImages: Set<string>;
constructor(knownImages: Set<string>) {
super("Walks over the entire theme and replaces images to the relative URL. Only works if the ID of the theme is an URL");
this._knownImages = knownImages;
}
/**
* Walks the path into the object till the end.
*
* If a list is encountered, this is tranparently walked recursively on every object.
*
* The leaf objects are replaced
*/
private static WalkPath(path: string[], object: any, replaceLeaf: ((leaf: any) => any)) {
const head = path[0]
if (path.length === 1) {
// We have reached the leaf
const leaf = object[head];
if (leaf !== undefined) {
object[head] = replaceLeaf(leaf)
}
return
}
const sub = object[head]
if (sub === undefined) {
return;
}
if (typeof sub !== "object") {
return;
}
if (sub["forEach"] !== undefined) {
sub.forEach(el => FixImages.WalkPath(path.slice(1), el, replaceLeaf))
return;
}
FixImages.WalkPath(path.slice(1), sub, replaceLeaf)
}
convert(json: LayoutConfigJson, context: string): { result: LayoutConfigJson; errors?: string[]; warnings?: string[] } {
let url: URL;
console.log("Fixing images!")
try {
url = new URL(json.id)
} catch (e) {
// Not a URL, we don't rewrite
return {result: json}
}
const absolute = url.protocol +"//"+url.host
let relative = url.protocol +"//"+ url.host + url.pathname
relative = relative.substring(0, relative.lastIndexOf("/"))
const self = this;
function replaceString(leaf: string) {
if (self._knownImages.has(leaf)) {
return leaf;
}
if (leaf.startsWith("./")) {
return relative + leaf.substring(1)
}
if (leaf.startsWith("/")) {
return absolute + leaf
}
return leaf;
}
json = Utils.Clone(json)
let paths = metapaths["default"] ?? metapaths
for (const metapath of paths) {
if (metapath.typeHint !== "image" && metapath.typeHint !== "icon") {
continue
}
FixImages.WalkPath(metapath.path, json, leaf => {
console.log("Detected leaf: ", leaf)
if (typeof leaf === "string") {
return replaceString(leaf)
}
if (metapath.type["some"] !== undefined && (<any[]>metapath.type).some(t => t["$ref"] == "\"#/definitions/TagRenderingConfigJson\"")) {
console.log("Possibly found a tagrendering")
}
return leaf;
})
}
return {
result: json
};
}
}

View file

@ -8,6 +8,7 @@ import {LayoutConfigJson} from "../Json/LayoutConfigJson";
import LayoutConfig from "../LayoutConfig";
import {TagRenderingConfigJson} from "../Json/TagRenderingConfigJson";
import {TagUtils} from "../../../Logic/Tags/TagUtils";
import {ExtractImages} from "./FixImages";
class ValidateLanguageCompleteness extends DesugaringStep<any> {
@ -54,8 +55,9 @@ class ValidateTheme extends DesugaringStep<LayoutConfigJson> {
this._isBuiltin = isBuiltin;
}
convert(json: LayoutConfigJson, context: string): { result: LayoutConfigJson; errors: string[] } {
convert(json: LayoutConfigJson, context: string): { result: LayoutConfigJson; errors: string[], warnings: string[] } {
const errors = []
const warnings = []
{
// Legacy format checks
if (this._isBuiltin) {
@ -67,7 +69,34 @@ class ValidateTheme extends DesugaringStep<LayoutConfigJson> {
}
}
}
{
// Check for remote images
const images = new ExtractImages().convertStrict(json, "validation")
const remoteImages = images.filter(img => img.indexOf("http") == 0)
for (const remoteImage of remoteImages) {
errors.push("Found a remote image: " + remoteImage + " in theme " + json.id + ", please download it.")
}
for (const image of images) {
if (image.indexOf("{") >= 0) {
warnings.push("Ignoring image with { in the path: ", image)
continue
}
if(image === "assets/SocialImage.png"){
continue
}
if(image.match(/[a-z]*/)){
// This is a builtin img, e.g. 'checkmark' or 'crosshair'
continue;
}
if (this.knownImagePaths !== undefined && !this.knownImagePaths.has(image)) {
const ctx = context === undefined ? "" : ` in a layer defined in the theme ${context}`
errors.push(`Image with path ${image} not found or not attributed; it is used in ${json.id}${ctx}`)
}
}
}
try {
const theme = new LayoutConfig(json, true, "test")
if (theme.id !== theme.id.toLowerCase()) {
@ -97,7 +126,8 @@ class ValidateTheme extends DesugaringStep<LayoutConfigJson> {
return {
result: json,
errors
errors,
warnings
};
}
}
@ -247,27 +277,6 @@ export class ValidateLayer extends DesugaringStep<LayerConfigJson> {
errors.push(context + ": layer " + json.id + " contains an old 'hideUnderlayingFeaturesMinPercentage'")
}
}
{
// Check for remote images
const layer = new LayerConfig(json, "test", true)
const images = Array.from(layer.ExtractImages())
const remoteImages = images.filter(img => img.indexOf("http") == 0)
for (const remoteImage of remoteImages) {
errors.push("Found a remote image: " + remoteImage + " in layer " + layer.id + ", please download it. You can use the fixTheme script to automate this")
}
for (const image of images) {
if (image.indexOf("{") >= 0) {
warnings.push("Ignoring image with { in the path: ", image)
continue
}
if (this.knownImagePaths !== undefined && !this.knownImagePaths.has(image)) {
const ctx = context === undefined ? "" : ` in a layer defined in the theme ${context}`
errors.push(`Image with path ${image} not found or not attributed; it is used in ${layer.id}${ctx}`)
}
}
}
{
// CHeck location of layer file
const expected: string = `assets/layers/${json.id}/${json.id}.json`