const fs=require('fs'); const [inp,out]=process.argv.slice(2); let h=fs.readFileSync(inp,'utf8'); // tokenize const re=/<(\/?)([a-zA-Z0-9]+)([^>]*?)(\/?)>/g; let res='',last=0,stack=[],strike=0,m; function emit(t){ if(strike===0) res+=t; } while((m=re.exec(h))){ emit(h.slice(last,m.index)); last=re.lastIndex; const close=m[1]==='/', tag=m[2].toLowerCase(), attrs=m[3], selfc=m[4]==='/'||/\/\s*$/.test(attrs); if(tag==='span'||tag==='del'||tag==='strike'||tag==='s'){ if(close){ const x=stack.pop(); if(x) strike--; } else if(!selfc){ const isS=tag!=='span'||/line-through/.test(attrs); stack.push(isS); if(isS) strike++; } } else if(['p','div','br','tr','li','h1','h2','h3','h4'].includes(tag)) emit('\n'); else if(tag==='td') emit('\t'); } emit(h.slice(last)); res=res.replace(/ | /g,' ').replace(/&/g,'&').replace(/</g,'<').replace(/>/g,'>').replace(/—|—/g,'—').replace(/&#(\d+);/g,(a,b)=>String.fromCharCode(+b)).replace(/"/g,'"').replace(/'|’/g,"'"); res=res.replace(/[ \t]+\n/g,'\n').replace(/\n{3,}/g,'\n\n'); fs.writeFileSync(out,res);