From 607c9ea6cf21d2a587dcab136327d6d62b8198bd Mon Sep 17 00:00:00 2001 From: flashwave Date: Fri, 13 Oct 2023 18:01:07 +0000 Subject: [PATCH] Template folder restructuring. --- .gitignore | 2 + assets/assproc.js | 100 ++++++++++++++++++ assets/utils.js | 31 ++++++ package.json | 8 ++ public/index.php | 15 +++ src/Contacts/ContactsRoutes.php | 2 +- src/HomeRoutes.php | 4 +- src/NowListeningRoutes.php | 2 +- src/Projects/ProjectsRoutes.php | 2 +- src/Tools/AsciiRoutes.php | 2 +- src/Whois/WhoisRoutes.php | 2 +- templates/500.html | 16 +++ templates/{ascii.twig => ascii/index.twig} | 2 +- templates/ascii/master.twig | 1 + templates/{ => dev}/contact.twig | 2 +- templates/{home/index.twig => dev/home.twig} | 2 +- templates/{ => dev}/index.twig | 2 +- templates/dev/master.twig | 1 + templates/{ => dev}/np.twig | 2 +- templates/{ => dev}/projects.twig | 2 +- templates/errors/master.twig | 2 +- .../{home/master.twig => master-2021.twig} | 0 templates/{whois.twig => whois/index.twig} | 2 +- templates/whois/master.twig | 1 + 24 files changed, 190 insertions(+), 15 deletions(-) create mode 100644 assets/assproc.js create mode 100644 assets/utils.js create mode 100644 package.json create mode 100644 templates/500.html rename templates/{ascii.twig => ascii/index.twig} (99%) create mode 100644 templates/ascii/master.twig rename templates/{ => dev}/contact.twig (97%) rename templates/{home/index.twig => dev/home.twig} (98%) rename templates/{ => dev}/index.twig (99%) create mode 100644 templates/dev/master.twig rename templates/{ => dev}/np.twig (84%) rename templates/{ => dev}/projects.twig (98%) rename templates/{home/master.twig => master-2021.twig} (100%) rename templates/{whois.twig => whois/index.twig} (96%) create mode 100644 templates/whois/master.twig diff --git a/.gitignore b/.gitignore index 9218895..9e8a83f 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,5 @@ /.debug /config/*.ini /vendor +/public/assets +/assets/current.json diff --git a/assets/assproc.js b/assets/assproc.js new file mode 100644 index 0000000..d4144b9 --- /dev/null +++ b/assets/assproc.js @@ -0,0 +1,100 @@ +const fs = require('fs'); +const path = require('path'); +const readline = require('readline'); +const utils = require('./utils.js'); + +exports.process = async function(root, options) { + const macroPrefix = options.prefix || '#'; + const entryPoint = options.entry || ''; + + root = fs.realpathSync(root); + + const included = []; + + const processFile = async function(fileName) { + const fullPath = path.join(root, fileName); + if(included.includes(fullPath)) + return ''; + included.push(fullPath); + + if(!fullPath.startsWith(root)) { + console.error('INVALID PATH: ' + fullPath); + return '/* *** INVALID PATH: ' + fullPath + ' */'; + } + + if(!fs.existsSync(fullPath)) { + console.error('FILE NOT FOUND: ' + fullPath); + return '/* *** FILE NOT FOUND: ' + fullPath + ' */'; + } + + const lines = readline.createInterface({ + input: fs.createReadStream(fullPath), + crlfDelay: Infinity, + }); + + let output = ''; + let lastWasEmpty = false; + + if(options.showPath) + output += "/* *** PATH: " + fullPath + " */\n"; + + for await(const line of lines) { + const lineTrimmed = utils.trim(line); + if(lineTrimmed === '') + continue; + + if(line.startsWith(macroPrefix)) { + const args = lineTrimmed.split(' '); + const macro = utils.trim(utils.trimStart(args.shift(), macroPrefix)); + + switch(macro) { + case 'comment': + break; + + case 'include': { + const includePath = utils.trimEnd(args.join(' '), ';'); + output += utils.trim(await processFile(includePath)); + output += "\n"; + break; + } + + default: + output += line; + output += "\n"; + break; + } + } else { + output += line; + output += "\n"; + } + } + + return output; + }; + + return await processFile(entryPoint); +}; + +exports.housekeep = function(assetsPath) { + const files = fs.readdirSync(assetsPath).map(fileName => { + const stats = fs.statSync(path.join(assetsPath, fileName)); + return { + name: fileName, + lastMod: stats.mtimeMs, + }; + }).sort((a, b) => b.lastMod - a.lastMod).map(info => info.name); + + const regex = /^(.+)-([a-f0-9]+)\.(.+)$/i; + const counts = {}; + + for(const fileName of files) { + const match = fileName.match(regex); + if(match) { + const name = match[1] + '-' + match[3]; + counts[name] = (counts[name] || 0) + 1; + + if(counts[name] > 5) + fs.unlinkSync(path.join(assetsPath, fileName)); + } else console.log(`Encountered file name in assets folder with unexpected format: ${fileName}`); + } +}; diff --git a/assets/utils.js b/assets/utils.js new file mode 100644 index 0000000..fb7a1dd --- /dev/null +++ b/assets/utils.js @@ -0,0 +1,31 @@ +const crypto = require('crypto'); + +const trim = function(str, chars, flags) { + if(chars === undefined) + chars = " \n\r\t\v\0"; + + let start = 0, + end = str.length; + + if(flags & 0x01) + while(start < end && chars.indexOf(str[start]) >= 0) + ++start; + + if(flags & 0x02) + while(end > start && chars.indexOf(str[end - 1]) >= 0) + --end; + + return (start > 0 || end < str.length) + ? str.substring(start, end) + : str; +}; + +exports.trimStart = (str, chars) => trim(str, chars, 0x01); +exports.trimEnd = (str, chars) => trim(str, chars, 0x02); +exports.trim = (str, chars) => trim(str, chars, 0x03); + +exports.shortHash = function(text) { + const hash = crypto.createHash('sha256'); + hash.update(text); + return hash.digest('hex').substring(0, 8); +}; diff --git a/package.json b/package.json new file mode 100644 index 0000000..391b867 --- /dev/null +++ b/package.json @@ -0,0 +1,8 @@ +{ + "dependencies": { + "@swc/core": "^1.3.69", + "autoprefixer": "^10.4.14", + "cssnano": "^6.0.1", + "postcss": "^8.4.26" + } +} diff --git a/public/index.php b/public/index.php index 3fcf49f..2a31e92 100644 --- a/public/index.php +++ b/public/index.php @@ -3,4 +3,19 @@ namespace Makai; require_once __DIR__ . '/../makai.php'; +set_exception_handler(function(\Throwable $ex) { + http_response_code(500); + ob_clean(); + + if(MKI_DEBUG) { + header('Content-Type: text/plain; charset=utf-8'); + echo (string)$ex; + exit; + } + + header('Content-Type: text/html; charset=utf-8'); + echo file_get_contents(MKI_DIR_TEMPLATES . '/500.html'); + exit; +}); + $makai->createRouting()->dispatch(); diff --git a/src/Contacts/ContactsRoutes.php b/src/Contacts/ContactsRoutes.php index c79a110..5a98b15 100644 --- a/src/Contacts/ContactsRoutes.php +++ b/src/Contacts/ContactsRoutes.php @@ -13,7 +13,7 @@ class ContactsRoutes extends RouteHandler { #[Route('GET', '/contact')] public function getContact(): string { - return $this->templating->render('contact', [ + return $this->templating->render('dev/contact', [ 'contacts' => $this->contacts->getContacts(), ]); } diff --git a/src/HomeRoutes.php b/src/HomeRoutes.php index b36e0e6..e59efa9 100644 --- a/src/HomeRoutes.php +++ b/src/HomeRoutes.php @@ -49,7 +49,7 @@ class HomeRoutes extends RouteHandler { take: 3, ); - return $this->templating->render('index', [ + return $this->templating->render('dev/index', [ 'projects' => $projects, 'contacts' => $contacts, ]); @@ -57,7 +57,7 @@ class HomeRoutes extends RouteHandler { #[Route('GET', '/home')] public function getPersonalHome(): string { - return $this->templating->render('home/index'); + return $this->templating->render('dev/home'); } // wow this is pretty quirky diff --git a/src/NowListeningRoutes.php b/src/NowListeningRoutes.php index c81ab83..a9cbec6 100644 --- a/src/NowListeningRoutes.php +++ b/src/NowListeningRoutes.php @@ -12,7 +12,7 @@ class NowListeningRoutes extends RouteHandler { #[Route('GET', '/now-listening')] public function getIndex($response, $request): string { - return $this->templating->render('np', [ + return $this->templating->render('dev/np', [ 'header_offset' => (int)$request->getParam('offset', FILTER_SANITIZE_NUMBER_INT), ]); } diff --git a/src/Projects/ProjectsRoutes.php b/src/Projects/ProjectsRoutes.php index 54e424f..19fe4cf 100644 --- a/src/Projects/ProjectsRoutes.php +++ b/src/Projects/ProjectsRoutes.php @@ -31,7 +31,7 @@ class ProjectsRoutes extends RouteHandler { ], ]; - return $this->templating->render('projects', [ + return $this->templating->render('dev/projects', [ 'sections' => $sections, ]); } diff --git a/src/Tools/AsciiRoutes.php b/src/Tools/AsciiRoutes.php index 557a813..b9fd88f 100644 --- a/src/Tools/AsciiRoutes.php +++ b/src/Tools/AsciiRoutes.php @@ -16,7 +16,7 @@ class AsciiRoutes extends RouteHandler { $this->templating->addFilter('decoct', 'decoct'); $this->templating->addFilter('dechex', 'dechex'); - return $this->templating->render('ascii'); + return $this->templating->render('ascii/index'); } #[Route('GET', '/ascii.php')] diff --git a/src/Whois/WhoisRoutes.php b/src/Whois/WhoisRoutes.php index bc996f7..3f5d302 100644 --- a/src/Whois/WhoisRoutes.php +++ b/src/Whois/WhoisRoutes.php @@ -12,7 +12,7 @@ class WhoisRoutes extends RouteHandler { #[Route('GET', '/whois')] public function getIndex(): string { - return $this->templating->render('whois'); + return $this->templating->render('whois/index'); } #[Route('GET', '/whois/lookup')] diff --git a/templates/500.html b/templates/500.html new file mode 100644 index 0000000..2f36815 --- /dev/null +++ b/templates/500.html @@ -0,0 +1,16 @@ + + + + + Error 500 + + + +

