1- const fs = require ( "fs" ) ;
2- const path = require ( "path" ) ;
3-
4- /**
5- * Escapes characters that have special meaning in HTML.
6- * @param {string } text The text to escape.
7- * @returns {string } The escaped text.
8- */
9- function escapeHtml ( text ) {
10- return text . replace ( / < / g, "<" ) . replace ( / > / g, ">" ) ;
11- }
1+ /* eslint-disable no-console */
2+
3+ import fs from "fs" ;
4+ import path from "path" ;
125
13- /**
14- * Parses a single line of the input text.
15- * @param {string } line The line to parse.
16- * @returns {{ level: number, text: string, link: string | undefined } }
17- */
18- function parseLine ( line ) {
6+ type Entry = { level : number ; text : string ; link : string | undefined } ;
7+
8+ /// Parses a single line of the input text.
9+ function parseLine ( line : string ) {
1910 const linkRegex = / ` ( [ ^ ` ] + ) ` $ / ;
2011 const linkMatch = line . match ( linkRegex ) ;
2112 let link = undefined ;
@@ -25,22 +16,19 @@ function parseLine(line) {
2516 link = `https://github.com/GraphiteEditor/Graphite/blob/master/${ filePath } ` ;
2617 }
2718
28- const textContent = line . replace ( / ^ [ \s │ ├ └ ─ ] * / , "" ) . replace ( linkRegex , "" ) . trim ( ) ;
19+ const textContent = line
20+ . replace ( / ^ [ \s │ ├ └ ─ ] * / , "" )
21+ . replace ( linkRegex , "" )
22+ . trim ( ) ;
2923 const indentation = line . indexOf ( textContent ) ;
3024 // Each level of indentation is 4 characters.
3125 const level = Math . floor ( indentation / 4 ) ;
3226
3327 return { level, text : textContent , link } ;
3428}
3529
36- /**
37- * Recursively builds the HTML list from the parsed nodes.
38- * @param {Array } nodes The array of parsed node objects.
39- * @param {number } currentIndex The current index in the nodes array.
40- * @param {number } currentLevel The current indentation level.
41- * @returns {{html: string, nextIndex: number} }
42- */
43- function buildHtmlList ( nodes , currentIndex , currentLevel ) {
30+ /// Recursively builds the HTML list from the parsed nodes.
31+ function buildHtmlList ( nodes : Entry [ ] , currentIndex : number , currentLevel : number ) {
4432 if ( currentIndex >= nodes . length ) {
4533 return { html : "" , nextIndex : currentIndex } ;
4634 }
@@ -68,16 +56,18 @@ function buildHtmlList(nodes, currentIndex, currentLevel) {
6856 } else {
6957 escapedText = [ escapeHtml ( node . text ) ] ;
7058 }
71-
59+
7260 let role = "message" ;
7361 if ( node . link ) role = "subsystem" ;
7462 else if ( hasDeeperChildren ) role = "submessage" ;
7563 else if ( escapedText . length === 2 ) role = "field" ;
7664
7765 const partOfMessageFromNamingConvention = [ "Message" , "MessageHandler" , "MessageContext" ] . some ( ( suffix ) => node . text . replace ( / ( .* ) < .* > / g, "$1" ) . endsWith ( suffix ) ) ;
7866 const partOfMessageViolatesNamingConvention = node . link && ! partOfMessageFromNamingConvention ;
79- const violatesNamingConvention = partOfMessageViolatesNamingConvention ? "<span class=\"warn\">(violates naming convention — should end with 'Message', 'MessageHandler', or 'MessageContext')</span>" : "" ;
80-
67+ const violatesNamingConvention = partOfMessageViolatesNamingConvention
68+ ? "<span class=\"warn\">(violates naming convention — should end with 'Message', 'MessageHandler', or 'MessageContext')</span>"
69+ : "" ;
70+
8171 if ( hasDirectChildren ) {
8272 html += `<li><span class="tree-node"><span class="${ role } ">${ escapedText } </span>${ linkHtml } ${ violatesNamingConvention } </span>` ;
8373 const childResult = buildHtmlList ( nodes , i + 1 , node . level + 1 ) ;
@@ -96,12 +86,16 @@ function buildHtmlList(nodes, currentIndex, currentLevel) {
9686 return { html, nextIndex : i } ;
9787}
9888
89+ function escapeHtml ( text : string ) {
90+ return text . replace ( / < / g, "<" ) . replace ( / > / g, ">" ) ;
91+ }
92+
9993const inputFile = process . argv [ 2 ] ;
10094const outputFile = process . argv [ 3 ] ;
10195
10296if ( ! inputFile || ! outputFile ) {
10397 console . error ( "Error: Please provide the input text and output HTML file paths as arguments." ) ;
104- console . log ( "Usage: node generate-editor-structure.js <input txt> <output html>" ) ;
98+ console . log ( "Usage: node generate-editor-structure.ts <input txt> <output html>" ) ;
10599 process . exit ( 1 ) ;
106100}
107101
@@ -112,7 +106,7 @@ if (!fs.existsSync(inputFile)) {
112106
113107try {
114108 const fileContent = fs . readFileSync ( inputFile , "utf-8" ) ;
115- const lines = fileContent . split ( / \r ? \n / ) . filter ( line => line . trim ( ) !== "" && ! line . startsWith ( "// filepath:" ) ) ;
109+ const lines = fileContent . split ( / \r ? \n / ) . filter ( ( line ) => line . trim ( ) !== "" && ! line . startsWith ( "// filepath:" ) ) ;
116110 const parsedNodes = lines . map ( parseLine ) ;
117111
118112 const { html } = buildHtmlList ( parsedNodes , 0 , 0 ) ;
0 commit comments