// TODO: Port this script to Rust as part of `tools/editor-message-tree/src/main.rs` /* eslint-disable no-console */ import fs from "fs"; import path from "path"; type Entry = { level: number; text: string; link: string | undefined }; /// Parses a single line of the input text. function parseLine(line: string) { const linkRegex = /`([^`]+)`$/; const linkMatch = line.match(linkRegex); let link = undefined; if (linkMatch) { const filePath = linkMatch[1].replace(/\\/g, "/"); link = `https://github.com/GraphiteEditor/Graphite/blob/master/${filePath}`; } const textContent = line .replace(/^[\s│├└─]*/, "") .replace(linkRegex, "") .trim(); const indentation = line.indexOf(textContent); // Each level of indentation is 4 characters. const level = Math.floor(indentation / 4); return { level, text: textContent, link }; } /// Recursively builds the HTML list from the parsed nodes. function buildHtmlList(nodes: Entry[], currentIndex: number, currentLevel: number) { if (currentIndex >= nodes.length) { return { html: "", nextIndex: currentIndex }; } let html = "\n"; return { html, nextIndex: i }; } function escapeHtml(text: string) { return text.replace(//g, ">"); } const inputFile = process.argv[2]; const outputFile = process.argv[3]; if (!inputFile || !outputFile) { console.error("Error: Please provide the input text and output HTML file paths as arguments."); console.log("Usage: node generate-editor-structure.ts "); process.exit(1); } if (!fs.existsSync(inputFile)) { console.error(`Error: File not found at "${inputFile}"`); process.exit(1); } try { const fileContent = fs.readFileSync(inputFile, "utf-8"); const lines = fileContent.split(/\r?\n/).filter((line) => line.trim() !== "" && !line.startsWith("// filepath:")); const parsedNodes = lines.map(parseLine); const { html } = buildHtmlList(parsedNodes, 0, 0); fs.writeFileSync(outputFile, html, "utf-8"); console.log(`Successfully generated HTML outline at: ${outputFile}`); } catch (error) { console.error("An error occurred during processing:", error); process.exit(1); }