diff options
author | Jonas Kohl | 2024-07-23 16:34:35 +0200 |
---|---|---|
committer | Jonas Kohl | 2024-07-23 16:34:35 +0200 |
commit | 252e4cfa9c2236e50f38adc4611396a5665f8301 (patch) | |
tree | 17a3359490f99ecbce2548d775a782fba6c40417 | |
parent | e163c5950aa74278401679190fb7d755f730fa04 (diff) |
Add codegen & emoticons
30 files changed, 143 insertions, 6 deletions
@@ -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 @@ +<?php + +require_once __DIR__ . "/common.php"; +$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; + echo "[codegen] $cg_ent "; + $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; + } + echo "-> $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 @@ +<?php + +$em_dir = SRCDIR . "/static/emoticons"; +$emoticons = array_map( + fn($i) => [ + 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 @@ <param:active-link>guestbook</param:active-link> <div id="guestbook_frame"></div> - +<p><a href="#" id="emoticon-guide">Emoticon guide</a></p> <div id="guestbook"></div> <script type="text/javascript" src="/static/jquery-1.12.4.min.js"></script> +<script type="text/javascript" src="/emoticons.js"></script> <script> function htmlentities(str) { return str.replace(/&/g, "&") @@ -20,13 +21,56 @@ function nl2br(str, xhtml) { return str.replace(/\n/g, "<br" + (xhtml ? " /" : "") + ">"); } +function replace_emoticons(html) { + for (var i = 0; i < EMOTICONS[0].length; ++i) { + html = html.split(":" + EMOTICONS[0][i][0] + ":") + .join( + '<img src="/static/emoticons/' + EMOTICONS[0][i][0] + '.gif" ' + + EMOTICONS[0][i][1] + + ' alt="' + EMOTICONS[0][i][0] + '"' + + ' title=":' + EMOTICONS[0][i][0] + ':" />' + ); + } + return html; +} + $(function() { $("#guestbook_frame").append( $("<iframe></iframe>") .attr("id", "gbframe") .attr("frameborder", "0") - .attr("src", "//guestbook.hozyro.jkohl.link/?__e=1&__p[ok_target]=_top&__p[ok_url]=" + encodeURIComponent(location.href)) + .attr("src", "//guestbook.web.local/?__e=1&__p[ok_target]=_top&__p[ok_url]=" + encodeURIComponent(location.href)) ); + $("#emoticon-guide").click(function(e) { + e.preventDefault(); + var win = window.open("about:blank", "emguide", "width=260,height=450,left=0,top=0,popup=yes,toolbar=no,menubar=no,scrollbars=yes"); + //win.document.title = "Emoticon guide"; + win.document.open(); + win.document.write('<html><head>'); + win.document.write('<title>Emoticon guide</title>'); + win.document.write('</head><body bgcolor="#05456a">'); + win.document.write('<center>'); + win.document.write('<table border=1 bgcolor=white><tbody>'); + var idx = 0; + for (var i = 0; i < EMOTICONS[1].length; ++i) { + idx = EMOTICONS[1][i]; + win.document.write( + "<tr><td>" + + '<img src="/static/emoticons/' + EMOTICONS[0][idx][0] + '.gif" ' + + EMOTICONS[0][idx][1] + + ' alt="' + EMOTICONS[0][idx][0] + '"' + + ' title=":' + EMOTICONS[0][idx][0] + ':" />' + + "</td><td><font face=Verdana size=2>" + + ":" + EMOTICONS[0][idx][0] + ":" + + "</font></td></tr>\n" + ); + } + win.document.write('</tbody></table>'); + win.document.write('</center>'); + win.document.write('</body></html>'); + win.document.close(); + return; + }); }) function _gbcb(data) { @@ -50,9 +94,9 @@ function _gbcb(data) { .text(entry.published), $("<div></div>") .addClass("guestbook-content") - .html(nl2br(htmlentities(entry.content))) + .html(replace_emoticons(nl2br(htmlentities(entry.content)))) ).appendTo("#guestbook") }) } </script> -<script type="text/javascript" src="//guestbook.hozyro.jkohl.link/?__m=ajax&__r=jsonp&__c=_gbcb"></script> +<script type="text/javascript" src="//guestbook.web.local/?__m=ajax&__r=jsonp&__c=_gbcb"></script> diff --git a/src/static/emoticons/angel.gif b/src/static/emoticons/angel.gif Binary files differnew file mode 100644 index 0000000..f52c820 --- /dev/null +++ b/src/static/emoticons/angel.gif diff --git a/src/static/emoticons/angry.gif b/src/static/emoticons/angry.gif Binary files differnew file mode 100644 index 0000000..205ebfc --- /dev/null +++ b/src/static/emoticons/angry.gif diff --git a/src/static/emoticons/azn.gif b/src/static/emoticons/azn.gif Binary files differnew file mode 100644 index 0000000..33149fb --- /dev/null +++ b/src/static/emoticons/azn.gif diff --git a/src/static/emoticons/bang.gif b/src/static/emoticons/bang.gif Binary files differnew file mode 100644 index 0000000..cde005e --- /dev/null +++ b/src/static/emoticons/bang.gif diff --git a/src/static/emoticons/blank.gif b/src/static/emoticons/blank.gif Binary files differnew file mode 100644 index 0000000..73909d8 --- /dev/null +++ b/src/static/emoticons/blank.gif diff --git a/src/static/emoticons/cheesy.gif b/src/static/emoticons/cheesy.gif Binary files differnew file mode 100644 index 0000000..d352772 --- /dev/null +++ b/src/static/emoticons/cheesy.gif diff --git a/src/static/emoticons/cool.gif b/src/static/emoticons/cool.gif Binary files differnew file mode 100644 index 0000000..cead030 --- /dev/null +++ b/src/static/emoticons/cool.gif diff --git a/src/static/emoticons/cry.gif b/src/static/emoticons/cry.gif Binary files differnew file mode 100644 index 0000000..aaa73a6 --- /dev/null +++ b/src/static/emoticons/cry.gif diff --git a/src/static/emoticons/embarrassed.gif b/src/static/emoticons/embarrassed.gif Binary files differnew file mode 100644 index 0000000..ad76283 --- /dev/null +++ b/src/static/emoticons/embarrassed.gif diff --git a/src/static/emoticons/evil.gif b/src/static/emoticons/evil.gif Binary files differnew file mode 100644 index 0000000..502fe24 --- /dev/null +++ b/src/static/emoticons/evil.gif diff --git a/src/static/emoticons/grin.gif b/src/static/emoticons/grin.gif Binary files differnew file mode 100644 index 0000000..4f86116 --- /dev/null +++ b/src/static/emoticons/grin.gif diff --git a/src/static/emoticons/huh.gif b/src/static/emoticons/huh.gif Binary files differnew file mode 100644 index 0000000..05721b0 --- /dev/null +++ b/src/static/emoticons/huh.gif diff --git a/src/static/emoticons/kiss.gif b/src/static/emoticons/kiss.gif Binary files differnew file mode 100644 index 0000000..ad9a55b --- /dev/null +++ b/src/static/emoticons/kiss.gif diff --git a/src/static/emoticons/laugh.gif b/src/static/emoticons/laugh.gif Binary files differnew file mode 100644 index 0000000..374ba15 --- /dev/null +++ b/src/static/emoticons/laugh.gif diff --git a/src/static/emoticons/lipsrsealed.gif b/src/static/emoticons/lipsrsealed.gif Binary files differnew file mode 100644 index 0000000..448399b --- /dev/null +++ b/src/static/emoticons/lipsrsealed.gif diff --git a/src/static/emoticons/police.gif b/src/static/emoticons/police.gif Binary files differnew file mode 100644 index 0000000..4f31156 --- /dev/null +++ b/src/static/emoticons/police.gif diff --git a/src/static/emoticons/rolleyes.gif b/src/static/emoticons/rolleyes.gif Binary files differnew file mode 100644 index 0000000..d7f5f2f --- /dev/null +++ b/src/static/emoticons/rolleyes.gif diff --git a/src/static/emoticons/sad.gif b/src/static/emoticons/sad.gif Binary files differnew file mode 100644 index 0000000..bf0348c --- /dev/null +++ b/src/static/emoticons/sad.gif diff --git a/src/static/emoticons/sad2.gif b/src/static/emoticons/sad2.gif Binary files differnew file mode 100644 index 0000000..6945d80 --- /dev/null +++ b/src/static/emoticons/sad2.gif diff --git a/src/static/emoticons/shocked.gif b/src/static/emoticons/shocked.gif Binary files differnew file mode 100644 index 0000000..9f51fe0 --- /dev/null +++ b/src/static/emoticons/shocked.gif diff --git a/src/static/emoticons/shrug.gif b/src/static/emoticons/shrug.gif Binary files differnew file mode 100644 index 0000000..9e8cda6 --- /dev/null +++ b/src/static/emoticons/shrug.gif diff --git a/src/static/emoticons/smiley.gif b/src/static/emoticons/smiley.gif Binary files differnew file mode 100644 index 0000000..7b1f6d3 --- /dev/null +++ b/src/static/emoticons/smiley.gif diff --git a/src/static/emoticons/tongue.gif b/src/static/emoticons/tongue.gif Binary files differnew file mode 100644 index 0000000..1ad749f --- /dev/null +++ b/src/static/emoticons/tongue.gif diff --git a/src/static/emoticons/undecided.gif b/src/static/emoticons/undecided.gif Binary files differnew file mode 100644 index 0000000..0c49e06 --- /dev/null +++ b/src/static/emoticons/undecided.gif diff --git a/src/static/emoticons/wink.gif b/src/static/emoticons/wink.gif Binary files differnew file mode 100644 index 0000000..d148288 --- /dev/null +++ b/src/static/emoticons/wink.gif |