Skip to content

Commit

Permalink
fix broken go template if statements
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
hrfee committed Jan 8, 2022
1 parent e86f5f4 commit 1ebc648
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 19 deletions.
2 changes: 1 addition & 1 deletion html/admin.html
Expand Up @@ -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">
Expand Down
38 changes: 38 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions package.json
Expand Up @@ -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",
Expand Down
52 changes: 34 additions & 18 deletions scripts/missing-colors.js
@@ -1,48 +1,64 @@
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;
}
}
return false;
};


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];
Expand Down

0 comments on commit 1ebc648

Please sign in to comment.