1
0
mirror of https://github.com/hrfee/jfa-go.git synced 2024-12-22 09:00:10 +00:00

fix broken go template if statements

All available DOM parsers for node would move the contents of if
statements outside of them, breaking things like the accounts tab. Fixed
with a regex pre and post process to comment out then uncomment all template usage.

builds now depend on perl for some regex, this can likely be changed in
future though.
This commit is contained in:
Harvey Tindall 2022-01-08 00:07:23 +00:00
parent e86f5f4c3c
commit 1ebc648158
Signed by: hrfee
GPG Key ID: BBC65952848FB1A2
4 changed files with 76 additions and 19 deletions

View File

@ -15,8 +15,8 @@
window.linkResetEnabled = {{ .linkResetEnabled }};
window.language = "{{ .langName }}";
</script>
{{ template "header.html" . }}
<title>Admin - jfa-go</title>
{{ template "header.html" . }}
</head>
<body class="max-w-full overflow-x-hidden section">
<div id="modal-login" class="modal">

38
package-lock.json generated
View File

@ -13,12 +13,15 @@
"@types/node": "^15.0.1",
"a17t": "^0.10.1",
"browserslist": "^4.16.6",
"cheerio": "^1.0.0-rc.10",
"esbuild": "^0.8.57",
"fs-cheerio": "^3.0.0",
"inline-source": "^7.2.0",
"jsdom": "^19.0.0",
"lodash": "^4.17.21",
"mjml": "^4.8.0",
"nightwind": "github:yonson2/nightwind",
"perl-regex": "^1.0.4",
"remixicon": "^2.5.0",
"remove-markdown": "^0.3.0",
"typescript": "^4.0.3",
@ -2058,6 +2061,14 @@
"integrity": "sha1-g8YK/Fi5xWmXAH7Rp2izqzA6RP4=",
"dev": true
},
"node_modules/fs-cheerio": {
"version": "3.0.0",
"resolved": "https://registry.npmjs.org/fs-cheerio/-/fs-cheerio-3.0.0.tgz",
"integrity": "sha512-pOlWiRDIX8bc2aGVmAX2e3dH514IUEmXoM9WOZnraYEzC9uXelSCJdKo0s4+7US0rYgNTHLw4765J5ZWsKTzqA==",
"dependencies": {
"cheerio": "^1.0.0-rc.2"
}
},
"node_modules/fs.realpath": {
"version": "1.0.0",
"resolved": "https://registry.npmjs.org/fs.realpath/-/fs.realpath-1.0.0.tgz",
@ -4484,6 +4495,17 @@
"resolved": "https://registry.npmjs.org/performance-now/-/performance-now-2.1.0.tgz",
"integrity": "sha1-Ywn04OX6kT7BxpMHrjZLSzd8nns="
},
"node_modules/perl-regex": {
"version": "1.0.4",
"resolved": "https://registry.npmjs.org/perl-regex/-/perl-regex-1.0.4.tgz",
"integrity": "sha1-cQvC7fh23qd+TQQ951CsdAv0Z9I=",
"dependencies": {
"sprintf-js": "^1.0.3"
},
"engines": {
"node": ">=4.5.0"
}
},
"node_modules/picocolors": {
"version": "1.0.0",
"resolved": "https://registry.npmjs.org/picocolors/-/picocolors-1.0.0.tgz",
@ -8597,6 +8619,14 @@
"integrity": "sha1-g8YK/Fi5xWmXAH7Rp2izqzA6RP4=",
"dev": true
},
"fs-cheerio": {
"version": "3.0.0",
"resolved": "https://registry.npmjs.org/fs-cheerio/-/fs-cheerio-3.0.0.tgz",
"integrity": "sha512-pOlWiRDIX8bc2aGVmAX2e3dH514IUEmXoM9WOZnraYEzC9uXelSCJdKo0s4+7US0rYgNTHLw4765J5ZWsKTzqA==",
"requires": {
"cheerio": "^1.0.0-rc.2"
}
},
"fs.realpath": {
"version": "1.0.0",
"resolved": "https://registry.npmjs.org/fs.realpath/-/fs.realpath-1.0.0.tgz",
@ -10522,6 +10552,14 @@
"resolved": "https://registry.npmjs.org/performance-now/-/performance-now-2.1.0.tgz",
"integrity": "sha1-Ywn04OX6kT7BxpMHrjZLSzd8nns="
},
"perl-regex": {
"version": "1.0.4",
"resolved": "https://registry.npmjs.org/perl-regex/-/perl-regex-1.0.4.tgz",
"integrity": "sha1-cQvC7fh23qd+TQQ951CsdAv0Z9I=",
"requires": {
"sprintf-js": "^1.0.3"
}
},
"picocolors": {
"version": "1.0.0",
"resolved": "https://registry.npmjs.org/picocolors/-/picocolors-1.0.0.tgz",

View File

@ -21,12 +21,15 @@
"@types/node": "^15.0.1",
"a17t": "^0.10.1",
"browserslist": "^4.16.6",
"cheerio": "^1.0.0-rc.10",
"esbuild": "^0.8.57",
"fs-cheerio": "^3.0.0",
"inline-source": "^7.2.0",
"jsdom": "^19.0.0",
"lodash": "^4.17.21",
"mjml": "^4.8.0",
"nightwind": "github:yonson2/nightwind",
"perl-regex": "^1.0.4",
"remixicon": "^2.5.0",
"remove-markdown": "^0.3.0",
"typescript": "^4.0.3",

View File

@ -1,10 +1,12 @@
let parser = require("jsdom");
let parser = require("cheerio");
let fs = require("fs");
let path = require("path");
let pre = require("perl-regex");
const hasDark = (item) => {
for (let i = 0; i < item.classList.length; i++) {
if (item.classList[i].substring(0,5) == "dark:") {
let list = item.attr("class").split(/\s+/);
for (let i = 0; i < list.length; i++) {
if (list[i].substring(0,5) == "dark:") {
return true;
}
}
@ -12,37 +14,51 @@ const hasDark = (item) => {
};
const fixHTML = (infile, outfile) => {
console.log(infile, outfile)
let doc = new parser.JSDOM(fs.readFileSync(infile));
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);
for (let item of ["badge", "chip", "shield", "input", "table", "button", "portal", "select", "aside", "card", "field", "textarea"]) {
let items = doc.window.document.body.querySelectorAll("."+item);
for (let i = 0; i < items.length; i++) {
let items = doc("."+item);
items.each((i, elem) => {
let hasColor = false;
for (let color of ["neutral", "positive", "urge", "warning", "info", "critical"]) {
//console.log(color);
if (items[i].classList.contains("~"+color)) {
if (doc(elem).hasClass("~"+color)) {
hasColor = true;
// console.log("adding to", items[i].classList)
if (!hasDark(items[i])) {
items[i].classList.add("dark:~d_"+color);
if (!hasDark(doc(elem))) {
doc(elem).addClass("dark:~d_"+color);
}
break;
}
}
if (!hasColor) {
if (!hasDark(items[i])) {
if (!hasDark(doc(elem))) {
// card without ~neutral look different than with.
if (item != "card") items[i].classList.add("~neutral");
items[i].classList.add("dark:~d_neutral");
if (item != "card") doc(elem).addClass("~neutral");
doc(elem).addClass("dark:~d_neutral");
}
}
if (!items[i].classList.contains("@low") && !items[i].classList.contains("@high")) {
items[i].classList.add("@low");
if (!doc(elem).hasClass("@low") && !doc(elem).hasClass("@high")) {
doc(elem).addClass("@low");
}
});
}
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);
}
fs.writeFileSync(outfile, doc.window.document.documentElement.outerHTML);
fs.writeFileSync(outfile, out);
console.log(infile, outfile);
};
let inpath = process.argv[process.argv.length-2];