Error 500

+

Something went very wrong. Please let me know about this.

+ + diff --git a/templates/ascii.twig b/templates/ascii/index.twig similarity index 99% rename from templates/ascii.twig rename to templates/ascii/index.twig index 6aa69f7..41dcdd6 100644 --- a/templates/ascii.twig +++ b/templates/ascii/index.twig @@ -1,4 +1,4 @@ -{% extends 'master.twig' %} +{% extends 'ascii/master.twig' %} {% set header_title = 'flash.moe / ascii table' %} {% set header_minimal = true %} diff --git a/templates/ascii/master.twig b/templates/ascii/master.twig new file mode 100644 index 0000000..dbcc819 --- /dev/null +++ b/templates/ascii/master.twig @@ -0,0 +1 @@ +{% extends 'master-2021.twig' %} diff --git a/templates/contact.twig b/templates/dev/contact.twig similarity index 97% rename from templates/contact.twig rename to templates/dev/contact.twig index 0037500..0aaa99b 100644 --- a/templates/contact.twig +++ b/templates/dev/contact.twig @@ -1,4 +1,4 @@ -{% extends 'master.twig' %} +{% extends 'dev/master.twig' %} {% set header_title = 'flash.moe / contact' %} diff --git a/templates/home/index.twig b/templates/dev/home.twig similarity index 98% rename from templates/home/index.twig rename to templates/dev/home.twig index a952e97..c39c570 100644 --- a/templates/home/index.twig +++ b/templates/dev/home.twig @@ -1,4 +1,4 @@ -{% extends 'home/master.twig' %} +{% extends 'dev/master.twig' %} {% set header_title = 'flash.moe / homepage' %} {% set header_full = true %} diff --git a/templates/index.twig b/templates/dev/index.twig similarity index 99% rename from templates/index.twig rename to templates/dev/index.twig index 1f44a6e..4596cd8 100644 --- a/templates/index.twig +++ b/templates/dev/index.twig @@ -1,4 +1,4 @@ -{% extends 'master.twig' %} +{% extends 'dev/master.twig' %} {% set header_title = 'flash.moe' %} {% set header_is_index = true %} diff --git a/templates/dev/master.twig b/templates/dev/master.twig new file mode 100644 index 0000000..dbcc819 --- /dev/null +++ b/templates/dev/master.twig @@ -0,0 +1 @@ +{% extends 'master-2021.twig' %} diff --git a/templates/np.twig b/templates/dev/np.twig similarity index 84% rename from templates/np.twig rename to templates/dev/np.twig index 0709da1..0dac22e 100644 --- a/templates/np.twig +++ b/templates/dev/np.twig @@ -1,4 +1,4 @@ -{% extends 'master.twig' %} +{% extends 'dev/master.twig' %} {% set header_title = 'flash.moe / now listening' %} {% set header_full = true %} diff --git a/templates/projects.twig b/templates/dev/projects.twig similarity index 98% rename from templates/projects.twig rename to templates/dev/projects.twig index 21dcc27..041a4b2 100644 --- a/templates/projects.twig +++ b/templates/dev/projects.twig @@ -1,4 +1,4 @@ -{% extends 'master.twig' %} +{% extends 'dev/master.twig' %} {% set header_title = 'flash.moe / projects' %} diff --git a/templates/errors/master.twig b/templates/errors/master.twig index f183844..8551693 100644 --- a/templates/errors/master.twig +++ b/templates/errors/master.twig @@ -1,4 +1,4 @@ -{% extends 'master.twig' %} +{% extends 'master-2021.twig' %} {% block container %}
diff --git a/templates/home/master.twig b/templates/master-2021.twig similarity index 100% rename from templates/home/master.twig rename to templates/master-2021.twig diff --git a/templates/whois.twig b/templates/whois/index.twig similarity index 96% rename from templates/whois.twig rename to templates/whois/index.twig index 916a5df..f6d3d5a 100644 --- a/templates/whois.twig +++ b/templates/whois/index.twig @@ -1,4 +1,4 @@ -{% extends 'master.twig' %} +{% extends 'whois/master.twig' %} {% set header_title = 'flash.moe / whois' %} {% set header_logo_flash = 'flash.moe ' %} diff --git a/templates/whois/master.twig b/templates/whois/master.twig new file mode 100644 index 0000000..dbcc819 --- /dev/null +++ b/templates/whois/master.twig @@ -0,0 +1 @@ +{% extends 'master-2021.twig' %}