Files
Docs-v2/autogen/mdgen.js

216 lines
6.3 KiB
JavaScript
Raw Normal View History

2025-12-29 00:14:11 +07:00
const fs = require("fs")
const path = require("path")
2025-12-30 01:10:38 +07:00
const yaml = require("yaml")
2025-12-29 00:14:11 +07:00
2025-12-30 01:10:38 +07:00
const yamlAPIPath = path.join(__dirname, "../", "yaml", "types")
2025-12-29 00:14:11 +07:00
const mdAPIPath = path.join(__dirname, "../", "docs/api", "types")
2025-12-29 21:51:30 +07:00
const iconDataPath = path.join(__dirname, "../", "docs/theme/.icons", "polytoria")
2025-12-30 01:10:38 +07:00
const yamlEnumPath = path.join(__dirname, "../", "yaml", "enums")
2025-12-29 00:14:11 +07:00
const mdEnumPath = path.join(__dirname, "../", "docs/api", "enums")
2026-01-29 15:27:19 +07:00
// Cleanup md (excluding index.md files)
if (fs.existsSync(mdAPIPath)) {
const files = fs.readdirSync(mdAPIPath)
for (const file of files) {
if (file !== 'index.md') {
const filePath = path.join(mdAPIPath, file)
fs.rmSync(filePath, { recursive: true, force: true })
}
}
}
if (fs.existsSync(mdEnumPath)) {
const files = fs.readdirSync(mdEnumPath)
for (const file of files) {
if (file !== 'index.md') {
const filePath = path.join(mdEnumPath, file)
fs.rmSync(filePath, { recursive: true, force: true })
}
}
}
// Create directories
2025-12-29 00:14:11 +07:00
if (!fs.existsSync(mdAPIPath)) {
fs.mkdirSync(mdAPIPath, { recursive: true })
}
2025-12-30 01:10:38 +07:00
if (!fs.existsSync(yamlAPIPath)) {
fs.mkdirSync(yamlAPIPath, { recursive: true })
2025-12-29 00:14:11 +07:00
}
if (!fs.existsSync(mdEnumPath)) {
fs.mkdirSync(mdEnumPath, { recursive: true })
}
2025-12-30 01:10:38 +07:00
if (!fs.existsSync(yamlEnumPath)) {
fs.mkdirSync(yamlEnumPath, { recursive: true })
2025-12-29 00:14:11 +07:00
}
// Process API Classes
2025-12-30 01:10:38 +07:00
const yamlFiles = fs.readdirSync(yamlAPIPath).filter(file => file.endsWith('.yaml'));
2025-12-29 00:14:11 +07:00
2025-12-30 01:10:38 +07:00
for (const yamlFile of yamlFiles) {
const yamlPath = path.join(yamlAPIPath, yamlFile)
const yamlContent = fs.readFileSync(yamlPath, "utf-8")
2025-12-29 00:14:11 +07:00
2025-12-30 01:10:38 +07:00
const c = yaml.parse(yamlContent);
const className = path.basename(yamlFile, '.yaml')
2025-12-29 00:14:11 +07:00
let mdPath = path.join(mdAPIPath, className + ".md")
let mk = ""
2025-12-29 21:51:30 +07:00
const iconPath = path.join(iconDataPath, c.Name + ".svg")
2025-12-29 21:54:12 +07:00
const emojiExists = fs.existsSync(iconPath)
2025-12-29 00:14:11 +07:00
function appendLine(str) {
mk += str + "\n"
}
appendLine("---")
appendLine("title: " + c.Name)
appendLine("description:")
2025-12-29 21:54:12 +07:00
if (emojiExists) {
2025-12-29 21:51:30 +07:00
appendLine("icon: polytoria/" + c.Name)
} else {
appendLine("icon: polytoria/Unknown")
}
2025-12-29 00:14:11 +07:00
appendLine("---")
appendLine("")
2025-12-29 21:54:12 +07:00
if (emojiExists) {
appendLine(`# :polytoria-${c.Name}: ` + c.Name)
} else {
appendLine("# " + c.Name)
}
2025-12-29 00:14:11 +07:00
if (c.BaseType) {
appendLine("")
appendLine(`{{ inherits("${c.BaseType}") }}`)
}
appendLine("")
appendLine(c.Description)
appendLine("")
if (c.IsStatic) {
2026-01-05 21:44:52 +07:00
appendLine("")
appendLine(`{{ staticclass(${c.StaticAlias ? `"${c.StaticAlias}"` : ""}) }}`)
2025-12-29 00:14:11 +07:00
appendLine("")
}
if (c.IsAbstract) {
appendLine("{{ abstract() }}")
appendLine("")
}
if (!c.IsInstantiatable) {
appendLine("{{ notnewable() }}")
appendLine("")
}
const properties = c.Properties ? (Array.isArray(c.Properties) ? c.Properties : [c.Properties]) : [];
2025-12-29 22:08:24 +07:00
if (properties.length > 0) {
appendLine("")
appendLine("## Properties")
appendLine("")
}
2025-12-29 00:14:11 +07:00
for (const prop of properties) {
appendLine(`### ${prop.Name}:${prop.Type} { property }`)
appendLine(``)
appendLine(prop.Description || "Missing documentation!")
appendLine(``)
}
const methods = c.Methods ? (Array.isArray(c.Methods) ? c.Methods : [c.Methods]) : [];
2025-12-29 22:08:24 +07:00
if (methods.length > 0) {
appendLine("")
appendLine("## Methods")
appendLine("")
}
2025-12-29 00:14:11 +07:00
for (const m of methods) {
if (m.IsObsolete) continue
let params = []
const parameters = m.Parameters ? (Array.isArray(m.Parameters) ? m.Parameters : [m.Parameters]) : [];
for (const p of parameters) {
params.push(`${p.Name};${p.Type}${p.IsOptional ? "?" : ""}`)
}
appendLine(`### ${m.Name}(${params.join(",")}):${m.ReturnType || "void"} { method }`)
appendLine(``)
appendLine(m.Description || "Missing documentation!")
appendLine(``)
}
const events = c.Events ? (Array.isArray(c.Events) ? c.Events : [c.Events]) : [];
2025-12-29 22:08:24 +07:00
if (events.length > 0) {
appendLine("")
appendLine("## Events")
appendLine("")
}
2025-12-29 00:14:11 +07:00
for (const e of events) {
let args = []
const aargs = e.Arguments ? (Array.isArray(e.Arguments) ? e.Arguments : [e.Arguments]) : [];
for (const arg of aargs) {
args.push(`${arg.Name};${arg.Type}`)
}
appendLine(`### ${e.Name}(${args.join(",")}) { event }`)
appendLine(``)
appendLine(e.Description || "")
appendLine(``)
}
fs.writeFileSync(mdPath, mk)
}
2026-01-22 23:07:24 +07:00
console.log(`Converted ${yamlFiles.length} YAML files to Markdown`)
2025-12-29 00:14:11 +07:00
// Process Enums
2025-12-30 01:10:38 +07:00
const yamlEnumFiles = fs.readdirSync(yamlEnumPath).filter(file => file.endsWith('.yaml'));
2025-12-29 00:14:11 +07:00
2025-12-30 01:10:38 +07:00
for (const yamlFile of yamlEnumFiles) {
const yamlPath = path.join(yamlEnumPath, yamlFile);
const yamlContent = fs.readFileSync(yamlPath, "utf-8");
2025-12-29 00:14:11 +07:00
2025-12-30 01:10:38 +07:00
const e = yaml.parse(yamlContent);
const enumName = path.basename(yamlFile, '.yaml');
2025-12-29 00:14:11 +07:00
let mdPath = path.join(mdEnumPath, enumName + ".md")
let mk = ""
function appendLine(str) {
mk += str + "\n"
}
appendLine("---")
appendLine("title: " + e.Name)
appendLine("description: " + (e.Description && e.Description !== "Missing Documentation" ? e.Description : ""))
2025-12-29 19:52:18 +07:00
appendLine("icon: polytoria/Enum")
2025-12-29 00:14:11 +07:00
appendLine("---")
appendLine("")
appendLine("# " + e.Name)
appendLine("")
if (e.Description && e.Description !== "Missing Documentation") {
appendLine(e.Description)
appendLine("")
}
appendLine("| Name | Description |")
appendLine("| --- | --- |")
const options = e.Options ? (Array.isArray(e.Options) ? e.Options : [e.Options]) : [];
for (const option of options) {
const optionName = typeof option === 'string' ? option : option.Name;
const optionDesc = typeof option === 'string' ? "" : (option.Description || "");
const displayDesc = optionDesc === "Missing Documentation" ? "" : optionDesc;
appendLine(`| \`${e.Name}.${optionName}\` | ${displayDesc} |`)
}
fs.writeFileSync(mdPath, mk)
}
2026-01-22 23:07:24 +07:00
console.log(`Converted ${yamlEnumFiles.length} enum YAML files to Markdown`)