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
|
const fs = require('fs');
const path = require('path');
const inputPath = './input.html';
const htmlContent = fs.readFileSync(inputPath, 'utf-8');
const styleRegex = /<style[^>]*>([\s\S]*?)<\/style>/i;
const scriptRegex = /<script[^>]*>([\s\S]*?)<\/script>/i;
const styleMatch = htmlContent.match(styleRegex);
const scriptMatch = htmlContent.match(scriptRegex);
const cssContent = styleMatch ? styleMatch[1].trim() : '';
const jsContent = scriptMatch ? scriptMatch[1].trim() : '';
let cleanHtml = htmlContent
.replace(styleRegex, '')
.replace(scriptRegex, '');
cleanHtml = cleanHtml.replace('</head>', ' <link rel="stylesheet" href="style.css">\n</head>');
cleanHtml = cleanHtml.replace('</body>', ' <script src="script.js"></script>\n</body>');
fs.writeFileSync('./index.html', cleanHtml.trim());
fs.writeFileSync('./style.css', cssContent);
fs.writeFileSync('./script.js', jsContent);
console.log('✅ Fichiers générés : index.html, style.css, script.js'); |
Partager