feat: rewrite all JS in haxe

This commit is contained in:
LordMZTE 2024-08-21 17:41:58 +02:00
parent e24830d4de
commit f9c2addf99
Signed by: LordMZTE
GPG key ID: B64802DC33A64FF6
16 changed files with 205 additions and 61 deletions

5
Makefile Normal file
View file

@ -0,0 +1,5 @@
build: confgen.lua src resources hx/out/index.js hx/out/article.js
confgen confgen.lua build
hx/out/%.js: hx/%.hxml hx/src hx/common.hxml
cd $(dir $<) && haxe $(notdir $<) && cd .. && mv $(basename $@).min.js $@

View file

@ -4,6 +4,8 @@ local cmark = require "cmark"
cg.addPath("src", ".")
cg.addPath("resources", ".")
cg.addPath("hx/out", ".")
cg.doTemplateFile "lib.cgt"
cg.opt.doctype = "<!DOCTYPE html>"

View file

@ -42,26 +42,65 @@
)
{ };
packages.haxelib-deps = pkgs.stdenvNoCC.mkDerivation {
name = "haxelib-deps";
src = ./hx/build.hxml;
dontUnpack = true;
nativeBuildInputs = with pkgs; [ haxe curl git ];
buildPhase = ''
export HOME="$(pwd)"
echo "$out" > .haxelib
mkdir "$out"
{ yes || true; } | haxelib install "$src"
'';
fixupPhase = ''
# Delete non-deterministic .git directories
cd "$out"
find -type d -name .git | xargs rm -rf
'';
GIT_SSL_CAINFO = "${pkgs.cacert}/etc/ssl/certs/ca-bundle.crt";
outputHashMode = "recursive";
outputHash = "sha256-FkMEF43ClO2wWg1+P/qD6PX+4zK3+okfsgse9SJ1idk=";
};
packages.default = pkgs.stdenvNoCC.mkDerivation {
name = "homepage";
src = ./.;
dontConfigure = true;
dontFixup = true;
nativeBuildInputs = with pkgs; [
confgen.packages.${system}.default
packages.cmark-lua
luajitPackages.luafilesystem
nodePackages.uglify-js
gnumake
haxe
aha
bat
];
configurePhase = ''
# Haxelib setup
export HOME=$TMPDIR
echo '${packages.haxelib-deps}' > ~/.haxelib
'';
buildPhase = ''
cd $src
export XDG_CONFIG_HOME=$src/.config
export XDG_CACHE_HOME=$TMPDIR
bat cache --build
confgen confgen.lua $out
make -j$(nproc)
'';
installPhase = ''
mv build $out
'';
};
@ -69,6 +108,8 @@
buildInputs = with pkgs; [
luajit
confgen.packages.${system}.default
nodePackages.uglify-js
gnumake
luajitPackages.luafilesystem
packages.cmark-lua
aha

1
hx/.gitignore vendored Normal file
View file

@ -0,0 +1 @@
out

3
hx/article.hxml Normal file
View file

@ -0,0 +1,3 @@
common.hxml
-main ArticleScript
-js out/article.js

3
hx/build.hxml Normal file
View file

@ -0,0 +1,3 @@
# This file exists only for haxe-language-server
common.hxml
-js /dev/null

3
hx/common.hxml Normal file
View file

@ -0,0 +1,3 @@
-cp src
-lib uglifyjs
-D js-es=6

3
hx/index.hxml Normal file
View file

@ -0,0 +1,3 @@
common.hxml
-main IndexScript
-js out/index.js

20
hx/src/ArticleScript.hx Normal file
View file

@ -0,0 +1,20 @@
import js.Browser.document;
import js.Browser.navigator;
function main() {
addCopyButtons();
}
/**
Adds copy buttons to codeblocks.
**/
function addCopyButtons() {
for (cb in document.getElementsByClassName("codeblock")) {
final codeEl = cb.getElementsByTagName("code")[0];
final btnEl = document.createAnchorElement();
btnEl.innerText = "[cpy]";
btnEl.classList.add("codecopybtn");
btnEl.onclick = () -> navigator.clipboard.writeText(codeEl.innerText);
cb.prepend(btnEl);
}
}

59
hx/src/IndexScript.hx Normal file
View file

@ -0,0 +1,59 @@
import js.html.Element;
import js.Browser.window;
import js.Browser.document;
import lib.Article;
using Lambda;
function main() {
loadArticles();
}
typedef Articles = {
var articles:Array<Article>;
};
/**
Adds the articles from the JSON index to the page.
**/
function loadArticles() {
final artdiv = document.getElementById("articles");
window
.fetch("/articles.json")
.then(r -> r.json())
.then((j:Articles) -> j.articles.iter(a -> artdiv.appendChild(makeArticleElement(a))));
}
/**
Creates an Element for an article to be added to the site.
**/
function makeArticleElement(art:Article):Element {
final aEl = document.createAnchorElement();
aEl.href = '/a/${art.id}/';
aEl.classList.add("article_a");
final divEl = document.createDivElement();
divEl.classList.add("article");
{
final rowEl = document.createDivElement();
rowEl.classList.add("row");
{
final headerEl = document.createElement("h2");
headerEl.innerText = art.title;
rowEl.appendChild(headerEl);
final dateEl = document.createParagraphElement();
dateEl.classList.add("date");
dateEl.innerText = art.date;
rowEl.appendChild(dateEl);
}
divEl.appendChild(rowEl);
final summaryEl = document.createParagraphElement();
summaryEl.innerText = art.summary;
divEl.appendChild(summaryEl);
}
aEl.appendChild(divEl);
return aEl;
}

9
hx/src/lib/Article.hx Normal file
View file

@ -0,0 +1,9 @@
package lib;
@:from @:to @:forward
abstract Article({
var title:String;
var id:String;
var summary:String;
var date:String;
}) {}

51
hxformat.json Normal file
View file

@ -0,0 +1,51 @@
{
"wrapping": {
"arrayWrap": {
"defaultWrap": "onePerLine",
"rules": [
{
"conditions": [
{
"cond": "totalItemLength <= n",
"value": 50
}
],
"type": "noWrap"
}
]
},
"methodChain": {
"rules": [
{
"conditions": [
{
"cond": "itemCount >= n",
"value": 3
}
],
"type": "onePerLine"
}
]
},
"callParameter": {
"rules": [
{
"conditions": [
{
"cond": "itemCount >= n",
"value": 3
},
{
"cond": "totalItemLength >= n",
"value": 50
}
],
"type": "onePerLine"
}
]
}
},
"indentation": {
"character": " "
}
}

View file

@ -5,7 +5,6 @@
<meta charset="UTF-8" />
<title><% meta.title %></title>
<link rel="stylesheet" href="/article.css" type="text/css" media="all" />
<script src="/article.js"></script>
</head>
<body>
@ -19,7 +18,7 @@
<div id="content">
<% opt.renderMarkdown(inner) %>
</div>
<script>addCopyButtons();</script>
<script src="/article.js"></script>
</body>
</html>
<! end) end end !>

