|
| 1 | +#!/usr/bin/env node |
| 2 | +const fs = require('fs'); |
| 3 | +const path = require('path'); |
| 4 | + |
| 5 | +function resolveTargetFile(provided) { |
| 6 | + if (provided) return path.resolve(provided); |
| 7 | + return path.join(__dirname, '..', 'src', 'pages', 'svgViewer', 'index.jsx'); |
| 8 | +} |
| 9 | + |
| 10 | +function readFile(fp) { |
| 11 | + try { |
| 12 | + return fs.readFileSync(fp, 'utf8'); |
| 13 | + } catch (e) { |
| 14 | + console.error('Cannot read file:', fp); |
| 15 | + process.exit(2); |
| 16 | + } |
| 17 | +} |
| 18 | + |
| 19 | +function writeFile(fp, content) { |
| 20 | + fs.writeFileSync(fp, content, 'utf8'); |
| 21 | +} |
| 22 | + |
| 23 | +function findSvgSource(content) { |
| 24 | + const m = content.match(/const\s+SVG_SOURCE\s*=\s*`([\s\S]*?)`;/m); |
| 25 | + return m ? { fullMatch: m[0], svg: m[1], index: m.index } : null; |
| 26 | +} |
| 27 | + |
| 28 | +function showContext(svg, idx, label) { |
| 29 | + if (idx < 0) { |
| 30 | + console.log(`${label}: not found`); |
| 31 | + return; |
| 32 | + } |
| 33 | + const start = Math.max(0, idx - 200); |
| 34 | + const end = Math.min(svg.length, idx + 200); |
| 35 | + console.log(`\n${label} context (around index ${idx}):\n---START---\n${svg.slice(start, end)}\n---END---`); |
| 36 | +} |
| 37 | + |
| 38 | +function cmd_check(fp) { |
| 39 | + const content = readFile(fp); |
| 40 | + const found = findSvgSource(content); |
| 41 | + if (!found) { |
| 42 | + console.error('Cannot find SVG_SOURCE in file'); |
| 43 | + process.exit(2); |
| 44 | + } |
| 45 | + const svg = found.svg; |
| 46 | + function countTag(tag) { |
| 47 | + const openRe = new RegExp('<' + tag + '[\\s>]', 'gi'); |
| 48 | + const closeRe = new RegExp('</' + tag + '>', 'gi'); |
| 49 | + const opens = (svg.match(openRe) || []).length; |
| 50 | + const closes = (svg.match(closeRe) || []).length; |
| 51 | + return { opens, closes }; |
| 52 | + } |
| 53 | + const tags = ['svg','g','foreignObject','div','span','p','br']; |
| 54 | + const counts = {}; |
| 55 | + for (const t of tags) counts[t] = countTag(t); |
| 56 | + console.log('Tag counts:'); |
| 57 | + for (const t of tags) console.log(` ${t}: open=${counts[t].opens} close=${counts[t].closes}`); |
| 58 | + |
| 59 | + const lastG = svg.lastIndexOf('<g'); |
| 60 | + const lastCloseG = svg.lastIndexOf('</g>'); |
| 61 | + const lastCloseSvg = svg.lastIndexOf('</svg>'); |
| 62 | + console.log('\nPositions (char index):'); |
| 63 | + console.log(' last <g at', lastG); |
| 64 | + console.log(' last </g> at', lastCloseG); |
| 65 | + console.log(' last </svg> at', lastCloseSvg); |
| 66 | + |
| 67 | + showContext(svg, lastG, '<g (last)'); |
| 68 | + showContext(svg, lastCloseG, '</g> (last)'); |
| 69 | + showContext(svg, lastCloseSvg, '</svg> (last)'); |
| 70 | + |
| 71 | + // stack-based scan for unclosed <g> |
| 72 | + const tagRegex = /<\/?g[\s>]/gi; |
| 73 | + let stack = []; |
| 74 | + let m; |
| 75 | + while ((m = tagRegex.exec(svg)) !== null) { |
| 76 | + if (m[0].startsWith('</')) { |
| 77 | + if (stack.length === 0) { |
| 78 | + console.log('\nStray </g> at', m.index); |
| 79 | + showContext(svg, m.index, 'stray </g>'); |
| 80 | + break; |
| 81 | + } else { |
| 82 | + stack.pop(); |
| 83 | + } |
| 84 | + } else { |
| 85 | + stack.push(m.index); |
| 86 | + } |
| 87 | + } |
| 88 | + if (stack.length > 0) { |
| 89 | + const lastUnclosed = stack[stack.length - 1]; |
| 90 | + console.log('\nFound unclosed <g> (last open at index', lastUnclosed + ')'); |
| 91 | + showContext(svg, lastUnclosed, 'unclosed <g>'); |
| 92 | + } else { |
| 93 | + console.log('\nAll <g> tags appear closed (no unclosed <g> found)'); |
| 94 | + } |
| 95 | +} |
| 96 | + |
| 97 | +function cmd_replace_br(fp) { |
| 98 | + const content = readFile(fp); |
| 99 | + const found = findSvgSource(content); |
| 100 | + if (!found) { |
| 101 | + console.error('Cannot find SVG_SOURCE'); |
| 102 | + process.exit(2); |
| 103 | + } |
| 104 | + const svg = found.svg; |
| 105 | + const replaced = svg.replace(/<br>/g, '<br/>'); |
| 106 | + if (replaced === svg) { |
| 107 | + console.log('No <br> occurrences found to replace'); |
| 108 | + process.exit(0); |
| 109 | + } |
| 110 | + const newContent = content.replace(found.fullMatch, `const SVG_SOURCE = ` + '`' + replaced + '`;'); |
| 111 | + writeFile(fp, newContent); |
| 112 | + console.log('Replaced <br> with <br/> in SVG_SOURCE'); |
| 113 | +} |
| 114 | + |
| 115 | +function cmd_fix_unclosed_g(fp) { |
| 116 | + const content = readFile(fp); |
| 117 | + const found = findSvgSource(content); |
| 118 | + if (!found) { |
| 119 | + console.error('Cannot find SVG_SOURCE'); |
| 120 | + process.exit(2); |
| 121 | + } |
| 122 | + let svg = found.svg; |
| 123 | + const openCount = (svg.match(/<g[\s>]/gi) || []).length; |
| 124 | + const closeCount = (svg.match(/<\/g>/gi) || []).length; |
| 125 | + const missing = openCount - closeCount; |
| 126 | + if (missing <= 0) { |
| 127 | + console.log('No missing </g> tags detected (open:', openCount, 'close:', closeCount, ')'); |
| 128 | + process.exit(0); |
| 129 | + } |
| 130 | + // insert missing number of </g> before the last </svg> |
| 131 | + const lastCloseSvgIdx = svg.lastIndexOf('</svg>'); |
| 132 | + if (lastCloseSvgIdx === -1) { |
| 133 | + console.error('No </svg> found, aborting'); |
| 134 | + process.exit(2); |
| 135 | + } |
| 136 | + const inserts = '</g>'.repeat(missing); |
| 137 | + const newSvg = svg.slice(0, lastCloseSvgIdx) + inserts + svg.slice(lastCloseSvgIdx); |
| 138 | + const newContent = content.replace(found.fullMatch, `const SVG_SOURCE = ` + '`' + newSvg + '`;'); |
| 139 | + writeFile(fp, newContent); |
| 140 | + console.log(`Inserted ${missing} </g> before closing </svg>`); |
| 141 | +} |
| 142 | + |
| 143 | +function usage() { |
| 144 | + console.log('Usage: node scripts/svg_tools.cjs <command> [--file=path]'); |
| 145 | + console.log('Commands:'); |
| 146 | + console.log(' check : analyze tag counts and find unclosed <g>'); |
| 147 | + console.log(' replace-br : replace <br> with <br/> inside SVG_SOURCE'); |
| 148 | + console.log(' fix-unclosed-g : insert missing </g> before </svg> to balance <g> tags'); |
| 149 | + console.log('\nOptions:'); |
| 150 | + console.log(' --file=PATH : specify target file (defaults to src/pages/svgViewer/index.jsx)'); |
| 151 | +} |
| 152 | + |
| 153 | +function main() { |
| 154 | + const rawArgs = process.argv.slice(2); |
| 155 | + if (rawArgs.length === 0) return usage(); |
| 156 | + let cmd = rawArgs[0]; |
| 157 | + let fileArg = null; |
| 158 | + for (let i = 1; i < rawArgs.length; i++) { |
| 159 | + const a = rawArgs[i]; |
| 160 | + if (a.startsWith('--file=')) fileArg = a.split('=')[1]; |
| 161 | + else if (a === '--file' && rawArgs[i+1]) { fileArg = rawArgs[i+1]; i++; } |
| 162 | + } |
| 163 | + const fp = resolveTargetFile(fileArg); |
| 164 | + if (!fs.existsSync(fp)) { |
| 165 | + console.error('Target file does not exist:', fp); |
| 166 | + process.exit(2); |
| 167 | + } |
| 168 | + if (cmd === 'check') return cmd_check(fp); |
| 169 | + if (cmd === 'replace-br') return cmd_replace_br(fp); |
| 170 | + if (cmd === 'fix-unclosed-g') return cmd_fix_unclosed_g(fp); |
| 171 | + usage(); |
| 172 | +} |
| 173 | + |
| 174 | +main(); |
0 commit comments