| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | |
| 10 | |
| 11 | |
| 12 | |
| 13 | |
| 14 | |
| 15 | |
| 16 | |
| 17 | |
| 18 | |
| 19 | |
| 20 | |
| 21 | |
| 22 | |
| 23 | |
| 24 | |
| 25 | |
| 26 | |
| 27 | |
| 28 | |
| 29 | import FileWriter from './FileWriter.js'; |
| 30 | import NavHtmlBuilder from './NavHtmlBuilder.js'; |
| 31 | import theLinkBuilder from './LinkBuilder.js'; |
| 32 | import theConfig from './Config.js'; |
| 33 | |
| 34 | |
| 35 | |
| 36 | |
| 37 | |
| 38 | |
| 39 | |
| 40 | class SourceHtmlBuilder { |
| 41 | |
| 42 | |
| 43 | |
| 44 | |
| 45 | |
| 46 | |
| 47 | #sourcesCounter; |
| 48 | |
| 49 | |
| 50 | |
| 51 | |
| 52 | |
| 53 | |
| 54 | static get #jsKeywords ( ) { |
| 55 | return [ |
| 56 | 'async', |
| 57 | 'await', |
| 58 | 'break', |
| 59 | 'class', |
| 60 | 'const', |
| 61 | 'constructor', |
| 62 | 'export', |
| 63 | 'for', |
| 64 | 'get', |
| 65 | 'import', |
| 66 | 'let', |
| 67 | 'new', |
| 68 | 'return', |
| 69 | 'set', |
| 70 | 'static', |
| 71 | 'switch', |
| 72 | 'this', |
| 73 | 'throw', |
| 74 | 'try', |
| 75 | 'catch', |
| 76 | 'while' |
| 77 | ]; |
| 78 | } |
| 79 | |
| 80 | |
| 81 | |
| 82 | |
| 83 | |
| 84 | |
| 85 | #rootPath; |
| 86 | |
| 87 | |
| 88 | |
| 89 | |
| 90 | |
| 91 | |
| 92 | #fileContent; |
| 93 | |
| 94 | |
| 95 | |
| 96 | |
| 97 | |
| 98 | #colorizeJSKeywords ( ) { |
| 99 | SourceHtmlBuilder.#jsKeywords.forEach ( |
| 100 | keyword => { |
| 101 | const regexp = new RegExp ( '(?<=[\n| |,|;|.])' + keyword + '(?=[ |,|;|.])', 'g' ); |
| 102 | this.#fileContent = this.#fileContent.replaceAll ( regexp, '<span class="js-keyword">' + keyword + '</span>' ); |
| 103 | } |
| 104 | ); |
| 105 | } |
| 106 | |
| 107 | |
| 108 | |
| 109 | |
| 110 | |
| 111 | #setClassesLinks ( ) { |
| 112 | theLinkBuilder.classesLinks.forEach ( |
| 113 | classLink => { |
| 114 | const regexp = new RegExp ( '(?<=[\n| |,|;|.|{])' + classLink [ 0 ] + '(?=[ |,|;|.|}|\r|\n])', 'g' ); |
| 115 | this.#fileContent = this.#fileContent.replaceAll ( |
| 116 | regexp, |
| 117 | `<a class="class-link" href="${this.#rootPath + classLink [ 1 ]}">` + classLink [ 0 ] + '</a>' |
| 118 | ); |
| 119 | } |
| 120 | ); |
| 121 | } |
| 122 | |
| 123 | |
| 124 | |
| 125 | |
| 126 | |
| 127 | #setVariablesLinks ( ) { |
| 128 | theLinkBuilder.variablesLinks.forEach ( |
| 129 | variableLink => { |
| 130 | const regexp = new RegExp ( '(?<=[\n| |,|;|.])' + variableLink [ 0 ] + '(?=[ |,|;|.|\r|\n])', 'g' ); |
| 131 | this.#fileContent = this.#fileContent.replaceAll ( |
| 132 | regexp, |
| 133 | `<a class="variable-link" href="${this.#rootPath + variableLink [ 1 ]}">` + variableLink [ 0 ] + '</a>' |
| 134 | ); |
| 135 | } |
| 136 | ); |
| 137 | } |
| 138 | |
| 139 | |
| 140 | |
| 141 | |
| 142 | |
| 143 | constructor ( ) { |
| 144 | Object.freeze ( this ); |
| 145 | this.#sourcesCounter = 0; |
| 146 | } |
| 147 | |
| 148 | |
| 149 | |
| 150 | |
| 151 | |
| 152 | |
| 153 | get sourcesCounter ( ) { return this.#sourcesCounter; } |
| 154 | |
| 155 | |
| 156 | |
| 157 | |
| 158 | |
| 159 | |
| 160 | |
| 161 | |
| 162 | build ( fileContent, fileName, tagsData ) { |
| 163 | |
| 164 | this.#sourcesCounter ++; |
| 165 | |
| 166 | |
| 167 | |
| 168 | |
| 169 | |
| 170 | const dirs = fileName.split ( '/' ); |
| 171 | const htmlFileName = dirs.pop ( ).split ( '.' ) [ 0 ] + 'js.html'; |
| 172 | this.#rootPath = ''; |
| 173 | dirs.forEach ( ( ) => this.#rootPath += '../' ); |
| 174 | |
| 175 | const navHtmlBuilder = new NavHtmlBuilder ( ); |
| 176 | |
| 177 | |
| 178 | let html = |
| 179 | '<!DOCTYPE html><html><head><meta charset="UTF-8">' + |
| 180 | `<link type="text/css" rel="stylesheet" href="${this.#rootPath}ESSimpleDoc.css"></head><body>`; |
| 181 | |
| 182 | |
| 183 | html += navHtmlBuilder.build ( this.#rootPath ); |
| 184 | |
| 185 | |
| 186 | html += `<h1>File : ${fileName}</h1>`; |
| 187 | |
| 188 | if ( tagsData ) { |
| 189 | |
| 190 | |
| 191 | |
| 192 | tagsData.sort ( |
| 193 | ( first, second ) => { |
| 194 | if ( first.line === second.line ) { |
| 195 | if ( null === second.column ) { |
| 196 | return -1; |
| 197 | } |
| 198 | else if ( null === first.column ) { |
| 199 | return 1; |
| 200 | } |
| 201 | return second.column - first.column; |
| 202 | } |
| 203 | |
| 204 | return first.line - second.line; |
| 205 | |
| 206 | } |
| 207 | ); |
| 208 | |
| 209 | |
| 210 | let lines = fileContent.split ( /\r\n|\r|\n/ ); |
| 211 | |
| 212 | |
| 213 | let previousTag = { line : -1, column : -1, tag : '' }; |
| 214 | tagsData.forEach ( |
| 215 | tag => { |
| 216 | if ( previousTag.line !== tag.line || previousTag.column !== tag.column ) { |
| 217 | let line = lines [ tag.line - 1 ]; |
| 218 | if ( null === tag.column ) { |
| 219 | line += tag.tag; |
| 220 | } |
| 221 | else { |
| 222 | line = line.substring ( 0, tag.column ) + tag.tag + line.substring ( tag.column ); |
| 223 | } |
| 224 | lines [ tag.line - 1 ] = line; |
| 225 | } |
| 226 | |
| 227 | previousTag = tag; |
| 228 | } |
| 229 | ); |
| 230 | |
| 231 | |
| 232 | this.#fileContent = ''; |
| 233 | lines.forEach ( |
| 234 | line => this.#fileContent += line + '\n' |
| 235 | ); |
| 236 | } |
| 237 | else { |
| 238 | this.#fileContent = fileContent; |
| 239 | } |
| 240 | |
| 241 | this.#fileContent = this.#fileContent |
| 242 | |
| 243 | |
| 244 | .replaceAll ( '\t', ' ' ) |
| 245 | |
| 246 | |
| 247 | .replaceAll ( /\u003c/g, '<' ) |
| 248 | .replaceAll ( /\u003e/g, '>' ) |
| 249 | .replaceAll ( /\u0022/g, '"' ) |
| 250 | .replaceAll ( /\u0027/g, ''' ) |
| 251 | |
| 252 | |
| 253 | .replaceAll ( /, '<' ) |
| 254 | .replaceAll ( />/g, '>' ) |
| 255 | .replaceAll ( /"/g, '"' ); |
| 256 | |
| 257 | |
| 258 | if ( ! theConfig.noSourcesColor ) { |
| 259 | this.#colorizeJSKeywords ( ); |
| 260 | this.#setClassesLinks ( ); |
| 261 | this.#setVariablesLinks ( ); |
| 262 | } |
| 263 | |
| 264 | |
| 265 | let lineCounter = 0; |
| 266 | html += '<table class="src-code">'; |
| 267 | |
| 268 | |
| 269 | this.#fileContent.split ( /\r\n|\r|\n/ ).forEach ( |
| 270 | line => { |
| 271 | |
| 272 | |
| 273 | lineCounter ++; |
| 274 | |
| 275 | const strLineCounter = String ( lineCounter ).padStart ( 5, '_' ); |
| 276 | html += |
| 277 | `<tr><td><a id="L${strLineCounter}">${lineCounter}</a></td>` + |
| 278 | `<td><pre>${line}</pre></td></tr>`; |
| 279 | } |
| 280 | ); |
| 281 | |
| 282 | html += '</table>'; |
| 283 | |
| 284 | |
| 285 | html += navHtmlBuilder.footer; |
| 286 | html += |
| 287 | '<script>document.getElementById ( new URL ( window.location' + |
| 288 | ' ).hash.substring( 1 ) )?.parentNode.classList.add ( \'hash\' )</script>'; |
| 289 | html += '</body></html>'; |
| 290 | |
| 291 | |
| 292 | new FileWriter ( ).write ( dirs, htmlFileName, html ); |
| 293 | |
| 294 | } |
| 295 | } |
| 296 | |
| 297 | export default SourceHtmlBuilder; |
| 298 | |
| 299 | |
| 300 | |