View file

@ -1,12 +0,0 @@
function addCopyButtons() {
for (let cb of document.getElementsByClassName("codeblock")) {
let cbcode = cb.getElementsByTagName("code")[0];
let copy_btn = document.createElement("a");
copy_btn.innerText = "[cpy]";
copy_btn.classList.add("codecopybtn");
copy_btn.onclick = (ev) => {
navigator.clipboard.writeText(cbcode.innerText);
};
cb.prepend(copy_btn);
}
}

View file

@ -10,7 +10,6 @@
<head>
<link rel="stylesheet" href="index.css" type="text/css" media="all" />
<meta charset="UTF-8" />
<script src="index.js"></script>
<title>LordMZTE</title>
</head>
@ -52,7 +51,7 @@ If you want to chat, you can also reach me on matrix (`@lordmzte:mzte.de`).
<hr />
<div id="articles">
</div>
<script>loadArticles();</script>
<script src="index.js"></script>
</body>
</html>

View file

@ -1,42 +0,0 @@
// TODO: rewrite in Haxe
function makeArticleElement(article) {
let aEl = document.createElement("a");
aEl.href = `/a/${article.id}/`;
aEl.classList.add("article_a");
let divEl = document.createElement("div");
divEl.classList.add("article");
{
let rowEl = document.createElement("div");
rowEl.classList.add("row");
{
let headerEl = document.createElement("h2");
headerEl.innerText = article.title;
rowEl.appendChild(headerEl);
let dateEl = document.createElement("p");
dateEl.classList.add("date");
dateEl.innerText = article.date;
rowEl.append(dateEl);
}
divEl.appendChild(rowEl);
let summaryEl = document.createElement("p");
summaryEl.innerText = article.summary;
divEl.appendChild(summaryEl);
}
aEl.appendChild(divEl);
return aEl;
}
function loadArticles() {
let artdiv = document.getElementById("articles");
fetch("articles.json")
.then((res) => res.json())
.then((json) => {
json.articles.reverse().forEach((article) => {
artdiv.appendChild(makeArticleElement(article));
});
});
}