Refactoring: port doc generation to generate markdown directly without UIElements

This commit is contained in:
Pieter Vander Vennet 2024-07-12 03:17:15 +02:00
parent 7a7439b161
commit 8e9c03e258
17 changed files with 309 additions and 320 deletions

View file

@ -31,6 +31,7 @@ import { TagUtils } from "../src/Logic/Tags/TagUtils"
import Script from "./Script"
import { Changes } from "../src/Logic/Osm/Changes"
import TableOfContents from "../src/UI/Base/TableOfContents"
import MarkdownUtils from "../src/Utils/MarkdownUtils"
/**
* Converts a markdown-file into a .json file, which a walkthrough/slideshow element can use
@ -56,15 +57,15 @@ class ToSlideshowJson {
sections.push(currentSection)
currentSection = []
}
line = line.replace('src="../../public/', 'src="./')
line = line.replace('src="../../', 'src="./')
line = line.replace("src=\"../../public/", "src=\"./")
line = line.replace("src=\"../../", "src=\"./")
currentSection.push(line)
}
sections.push(currentSection)
writeFileSync(
this._target,
JSON.stringify({
sections: sections.map((s) => s.join("\n")).filter((s) => s.length > 0),
sections: sections.map((s) => s.join("\n")).filter((s) => s.length > 0)
})
)
}
@ -83,7 +84,7 @@ class WikiPageGenerator {
generate() {
let wikiPage =
'{|class="wikitable sortable"\n' +
"{|class=\"wikitable sortable\"\n" +
"! Name, link !! Genre !! Covered region !! Language !! Description !! Free materials !! Image\n" +
"|-"
@ -140,8 +141,8 @@ export class GenerateDocs extends Script {
mkdirSync("./Docs/Themes")
}
this.WriteFile("./Docs/Tags_format.md", TagUtils.generateDocs(), [
"src/Logic/Tags/TagUtils.ts",
this.WriteMarkdownFile("./Docs/Tags_format.md", TagUtils.generateDocs(), [
"src/Logic/Tags/TagUtils.ts"
])
new ToSlideshowJson(
@ -166,58 +167,30 @@ export class GenerateDocs extends Script {
})
this.WriteMarkdownFile("./Docs/SpecialRenderings.md", SpecialVisualizations.HelpMessage(), [
"src/UI/SpecialVisualizations.ts",
"src/UI/SpecialVisualizations.ts"
])
this.WriteFile(
this.WriteMarkdownFile(
"./Docs/CalculatedTags.md",
new Combine([
new Title("Metatags", 1),
[
"# Metatags",
SimpleMetaTaggers.HelpText(),
ExtraFunctions.HelpText(),
]).SetClass("flex-col"),
ExtraFunctions.HelpText()
].join("\n"),
["src/Logic/SimpleMetaTagger.ts", "src/Logic/ExtraFunctions.ts"]
)
this.WriteFile("./Docs/SpecialInputElements.md", Validators.HelpText(), [
"src/UI/InputElement/Validators.ts",
this.WriteMarkdownFile("./Docs/SpecialInputElements.md", Validators.HelpText(), [
"src/UI/InputElement/Validators.ts"
])
this.WriteFile("./Docs/ChangesetMeta.md", Changes.getDocs(), [
this.WriteMarkdownFile("./Docs/ChangesetMeta.md", Changes.getDocs(), [
"src/Logic/Osm/Changes.ts",
"src/Logic/Osm/ChangesetHandler.ts",
"src/Logic/Osm/ChangesetHandler.ts"
])
new WikiPageGenerator().generate()
console.log("Generated docs")
}
/**
* @deprecated
*/
private WriteFile(
filename,
html: string | BaseUIElement,
autogenSource: string[],
options?: {
noTableOfContents: boolean
}
): void {
if (!html) {
return
}
let md = new Combine([
Translations.W(html),
"\n\nThis document is autogenerated from " +
autogenSource
.map(
(file) =>
`[${file}](https://github.com/pietervdvn/MapComplete/blob/develop/${file})`
)
.join(", "),
]).AsMarkdown()
this.WriteMarkdownFile(filename, md, autogenSource, options)
}
private WriteMarkdownFile(
filename: string,
markdown: string,
@ -254,22 +227,30 @@ export class GenerateDocs extends Script {
const warnAutomated =
"[//]: # (WARNING: this file is automatically generated. Please find the sources at the bottom and edit those sources)\n\n"
writeFileSync(filename, warnAutomated + md)
const generatedFrom =
[
"This document is autogenerated from",
autogenSource.map(s => `[${s}](https://github.com/pietervdvn/MapComplete/blob/develop/${s})`).join(", ")
].join(" ")
writeFileSync(filename, warnAutomated + md+"\n\n" +generatedFrom+"\n")
}
private generateHotkeyDocs() {
new ThemeViewState(new LayoutConfig(<any>bookcases), new Set())
this.WriteFile("./Docs/Hotkeys.md", Hotkeys.generateDocumentation(), [])
this.WriteMarkdownFile("./Docs/Hotkeys.md", Hotkeys.generateDocumentation(), ["src/UI/Base/Hotkeys.ts"])
}
private generateBuiltinUnits() {
const layer = new LayerConfig(<LayerConfigJson>unit, "units", true)
const els: (BaseUIElement | string)[] = [new Title(layer.id, 2)]
const els: string[] = ["## " + layer.id]
for (const unit of layer.units) {
els.push(new Title(unit.quantity))
els.push("### " + unit.quantity)
for (const denomination of unit.denominations) {
els.push(new Title(denomination.canonical, 4))
els.push("#### " + denomination.canonical)
if (denomination.useIfNoUnitGiven === true) {
els.push("*Default denomination*")
} else if (
@ -277,7 +258,7 @@ export class GenerateDocs extends Script {
denomination.useIfNoUnitGiven.length > 0
) {
els.push("Default denomination in the following countries:")
els.push(new List(denomination.useIfNoUnitGiven))
els.push(MarkdownUtils.list(denomination.useIfNoUnitGiven))
}
if (denomination.prefix) {
els.push("Prefixed")
@ -285,14 +266,14 @@ export class GenerateDocs extends Script {
if (denomination.alternativeDenominations.length > 0) {
els.push(
"Alternative denominations:",
new List(denomination.alternativeDenominations)
MarkdownUtils.list(denomination.alternativeDenominations)
)
}
}
}
this.WriteFile("./Docs/builtin_units.md", new Combine([new Title("Units", 1), ...els]), [
`assets/layers/unit/unit.json`,
this.WriteMarkdownFile("./Docs/builtin_units.md", ["# Units", ...els].join("\n\n"), [
`assets/layers/unit/unit.json`
])
}
@ -373,7 +354,7 @@ export class GenerateDocs extends Script {
if (inlineSource !== undefined) {
source = `assets/themes/${inlineSource}/${inlineSource}.json`
}
this.WriteFile("./Docs/Layers/" + layer.id + ".md", element, [source])
this.WriteMarkdownFile("./Docs/Layers/" + layer.id + ".md", element, [source])
})
}
@ -442,7 +423,7 @@ export class GenerateDocs extends Script {
"The mode the application starts in, e.g. 'map', 'dashboard' or 'statistics'"
)
this.WriteFile(
this.WriteMarkdownFile(
"./Docs/URL_Parameters.md",
QueryParameterDocumentation.GenerateQueryParameterDocs(),
["src/Logic/Web/QueryParameters.ts", "src/UI/QueryParameterDocumentation.ts"]
@ -451,7 +432,7 @@ export class GenerateDocs extends Script {
private generateBuiltinQuestions() {
const qLayer = new LayerConfig(<LayerConfigJson>questions, "questions.json", true)
this.WriteFile(
this.WriteMarkdownFile(
"./Docs/BuiltinQuestions.md",
qLayer.GenerateDocumentation([], new Map(), []),
["assets/layers/questions/questions.json"]
@ -459,27 +440,25 @@ export class GenerateDocs extends Script {
}
private generateForTheme(theme: LayoutConfig): void {
const el = new Combine([
new Title(
new Combine([
theme.title,
"(",
new Link(theme.id, "https://mapcomplete.org/" + theme.id),
")",
]),
2
),
theme.description,
const el = [
["##",
theme.title,
"(",
`[${theme.id}](https://mapcomplete.org/${theme.id})`,
")"
].join(" "),
theme.description.txt,
"This theme contains the following layers:",
new List(
MarkdownUtils.list(
theme.layers
.filter((l) => !l.id.startsWith("note_import_"))
.map((l) => new Link(l.id, "../Layers/" + l.id + ".md"))
.map((l) => (`[${l.id}](../Layers/${l.id}.md)`))
),
"Available languages:",
new List(theme.language.filter((ln) => ln !== "_context")),
]).SetClass("flex flex-col")
this.WriteFile(
MarkdownUtils.list(theme.language.filter((ln) => ln !== "_context"))
].join("\n")
this.WriteMarkdownFile(
"./Docs/Themes/" + theme.id + ".md",
el,
[`assets/themes/${theme.id}/${theme.id}.json`],
@ -533,11 +512,11 @@ export class GenerateDocs extends Script {
}
}
const el = new Combine([
new Title("Special and other useful layers", 1),
const el = [
"# Special and other useful layers",
"MapComplete has a few data layers available in the theme which have special properties through builtin-hooks. Furthermore, there are some normal layers (which are built from normal Theme-config files) but are so general that they get a mention here.",
new Title("Priviliged layers", 1),
new List(Constants.priviliged_layers.map((id) => "[" + id + "](#" + id + ")")),
"# Priviliged layers",
MarkdownUtils.list(Constants.priviliged_layers.map((id) => "[" + id + "](#" + id + ")")),
...Utils.NoNull(
Constants.priviliged_layers.map((id) => AllSharedLayers.sharedLayers.get(id))
).map((l) =>
@ -549,15 +528,15 @@ export class GenerateDocs extends Script {
Constants.no_include.indexOf(<any>l.id) < 0
)
),
new Title("Normal layers", 1),
"# Normal layers",
"The following layers are included in MapComplete:",
new List(
MarkdownUtils.list(
Array.from(AllSharedLayers.sharedLayers.keys()).map(
(id) => new Link(id, "./Layers/" + id + ".md")
(id) => `[${id}](./Layers/${id}.md)`
)
),
])
this.WriteFile("./Docs/BuiltinLayers.md", el, ["src/Customizations/AllKnownLayouts.ts"])
)
].join("\n\n")
this.WriteMarkdownFile("./Docs/BuiltinLayers.md", el, ["src/Customizations/AllKnownLayouts.ts"])
}
}