1
0
mirror of https://github.com/hrfee/jfa-go.git synced 2024-12-23 09:30:12 +00:00
jfa-go/scripts/missing-colors.js

84 lines
2.9 KiB
JavaScript
Raw Normal View History

let parser = require("cheerio");
2021-12-30 23:52:53 +00:00
let fs = require("fs");
let path = require("path");
let pre = require("perl-regex");
2021-12-30 23:52:53 +00:00
const hasDark = (item) => {
let list = item.attr("class").split(/\s+/);
for (let i = 0; i < list.length; i++) {
if (list[i].substring(0,5) == "dark:") {
return true;
}
}
return false;
};
2022-01-08 00:22:21 +00:00
if (typeof String.prototype.replaceAll === "undefined") {
String.prototype.replaceAll = function(match, replace) {
return this.replace(new RegExp(match, 'g'), () => replace);
}
}
function fixHTML(infile, outfile) {
let f = fs.readFileSync(infile).toString();
// Find all go template strings ({{ example }})
let templateStrings = pre.exec(f, "(?s){{(?:(?!{{).)*?}}", "gi");
for (let i = 0; i < templateStrings.length; i++) {
let s = templateStrings[i].replace(/\\/g, '');
// let s = templateStrings[i];
f = f.replaceAll(s, "<!--" + s.slice(3).slice(0, -3) + "-->");
}
let doc = new parser.load(f);
2022-01-01 02:17:24 +00:00
for (let item of ["badge", "chip", "shield", "input", "table", "button", "portal", "select", "aside", "card", "field", "textarea"]) {
let items = doc("."+item);
items.each((i, elem) => {
2021-12-30 23:52:53 +00:00
let hasColor = false;
for (let color of ["neutral", "positive", "urge", "warning", "info", "critical"]) {
//console.log(color);
if (doc(elem).hasClass("~"+color)) {
2021-12-30 23:52:53 +00:00
hasColor = true;
// console.log("adding to", items[i].classList)
if (!hasDark(doc(elem))) {
doc(elem).addClass("dark:~d_"+color);
}
2021-12-30 23:52:53 +00:00
break;
}
}
if (!hasColor) {
if (!hasDark(doc(elem))) {
2021-12-31 17:28:08 +00:00
// card without ~neutral look different than with.
if (item != "card") doc(elem).addClass("~neutral");
doc(elem).addClass("dark:~d_neutral");
}
2021-12-30 23:52:53 +00:00
}
if (!doc(elem).hasClass("@low") && !doc(elem).hasClass("@high")) {
doc(elem).addClass("@low");
2021-12-30 23:52:53 +00:00
}
});
}
let out = doc.html();
// let out = f
for (let i = 0; i < templateStrings.length; i++) {
let s = templateStrings[i].replace(/\\/g, '');
out = out.replaceAll("<!--" + s.slice(3).slice(0, -3) + "-->", s);
2021-12-30 23:52:53 +00:00
}
2022-01-10 00:46:01 +00:00
out = out.replaceAll("&lt;!--", "{{");
out = out.replaceAll("--&gt;", "}}");
fs.writeFileSync(outfile, out);
console.log(infile, outfile);
2021-12-30 23:52:53 +00:00
};
let inpath = process.argv[process.argv.length-2];
let outpath = process.argv[process.argv.length-1];
2022-01-04 21:13:09 +00:00
if (fs.statSync(inpath).isDirectory()) {
let files = fs.readdirSync(inpath);
for (let i = 0; i < files.length; i++) {
if (files[i].indexOf(".html")>=0) {
fixHTML(path.join(inpath, files[i]), path.join(outpath, files[i]));
}
2021-12-30 23:52:53 +00:00
}
2022-01-04 21:13:09 +00:00
} else {
fixHTML(inpath, outpath);
2021-12-30 23:52:53 +00:00
}