You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Hydro/build/template.js

52 lines
1.6 KiB
JavaScript

const fs = require('fs');
const { root } = require('./utils');
function getFiles(folder) {
const files = [];
const f = fs.readdirSync(root(folder));
for (const i of f) {
if (!i.startsWith('.')) {
if (fs.statSync(root(`${folder}/${i}`)).isDirectory()) {
const g = getFiles(`${folder}/${i}`);
for (const j of g) files.push(`${i}/${j}`);
} else files.push(i);
}
}
return files;
}
5 years ago
const build = (dirOrObject, exclude = []) => {
let templates = {};
if (typeof dirOrObject === 'string') {
const files = getFiles(dirOrObject);
for (const i of files) {
const template = fs.readFileSync(root(`${dirOrObject}/${i}`)).toString();
templates[i] = template;
}
} else templates = dirOrObject;
for (const i in templates) {
4 years ago
if (!exclude.includes(i) && i.endsWith('.html')) {
5 years ago
templates[i] = templates[i]
.trim()
.replace(/ *\n */gmi, ' ')
.replace(/, /gmi, ',')
.replace(/%} {%/gmi, '%}{%')
.replace(/ %}/gmi, '%}')
.replace(/{% /gmi, '{%')
.replace(/> </gmi, '><')
.replace(/}} </gmi, '}}<')
.replace(/> {{/gmi, '>{{')
.replace(/{{ /gmi, '{{')
.replace(/ }}/gmi, '}}')
.replace(/%} </gmi, '%}<')
.replace(/> {%/gmi, '>{%')
.replace(/= /gmi, '=')
.replace(/ =/gmi, '=')
.trim();
}
}
return templates;
};
module.exports = build;