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

@ -1,83 +0,0 @@
/*
* This script attempt to automatically fix some basic issues when a theme from the custom generator is loaded
*/
import {Utils} from "../Utils"
import {readFileSync, writeFileSync} from "fs";
import SmallLicense from "../Models/smallLicense";
import ScriptUtils from "./ScriptUtils";
import AllImageProviders from "../Logic/ImageProviders/AllImageProviders";
import {LayoutConfigJson} from "../Models/ThemeConfig/Json/LayoutConfigJson";
import LayerConfig from "../Models/ThemeConfig/LayerConfig";
ScriptUtils.fixUtils()
if (process.argv.length == 2) {
console.log("USAGE: ts-node scripts/fixTheme <path to theme>")
throw "No path specified"
}
const path = process.argv[2]
const dir = path.substring(0, path.lastIndexOf("/"))
console.log("Fixing up ", path)
const themeConfigJson: LayoutConfigJson = JSON.parse(readFileSync(path, "UTF8"))
const licenses: SmallLicense[] = []
const replacements: { source: string, destination: string }[] = []
for (const layerConfigJson of themeConfigJson.layers) {
if (typeof (layerConfigJson) === "string") {
continue;
}
if (layerConfigJson["overpassTags"] !== undefined) {
const tags = layerConfigJson["overpassTags"];
layerConfigJson["overpassTags"] = undefined;
layerConfigJson["source"] = {osmTags: tags}
}
// @ts-ignore
const layerConfig = new LayerConfig(layerConfigJson, "fix theme", true)
const images: string[] = Array.from(layerConfig.ExtractImages())
const remoteImages = images.filter(img => img.startsWith("http"))
for (const remoteImage of remoteImages) {
const filename = remoteImage.substring(remoteImage.lastIndexOf("/"))
ScriptUtils.DownloadFileTo(remoteImage, dir + "/" + filename)
const imgPath = remoteImage.substring(remoteImage.lastIndexOf("/") + 1)
for (const attributionSrc of AllImageProviders.ImageAttributionSource) {
try {
attributionSrc.GetAttributionFor(remoteImage).addCallbackAndRun(license => {
console.log("Downloaded an attribution!")
licenses.push({
path: imgPath,
license: license?.license ?? "",
authors: Utils.NoNull([license?.artist]),
sources: [remoteImage]
})
})
} catch (e) {
// Hush hush
}
}
replacements.push({source: remoteImage, destination: `${dir}/${imgPath}`})
}
}
let fixedThemeJson = JSON.stringify(themeConfigJson, null, " ")
for (const replacement of replacements) {
fixedThemeJson = fixedThemeJson.replace(new RegExp(replacement.source, "g"), replacement.destination)
}
writeFileSync(dir + "/generated.license_info.json", JSON.stringify(licenses, null, " "))
writeFileSync(path + ".autofixed.json", fixedThemeJson)
console.log(`IMPORTANT:
1) Copy generated.license_info.json over into license_info.json and add the missing attributions and authors
2) Verify ${path}.autofixed.json as theme, and rename it to ${path}`)

View file

@ -2,6 +2,10 @@ import {existsSync, mkdirSync, readFileSync, unlinkSync, writeFileSync} from "fs
import SmallLicense from "../Models/smallLicense";
import ScriptUtils from "./ScriptUtils";
function validateLicenseInfo(l : SmallLicense){
l.sources.map(s => new URL(s))
}
/**
* Sweeps the entire 'assets/' (except assets/generated) directory for image files and any 'license_info.json'-file.
* Checks that the license info is included for each of them and generates a compiles license_info.json for those
@ -11,8 +15,6 @@ function generateLicenseInfos(paths: string[]): SmallLicense[] {
const licenses = []
for (const path of paths) {
try {
const parsed = JSON.parse(readFileSync(path, "UTF-8"))
if (Array.isArray(parsed)) {
const l: SmallLicense[] = parsed
@ -22,12 +24,6 @@ function generateLicenseInfos(paths: string[]): SmallLicense[] {
licenses.push(...l)
} else {
const smallLicens: SmallLicense = parsed;
/*if(parsed.license === "CC-BY"){
console.log("Rewriting ", path)
parsed.license === "CC-BY 4.0"
writeFileSync(path, JSON.stringify(smallLicens, null, " "))
}*/
smallLicens.path = path.substring(0, 1 + path.lastIndexOf("/")) + smallLicens.path
licenses.push(smallLicens)
}
@ -229,6 +225,7 @@ function createFullLicenseOverview(licensePaths) {
for (const licensePath of licensePaths) {
const licenses = <SmallLicense[]>JSON.parse(readFileSync(licensePath, "UTF-8"))
for (const license of licenses) {
validateLicenseInfo(license)
const dir = licensePath.substring(0, licensePath.length - "license_info.json".length)
license.path = dir + license.path
allLicenses.push(license)