From 252e4cfa9c2236e50f38adc4611396a5665f8301 Mon Sep 17 00:00:00 2001
From: Jonas Kohl
Date: Tue, 23 Jul 2024 16:34:35 +0200
Subject: Add codegen & emoticons
---
Makefile | 6 ++--
codegen.php | 23 ++++++++++++++++
dev-server.php | 32 +++++++++++++++++++++
src/codegen/emoticons.php | 36 ++++++++++++++++++++++++
src/pages/guestbook.php | 52 ++++++++++++++++++++++++++++++++---
src/static/emoticons/angel.gif | Bin 0 -> 175 bytes
src/static/emoticons/angry.gif | Bin 0 -> 167 bytes
src/static/emoticons/azn.gif | Bin 0 -> 168 bytes
src/static/emoticons/bang.gif | Bin 0 -> 351 bytes
src/static/emoticons/blank.gif | Bin 0 -> 156 bytes
src/static/emoticons/cheesy.gif | Bin 0 -> 172 bytes
src/static/emoticons/cool.gif | Bin 0 -> 172 bytes
src/static/emoticons/cry.gif | Bin 0 -> 573 bytes
src/static/emoticons/embarrassed.gif | Bin 0 -> 650 bytes
src/static/emoticons/evil.gif | Bin 0 -> 238 bytes
src/static/emoticons/grin.gif | Bin 0 -> 173 bytes
src/static/emoticons/huh.gif | Bin 0 -> 167 bytes
src/static/emoticons/kiss.gif | Bin 0 -> 171 bytes
src/static/emoticons/laugh.gif | Bin 0 -> 336 bytes
src/static/emoticons/lipsrsealed.gif | Bin 0 -> 231 bytes
src/static/emoticons/police.gif | Bin 0 -> 171 bytes
src/static/emoticons/rolleyes.gif | Bin 0 -> 485 bytes
src/static/emoticons/sad.gif | Bin 0 -> 167 bytes
src/static/emoticons/sad2.gif | Bin 0 -> 436 bytes
src/static/emoticons/shocked.gif | Bin 0 -> 178 bytes
src/static/emoticons/shrug.gif | Bin 0 -> 998 bytes
src/static/emoticons/smiley.gif | Bin 0 -> 174 bytes
src/static/emoticons/tongue.gif | Bin 0 -> 233 bytes
src/static/emoticons/undecided.gif | Bin 0 -> 171 bytes
src/static/emoticons/wink.gif | Bin 0 -> 170 bytes
30 files changed, 143 insertions(+), 6 deletions(-)
create mode 100644 codegen.php
create mode 100644 src/codegen/emoticons.php
create mode 100644 src/static/emoticons/angel.gif
create mode 100644 src/static/emoticons/angry.gif
create mode 100644 src/static/emoticons/azn.gif
create mode 100644 src/static/emoticons/bang.gif
create mode 100644 src/static/emoticons/blank.gif
create mode 100644 src/static/emoticons/cheesy.gif
create mode 100644 src/static/emoticons/cool.gif
create mode 100644 src/static/emoticons/cry.gif
create mode 100644 src/static/emoticons/embarrassed.gif
create mode 100644 src/static/emoticons/evil.gif
create mode 100644 src/static/emoticons/grin.gif
create mode 100644 src/static/emoticons/huh.gif
create mode 100644 src/static/emoticons/kiss.gif
create mode 100644 src/static/emoticons/laugh.gif
create mode 100644 src/static/emoticons/lipsrsealed.gif
create mode 100644 src/static/emoticons/police.gif
create mode 100644 src/static/emoticons/rolleyes.gif
create mode 100644 src/static/emoticons/sad.gif
create mode 100644 src/static/emoticons/sad2.gif
create mode 100644 src/static/emoticons/shocked.gif
create mode 100644 src/static/emoticons/shrug.gif
create mode 100644 src/static/emoticons/smiley.gif
create mode 100644 src/static/emoticons/tongue.gif
create mode 100644 src/static/emoticons/undecided.gif
create mode 100644 src/static/emoticons/wink.gif
diff --git a/Makefile b/Makefile
index 86803b0..edbbc2c 100644
--- a/Makefile
+++ b/Makefile
@@ -3,13 +3,15 @@ HTMLTARGETS:=$(SRCFILES:src/pages/%.php=build/%.html)
DEV_SERVER_BINDING?=127.0.0.1:19310
-.PHONY: build/static publish dev-server
+.PHONY: build/static publish dev-server build/codegen
-all: $(HTMLTARGETS) $(STATICFILES) build/static build/favicon.ico
+all: $(HTMLTARGETS) $(STATICFILES) build/static build/favicon.ico build/codegen
build/favicon.ico: src/favicon.ico
cp src/favicon.ico build/
+build/codegen:
+ php codegen.php
build/static:
rsync -rupE src/static build/
diff --git a/codegen.php b/codegen.php
new file mode 100644
index 0000000..a842b32
--- /dev/null
+++ b/codegen.php
@@ -0,0 +1,23 @@
+ $data[0]\n";
+ file_put_contents(__DIR__ . "/build/" . $data[0], $data[1]);
+}
diff --git a/dev-server.php b/dev-server.php
index 2e5aacb..24969f6 100644
--- a/dev-server.php
+++ b/dev-server.php
@@ -8,6 +8,27 @@ const FALLBACK_FILE = TEMPLATE_DIR . "not_found.php";
const DEFAULT_FILE = TEMPLATE_DIR . "index.php";
const DEFAULT_MIME_TYPE = "application/octet-stream";
+$cg_map = [];
+$cg_dir = SRCDIR . "/codegen";
+
+foreach (scandir($cg_dir) as $cg_ent) {
+ $cg_path = $cg_dir . "/" . $cg_ent;
+ if (
+ $cg_ent[0] === "." ||
+ !is_file($cg_path) ||
+ strtolower(pathinfo($cg_ent, PATHINFO_EXTENSION)) !== "php"
+ ) continue;
+ $data = include $cg_path;
+ if (!is_array($data) || count($data) !== 2) {
+ trigger_error(
+ "Invalid data returned from codegen script '$cg_ent'",
+ E_USER_WARNING,
+ );
+ continue;
+ }
+ $cg_map[$data[0]] = $cg_path;
+}
+
function get_mime(string $filename): string {
static $mime_overrides = [
"css" => "text/css",
@@ -39,6 +60,17 @@ elseif (str_ends_with($name, ".html")) {
header("Content-Length: $len");
readfile($path);
exit;
+} elseif (isset($cg_map[$name])) {
+ $data = include $cg_map[$name];
+ if (!is_array($data) || count($data) !== 2) {
+ http_response_code(500);
+ echo "Invalid data returned from codegen script '$cg_ent'\n";
+ exit;
+ }
+ header("Content-Type: " . get_mime($data[0]));
+ header("Content-Length: " . strlen($data[1]));
+ echo $data[1];
+ exit;
}
$include_file = FALLBACK_FILE;
diff --git a/src/codegen/emoticons.php b/src/codegen/emoticons.php
new file mode 100644
index 0000000..51b9ed6
--- /dev/null
+++ b/src/codegen/emoticons.php
@@ -0,0 +1,36 @@
+ [
+ pathinfo($i, PATHINFO_FILENAME),
+ getimagesize($em_dir . "/" . $i)["3"]
+ ],
+ array_values(
+ array_filter(
+ scandir($em_dir),
+ fn($i) =>
+ $i[0] !== "." &&
+ is_file($em_dir . "/" . $i) &&
+ strtolower(pathinfo($i, PATHINFO_EXTENSION)) === "gif"
+ )
+ )
+);
+
+usort($emoticons, fn($a, $b) => strlen($b[0]) <=> strlen($a[0]));
+
+$emoticons_alpha_lookup = [];
+foreach ($emoticons as $i => $emoticon) {
+ $emoticons_alpha_lookup []= [$i, $emoticon[0]];
+}
+usort($emoticons_alpha_lookup, fn($a, $b) => $a[1] <=> $b[1]);
+$emoticons_alpha_lookup = array_column($emoticons_alpha_lookup, 0);
+
+return [
+ "emoticons.js",
+ "/* auto-generated at " . date("c") . " */\nvar EMOTICONS = " .
+ json_encode([
+ $emoticons,
+ $emoticons_alpha_lookup,
+ ], JSON_PRETTY_PRINT) . ";\n",
+];
diff --git a/src/pages/guestbook.php b/src/pages/guestbook.php
index f84b92d..2d40089 100644
--- a/src/pages/guestbook.php
+++ b/src/pages/guestbook.php
@@ -2,10 +2,11 @@