Template folder restructuring.

This commit is contained in:
flash 2023-10-13 18:01:07 +00:00
parent 2050ba294b
commit 607c9ea6cf
24 changed files with 190 additions and 15 deletions

2
.gitignore vendored
View file

@ -3,3 +3,5 @@
/.debug
/config/*.ini
/vendor
/public/assets
/assets/current.json

100
assets/assproc.js Normal file
View file

@ -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}`);
}
};

31
assets/utils.js Normal file
View file

@ -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);
};

8
package.json Normal file
View file

@ -0,0 +1,8 @@
{
"dependencies": {
"@swc/core": "^1.3.69",
"autoprefixer": "^10.4.14",
"cssnano": "^6.0.1",
"postcss": "^8.4.26"
}
}

View file

@ -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();

View file

@ -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(),
]);
}

View file

@ -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

View file

@ -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),
]);
}

View file

@ -31,7 +31,7 @@ class ProjectsRoutes extends RouteHandler {
],
];
return $this->templating->render('projects', [
return $this->templating->render('dev/projects', [
'sections' => $sections,
]);
}

View file

@ -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')]

View file

@ -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')]

16
templates/500.html Normal file
View file

@ -0,0 +1,16 @@
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Error 500</title>
<style type="text/css">
body {
font: 12px/20px Verdana, Geneva, 'Dejavu Sans', Arial, Helvetica, sans-serif;
}
</style>
</head>
<body>
<h1>Error 500</h1>
<p>Something went very wrong. Please let me know about this.</p>
</body>
</html>

View file

@ -1,4 +1,4 @@
{% extends 'master.twig' %}
{% extends 'ascii/master.twig' %}
{% set header_title = 'flash.moe / ascii table' %}
{% set header_minimal = true %}

View file

@ -0,0 +1 @@
{% extends 'master-2021.twig' %}

View file

@ -1,4 +1,4 @@
{% extends 'master.twig' %}
{% extends 'dev/master.twig' %}
{% set header_title = 'flash.moe / contact' %}

View file

@ -1,4 +1,4 @@
{% extends 'home/master.twig' %}
{% extends 'dev/master.twig' %}
{% set header_title = 'flash.moe / homepage' %}
{% set header_full = true %}

View file

@ -1,4 +1,4 @@
{% extends 'master.twig' %}
{% extends 'dev/master.twig' %}
{% set header_title = 'flash.moe' %}
{% set header_is_index = true %}

View file

@ -0,0 +1 @@
{% extends 'master-2021.twig' %}

View file

@ -1,4 +1,4 @@
{% extends 'master.twig' %}
{% extends 'dev/master.twig' %}
{% set header_title = 'flash.moe / now listening' %}
{% set header_full = true %}

View file

@ -1,4 +1,4 @@
{% extends 'master.twig' %}
{% extends 'dev/master.twig' %}
{% set header_title = 'flash.moe / projects' %}

View file

@ -1,4 +1,4 @@
{% extends 'master.twig' %}
{% extends 'master-2021.twig' %}
{% block container %}
<div class="http-error">

View file

@ -1,4 +1,4 @@
{% extends 'master.twig' %}
{% extends 'whois/master.twig' %}
{% set header_title = 'flash.moe / whois' %}
{% set header_logo_flash = 'flash.moe ' %}

View file

@ -0,0 +1 @@
{% extends 'master-2021.twig' %}