refactor: ♻️ break out page type handling code and opengraph tag generation
This commit is contained in:
parent
1b8bff401b
commit
454161e98a
1 changed files with 83 additions and 49 deletions
132
www/router.php
132
www/router.php
|
@ -4,7 +4,6 @@ $data_time_start = microtime(true);
|
|||
|
||||
require_once 'vendor/autoload.php';
|
||||
|
||||
|
||||
require_once 'post_adapter.php';
|
||||
require_once 'fontawesome.php';
|
||||
|
||||
|
@ -63,8 +62,89 @@ $twig->addRuntimeLoader(new class implements RuntimeLoaderInterface {
|
|||
}
|
||||
});
|
||||
|
||||
function generate_website($SURI) {
|
||||
function render_twig($template, $args = []) {
|
||||
global $twig;
|
||||
global $FONT_AWESOME_ARRAY;
|
||||
|
||||
$args['fa'] = $FONT_AWESOME_ARRAY;
|
||||
|
||||
$post = $args['post'] ?? [];
|
||||
$settings = $post['settings'] ?? [];
|
||||
$meta = $post['post_metadata'] ?? [];
|
||||
|
||||
$args['banner'] ??= $settings['banners'] ?? [
|
||||
["src"=> "/static/banner/0.png"],
|
||||
["src" => "/static/banner/1.png"]
|
||||
];
|
||||
|
||||
$args['og'] = array_merge([
|
||||
"title" => $meta['title'] ?? "Dergennibble",
|
||||
"url" => $_SERVER['REQUEST_URI'],
|
||||
"description" => $meta['description']
|
||||
?? $settings['description']
|
||||
?? "The softest spot to find dragons on"
|
||||
], $args['og'] ?? []);
|
||||
|
||||
if(($meta['type'] ?? '') == 'image') {
|
||||
$args['og']['image'] ??= "https://lucidragons.de" . $post['post_file_dir'];
|
||||
}
|
||||
|
||||
$args['og']['image'] ??= 'https://lucidragons.de' . $args['banner'][0]["src"];
|
||||
|
||||
$args['banner'] = json_encode($args['banner']);
|
||||
|
||||
echo $twig->render($template, $args);
|
||||
}
|
||||
|
||||
function try_render_post($SURI) {
|
||||
global $adapter;
|
||||
|
||||
$post = $adapter->get_post_by_path($SURI);
|
||||
|
||||
if(!$post['found']) {
|
||||
echo render_twig('post_types/rrror.html',[
|
||||
"error_code" => '404 Hoard not found!',
|
||||
"error_description" => "Well, we searched
|
||||
far and wide for `" . $SURI . "` but
|
||||
somehow it must have gotten lost... Sorry!",
|
||||
"post" => array_merge($post, [
|
||||
"post_metadata" => ["title" => "404 ???"]
|
||||
])
|
||||
]);
|
||||
|
||||
log_and_die('/404', referrer: ($_SERVER['HTTP_REFERER'] ?? 'magic'));
|
||||
}
|
||||
|
||||
switch($post['post_metadata']['type']) {
|
||||
case 'directory':
|
||||
if(preg_match('/^(.*[^\/])((?:#.*)?)$/', $SURI, $match)) {
|
||||
header('Location: ' . $match[1] . '/' . $match[2]);
|
||||
|
||||
die();
|
||||
}
|
||||
|
||||
echo render_twig('post_types/directory.html', [
|
||||
"post" => $post,
|
||||
"subposts" => $adapter->get_subposts_by_path($SURI)
|
||||
]);
|
||||
|
||||
break;
|
||||
|
||||
case 'text/markdown':
|
||||
echo render_twig('post_types/markdown.html', [
|
||||
"post" => $post
|
||||
]);
|
||||
break;
|
||||
|
||||
case 'image':
|
||||
echo render_twig('post_types/image.html', [
|
||||
"post" => $post,
|
||||
]);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
function generate_website($SURI) {
|
||||
global $adapter;
|
||||
global $FONT_AWESOME_ARRAY;
|
||||
|
||||
|
@ -126,53 +206,7 @@ function generate_website($SURI) {
|
|||
header('Location: /raw' . $SURI);
|
||||
exit(0);
|
||||
} elseif(true) {
|
||||
$post = $adapter->get_post_by_path($SURI);
|
||||
|
||||
if(!$post['found']) {
|
||||
echo $twig->render('post_types/rrror.html',[
|
||||
"error_code" => '404 Hoard not found!',
|
||||
"error_description" => "Well, we searched
|
||||
far and wide for `" . $SURI . "` but
|
||||
somehow it must have gotten lost... Sorry!",
|
||||
"post" => $post
|
||||
]);
|
||||
|
||||
log_and_die('/404', referrer: ($_SERVER['HTTP_REFERER'] ?? 'magic'));
|
||||
}
|
||||
|
||||
if($post['post_metadata']['type'] == 'directory') {
|
||||
if(preg_match('/^(.*[^\/])((?:#.*)?)$/', $SURI, $match)) {
|
||||
header('Location: ' . $match[1] . '/' . $match[2]);
|
||||
|
||||
die();
|
||||
}
|
||||
|
||||
echo $twig->render('post_types/directory.html', [
|
||||
"post" => $post,
|
||||
"subposts" => $adapter->get_subposts_by_path($SURI),
|
||||
"fa" => $FONT_AWESOME_ARRAY
|
||||
]);
|
||||
}
|
||||
elseif($post['post_metadata']['type'] == 'text/markdown') {
|
||||
echo $twig->render('post_types/markdown.html', [
|
||||
"post" => $post,
|
||||
"fa" => $FONT_AWESOME_ARRAY
|
||||
]);
|
||||
}
|
||||
elseif($post['post_metadata']['type'] == 'image') {
|
||||
echo $twig->render('post_types/image.html', [
|
||||
"post" => $post,
|
||||
"fa" => $FONT_AWESOME_ARRAY
|
||||
]);
|
||||
}
|
||||
|
||||
} else {
|
||||
echo $twig->render('rrror.html',[
|
||||
"error_code" => '404 Hoard not found!',
|
||||
"error_description" => "Well, we searched
|
||||
far and wide for `" . $SURI . "` but
|
||||
somehow it must have gotten lost... Sorry!"
|
||||
]);
|
||||
try_render_post($SURI);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue