|
| 1 | +import * as fs from "node:fs"; |
| 2 | +import * as process from "node:process"; |
| 3 | +import * as tinyglobby from "tinyglobby"; |
| 4 | + |
| 5 | +const warn = (...args: [string, ...string[]]) => |
| 6 | + console.warn( |
| 7 | + args |
| 8 | + .map((a) => |
| 9 | + a.replace(/^reference[/]latest[/]src/, "@src").replace("/com/example/docs/", "/.../") |
| 10 | + ) |
| 11 | + .join("\n ") |
| 12 | + ); |
| 13 | + |
| 14 | +const PRIORITY = [ |
| 15 | + "index.md", |
| 16 | + "basics.md", |
| 17 | + "setup.md", |
| 18 | + "first-block.md", |
| 19 | + "first-entity.md", |
| 20 | + "first-item.md", |
| 21 | +].reverse(); |
| 22 | + |
| 23 | +const { compare } = Intl.Collator(); |
| 24 | + |
| 25 | +const sorter = (a: string, b: string) => { |
| 26 | + const as = a.split("/").map((f) => (f.endsWith(".md") ? f : f + "/")); |
| 27 | + const bs = b.split("/").map((f) => (f.endsWith(".md") ? f : f + "/")); |
| 28 | + |
| 29 | + while (as.length * bs.length !== 0) { |
| 30 | + const aFragment = as.shift()!; |
| 31 | + const bFragment = bs.shift()!; |
| 32 | + |
| 33 | + if ([...aFragment].reverse()[0] !== [...bFragment].reverse()[0]) { |
| 34 | + if (aFragment.endsWith(".md")) return -10.1; |
| 35 | + return +10.1; |
| 36 | + } |
| 37 | + |
| 38 | + const comparison = |
| 39 | + PRIORITY.indexOf(bFragment) - PRIORITY.indexOf(aFragment) || compare(aFragment, bFragment); |
| 40 | + if (comparison !== 0) return comparison; |
| 41 | + } |
| 42 | + |
| 43 | + return as.length - bs.length; |
| 44 | +}; |
| 45 | + |
| 46 | +const pathToContentMap = Object.fromEntries( |
| 47 | + tinyglobby |
| 48 | + .globSync(["contributing.md", "develop/**/*.md", "players/**/*.md"]) |
| 49 | + .sort(sorter) |
| 50 | + .map((f) => [f, fs.readFileSync(f, "utf-8")] as const) |
| 51 | + .filter(([_, c]) => c.split("@[").length !== 1) |
| 52 | +); |
| 53 | + |
| 54 | +const javaToNewMap: Record<string, string> = {}; |
| 55 | + |
| 56 | +for (const f of Object.keys(pathToContentMap)) { |
| 57 | + const newContent = pathToContentMap[f].replaceAll( |
| 58 | + /@\[code([^\]]*)\]\(([^\)]+)\)/g, |
| 59 | + (old, ...args) => { |
| 60 | + const config: Record<"highlight" | "transcludeWith", string> = Object.fromEntries( |
| 61 | + (args[0]! as string) |
| 62 | + .split(" ") |
| 63 | + .filter(Boolean) |
| 64 | + .filter((x) => !["java", "lang=java", "transclude"].includes(x)) |
| 65 | + .map((kv) => kv.split("=", 2)) |
| 66 | + ); |
| 67 | + |
| 68 | + const includedPath = (args[1] as string).replace(/^@\//, ""); |
| 69 | + |
| 70 | + if ("transclude" in config) { |
| 71 | + warn("LINE TRANSCLUSION DETECTED", f, includedPath); |
| 72 | + return old; |
| 73 | + } |
| 74 | + |
| 75 | + let replacement = `<<< @/${includedPath}`; |
| 76 | + if ("transcludeWith" in config) { |
| 77 | + const region = config.transcludeWith |
| 78 | + .replace(/^[:_#]+/, "") |
| 79 | + .replace(/:+$/, "") |
| 80 | + .replaceAll("_", "-") |
| 81 | + .replaceAll(":", "--") |
| 82 | + .replace("Lightning", "lightning") |
| 83 | + .replace("hudLayer", "hud-layer") |
| 84 | + .replace("registerMenu", "register-menu") |
| 85 | + .replace("providerImplemented", "provider-implemented") |
| 86 | + .replace("registerScreens", "register-screens") |
| 87 | + .replace("gameruleClass", "gamerule-class") |
| 88 | + .replace(/^$/, "TODO-give-me-a-name"); |
| 89 | + |
| 90 | + if (!/^[a-z0-9-]+$/.test(region) && region !== "TODO-give-me-a-name") { |
| 91 | + warn("WEIRD REGION", region, f); |
| 92 | + } |
| 93 | + |
| 94 | + replacement += "#"; |
| 95 | + replacement += region; |
| 96 | + |
| 97 | + const splits = ( |
| 98 | + javaToNewMap[includedPath as keyof typeof javaToNewMap] |
| 99 | + || fs.readFileSync(includedPath, "utf-8") |
| 100 | + ).split(config.transcludeWith); |
| 101 | + |
| 102 | + if (splits.length % 2 === 0) { |
| 103 | + warn("ODD NUMBER OF COMMENTS", region, includedPath, f); |
| 104 | + } |
| 105 | + |
| 106 | + for (let i = 0; i < splits.length - 1; i++) { |
| 107 | + if (!/(\n[\t ]*(?:\/\/|#)) ?$/.test(splits[i])) { |
| 108 | + warn("WEIRD PRE-TRANSCLUSION", region, includedPath, f); |
| 109 | + } |
| 110 | + |
| 111 | + splits[i] = splits[i].replace( |
| 112 | + /(\n[\t ]*(?:\/\/|#)) ?$/, |
| 113 | + (_, group) => group + ` #${i % 2 !== 0 ? "end" : ""}region ${region}` |
| 114 | + ); |
| 115 | + } |
| 116 | + |
| 117 | + for (const post of splits.slice(1)) { |
| 118 | + if (!/^\n/.test(post)) { |
| 119 | + warn("WEIRD POST-TRANSCLUSION", region, includedPath, f); |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + javaToNewMap[includedPath] = splits.join(""); |
| 124 | + } |
| 125 | + |
| 126 | + if ("highlight" in config) { |
| 127 | + replacement += config.highlight; |
| 128 | + } |
| 129 | + |
| 130 | + return replacement; |
| 131 | + } |
| 132 | + ); |
| 133 | + |
| 134 | + if (process.env.DO_IT) { |
| 135 | + fs.writeFileSync(f, newContent); |
| 136 | + } |
| 137 | +} |
| 138 | + |
| 139 | +if (process.env.DO_IT) { |
| 140 | + for (const f of Object.keys(javaToNewMap)) { |
| 141 | + fs.writeFileSync(f, javaToNewMap[f]); |
| 142 | + } |
| 143 | +} |
0 commit comments