Files
sisvietnamvn_01/process_templates.js
T
2026-05-27 09:01:38 +07:00

89 lines
3.0 KiB
JavaScript

const fs = require('fs');
const path = require('path');
const cheerio = require('cheerio');
const themeDir = path.join(__dirname, 'sis_theme');
const templatesDir = path.join(themeDir, 'templates');
const layoutDir = path.join(templatesDir, 'layout');
const includesDir = path.join(templatesDir, 'includes');
// 1. Create includes directory
if (!fs.existsSync(includesDir)) {
fs.mkdirSync(includesDir, { recursive: true });
}
// 2. Extract Header and Footer from page.html.twig
const pageTwigPath = path.join(layoutDir, 'page.html.twig');
let pageTwigContent = fs.readFileSync(pageTwigPath, 'utf8');
// Regex to extract header and footer
const headerRegex = /(<!-- HEADER -->\s*<header class="header">[\s\S]*?<\/header>)/;
const footerRegex = /(<!-- FOOTER -->\s*<footer class="footer">[\s\S]*?<\/footer>)/;
const headerMatch = pageTwigContent.match(headerRegex);
const footerMatch = pageTwigContent.match(footerRegex);
if (headerMatch && footerMatch) {
fs.writeFileSync(path.join(includesDir, 'header.html.twig'), headerMatch[1]);
fs.writeFileSync(path.join(includesDir, 'footer.html.twig'), footerMatch[1]);
// Replace in page.html.twig
pageTwigContent = pageTwigContent.replace(headerMatch[1], "{% include directory ~ '/templates/includes/header.html.twig' %}");
pageTwigContent = pageTwigContent.replace(footerMatch[1], "{% include directory ~ '/templates/includes/footer.html.twig' %}");
fs.writeFileSync(pageTwigPath, pageTwigContent);
console.log('✅ Extracted header and footer successfully.');
} else {
console.log('⚠️ Could not find header or footer in page.html.twig');
}
// 3. Process UMASS_sample files
const sampleDir = path.join(__dirname, 'UMASS_sample');
const files = fs.readdirSync(sampleDir).filter(file => file.endsWith('.html'));
files.forEach(file => {
const filePath = path.join(sampleDir, file);
const htmlContent = fs.readFileSync(filePath, 'utf8');
const $ = cheerio.load(htmlContent);
let mainContent = $('main').html();
if (!mainContent) {
mainContent = '<div class="container" style="padding: 100px 0;">No main content found.</div>';
}
// Generate a slug from filename
let slug = file.toLowerCase()
.replace(' _ umass amherst.html', '')
.replace(' _ umass flex', '')
.replace('umass ', '')
.replace(' amherst', '')
.replace(/[^a-z0-9]+/g, '-')
.replace(/^-|-$/g, '');
if (slug === '' || slug === 'front' || slug === 'amherst') slug = 'front'; // To avoid conflict with default front
const newTwigContent = `{#
/**
* Page template for ${file}
*/
#}
<div class="layout-container">
{% include directory ~ '/templates/includes/header.html.twig' %}
<main role="main">
${mainContent}
</main>
{% include directory ~ '/templates/includes/footer.html.twig' %}
</div>
`;
// We write to page--[slug].html.twig
const newFilePath = path.join(layoutDir, `page--${slug}.html.twig`);
fs.writeFileSync(newFilePath, newTwigContent);
console.log(`✅ Generated template: page--${slug}.html.twig`);
});
console.log('All templates generated successfully!');