minor cleanup

This commit is contained in:
speedie 2023-07-02 01:32:43 +02:00
parent 81a3e548f9
commit 649776d0d3

115
blog.php
View file

@ -8,33 +8,33 @@
* See LICENSE.blog file for copyright and license details. * See LICENSE.blog file for copyright and license details.
*/ */
spl_autoload_register(function($class){ // some settings
require str_replace('\\', DIRECTORY_SEPARATOR, ltrim($class, '\\')).'.php'; define('BLOG_EXT', "md");
});
use md\MarkdownExtra;
define('BLOG_PATH', dirname(__FILE__). '/articles');
define('BLOG_EXT', 'md');
define('BLOG_TITLE', "speedie's blog"); define('BLOG_TITLE', "speedie's blog");
define('BLOG_DESC', "speedie's blog, about stuff I want to talk about."); define('BLOG_DESC', "speedie's blog, about stuff I want to talk about.");
define('LATEST_TEXT', "Latest blog post: ");
define('BLOG_URL', "https://speedie.site/blog"); define('BLOG_URL', "https://speedie.site/blog");
define('BLOG_LATEST_TEXT', "Latest blog post: ");
define('BLOG_DIR', "/articles");
define('DISPLAY_NUM', false); define('DISPLAY_NUM', false);
define('DISPLAY_ID', false); define('DISPLAY_ID', false);
define('BASE_URI', str_replace('/index.php', '', $_SERVER['SCRIPT_NAME']));
define('SELF', $_SERVER['SCRIPT_NAME']);
define('VIEW', '');
define('ENABLE_TITLE', true); define('ENABLE_TITLE', true);
define('ENABLE_HEAD', true); define('ENABLE_HEAD', true);
define('ENABLE_FOOTER', true); define('ENABLE_FOOTER', true);
define('META_DESC', true); define('META_DESC', true);
define('META_ENC', true); define('META_ENC', true);
function __( $label, $alt_word = null ) { spl_autoload_register(function($class) {
require str_replace('\\', DIRECTORY_SEPARATOR, ltrim($class, '\\')).'.php';
});
define('SELF', $_SERVER['SCRIPT_NAME']);
define('VIEW', '');
define('BASE_URI', str_replace('/index.php', '', $_SERVER['SCRIPT_NAME']));
define('BLOG_PATH', dirname(__FILE__). BLOG_DIR);
use md\MarkdownExtra;
function isntNull( $label, $alt_word = null ) {
return is_null($alt_word) ? $label : $alt_word; return is_null($alt_word) ? $label : $alt_word;
} }
@ -95,8 +95,8 @@ function printFooter() {
} }
} }
function getDateForPage($pageName, $pageDate) { function getDateForPost($postName, $postDate) {
$file = BLOG_PATH . "/$pageName." . BLOG_EXT . ".date"; $file = BLOG_PATH . "/$postName." . BLOG_EXT . ".date";
if (file_exists($file)) { if (file_exists($file)) {
return file_get_contents($file); return file_get_contents($file);
@ -105,7 +105,7 @@ function getDateForPage($pageName, $pageDate) {
} }
} }
function getAllPageNames($path = "") { function getAllPostNames($path = "") {
$filenames = array(); $filenames = array();
$dir = opendir(BLOG_PATH . "/$path" ); $dir = opendir(BLOG_PATH . "/$path" );
@ -115,7 +115,7 @@ function getAllPageNames($path = "") {
} }
if ( is_dir( BLOG_PATH . "/$path/$filename" ) ) { if ( is_dir( BLOG_PATH . "/$path/$filename" ) ) {
array_push($filenames, ...getAllPageNames( "$path/$filename" ) ); array_push($filenames, ...getAllPostNames( "$path/$filename" ) );
continue; continue;
} }
@ -130,20 +130,20 @@ function getAllPageNames($path = "") {
return $filenames; return $filenames;
} }
function fileNameForPage($page) { function fileNameForPost($post) {
return BLOG_PATH . "/$page." . BLOG_EXT; return BLOG_PATH . "/$post." . BLOG_EXT;
} }
function dateForPage($page) { function dateForPost($post) {
$file = BLOG_PATH . "/$page." . BLOG_EXT . ".date"; $file = BLOG_PATH . "/$post." . BLOG_EXT . ".date";
if (file_exists($file)) { if (file_exists($file)) {
return file_get_contents($file); return file_get_contents($file);
} }
} }
function numForPage($page) { function numForPost($post) {
$file = BLOG_PATH . "/$page." . BLOG_EXT . ".num"; $file = BLOG_PATH . "/$post." . BLOG_EXT . ".num";
return file_get_contents($file); return file_get_contents($file);
} }
@ -152,12 +152,12 @@ function sanitizeFilename($inFileName) {
return str_replace(array('~', '..', '\\', ':', '|', '&', '.', '+', '!'), '-', $inFileName); return str_replace(array('~', '..', '\\', ':', '|', '&', '.', '+', '!'), '-', $inFileName);
} }
function pageURL($page) { function postURL($post) {
return SELF . VIEW . "/".str_replace("%2F", "/", str_replace("%23", "#", urlencode(sanitizeFilename($page)))); return SELF . VIEW . "/".str_replace("%2F", "/", str_replace("%23", "#", urlencode(sanitizeFilename($post))));
} }
function pageLink($page, $title, $attributes="") { function postLink($post, $title, $attributes="") {
return "<a href=\"" . pageURL($page) ."\"$attributes>$title</a>"; return "<a href=\"" . postURL($post) ."\"$attributes>$title</a>";
} }
function toHTMLID($noid) { function toHTMLID($noid) {
@ -174,13 +174,13 @@ function toHTML($inText) {
for ($i = 0; $i < count($matches[0]); $i++) { for ($i = 0; $i < count($matches[0]); $i++) {
$fullLinkText = $matches[1][$i]; $fullLinkText = $matches[1][$i];
$linkTitleSplit = explode('|', $fullLinkText); $linkTitleSplit = explode('|', $fullLinkText);
$linkedPage = $linkTitleSplit[0]; // split away an eventual link text $linkedPost = $linkTitleSplit[0]; // split away an eventual link text
$linkText = (count($linkTitleSplit) > 1) ? $linkTitleSplit[1] : $linkedPage; $linkText = (count($linkTitleSplit) > 1) ? $linkTitleSplit[1] : $linkedPost;
$pagePart = explode('#', $linkedPage)[0]; // split away an eventual anchor part $postPart = explode('#', $linkedPost)[0]; // split away an eventual anchor part
$linkedFilename = fileNameForPage(sanitizeFilename($pagePart)); $linkedFilename = fileNameForPost(sanitizeFilename($postPart));
$exists = file_exists($linkedFilename); $exists = file_exists($linkedFilename);
$outHTML = str_replace("[[$fullLinkText]]", $outHTML = str_replace("[[$fullLinkText]]",
pageLink($linkedPage, $linkText, ($exists? "" : " class=\"noexist\"")), $outHTML); postLink($linkedPost, $linkText, ($exists? "" : " class=\"noexist\"")), $outHTML);
} }
$outHTML = preg_replace("/\{\{(.*?)\}\}/", "<img src=\"" . BASE_URI . "/images/\\1\" alt=\"\\1\" />", $outHTML); $outHTML = preg_replace("/\{\{(.*?)\}\}/", "<img src=\"" . BASE_URI . "/images/\\1\" alt=\"\\1\" />", $outHTML);
@ -202,22 +202,22 @@ function toHTML($inText) {
} }
$action = isset($_REQUEST['action']) ? $_REQUEST['action'] : 'view'; $action = isset($_REQUEST['action']) ? $_REQUEST['action'] : 'view';
$newPage = ""; $newPost = "";
$text = ""; $text = "";
$html = ""; $html = "";
$rss = ""; $rss = "";
$page = preg_match('@^/@', @$_SERVER["PATH_INFO"]) ? $post = preg_match('@^/@', @$_SERVER["PATH_INFO"]) ?
urldecode(substr($_SERVER["PATH_INFO"], 1)) : urldecode(@$_REQUEST['page']); urldecode(substr($_SERVER["PATH_INFO"], 1)) : urldecode(@$_REQUEST['post']);
$page = sanitizeFilename($page); $post = sanitizeFilename($post);
if ($page != '') { if ($post != '') {
$filename = fileNameForPage($page); $filename = fileNameForPost($post);
if ( file_exists($filename) ) { if ( file_exists($filename) ) {
$text = file_get_contents($filename); $text = file_get_contents($filename);
} else { } else {
$newPage = NULL; $newPost = NULL;
include('php/404.php'); include('php/404.php');
die(); die();
} }
@ -226,12 +226,12 @@ if ($page != '') {
} }
if ( $action === 'home') { if ( $action === 'home') {
$pageNames = getAllPageNames(); $postNames = getAllPostNames();
$filelist = array(); $filelist = array();
$sortBy = isset($_REQUEST['sortBy']) ? $_REQUEST['sortBy'] : 'name'; $sortBy = isset($_REQUEST['sortBy']) ? $_REQUEST['sortBy'] : 'name';
foreach($pageNames as $page) { foreach($postNames as $post) {
$filelist[$page] = numForPage($page); $filelist[$post] = numForPost($post);
} }
arsort($filelist, SORT_NUMERIC); arsort($filelist, SORT_NUMERIC);
@ -250,23 +250,24 @@ if ( $action === 'home') {
$html .= " <ul>\n"; $html .= " <ul>\n";
$f = "0"; $f = "0";
foreach ($filelist as $pageName => $pageDate) { $num = "";
foreach ($filelist as $postName => $postDate) {
if (DISPLAY_NUM) { if (DISPLAY_NUM) {
$i += 1; $i += 1;
$num = "$i "; $num = "$i ";
} }
if (DISPLAY_ID) { if (DISPLAY_ID) {
$i = numForPage($pageName); $i = numForPost($postName);
$num = "$i"; $num = "$i";
} }
$pubDate = date('r', strtotime(getDateForPage($pageName, $pageDate))); $pubDate = date('r', strtotime(getDateForPost($postName, $postDate)));
$link = pageURL($pageName, $pageName); $link = postURL($postName, $postName);
$description = toHTML(file_get_contents(fileNameForPage($pageName))); $description = toHTML(file_get_contents(fileNameForPost($postName)));
$rss .= "<item>\n"; $rss .= "<item>\n";
$rss .= " <title>$pageName</title>\n"; $rss .= " <title>$postName</title>\n";
$rss .= " <link>$link</link>\n"; $rss .= " <link>$link</link>\n";
$rss .= " <guid>$link</guid>\n"; $rss .= " <guid>$link</guid>\n";
$rss .= " <pubDate>$pubDate</pubDate>\n"; $rss .= " <pubDate>$pubDate</pubDate>\n";
@ -280,10 +281,10 @@ if ( $action === 'home') {
$html .= "<li>"; $html .= "<li>";
if (!$f) { if (!$f) {
$html .= " <p class=\"latest\"><strong>$num</strong>".LATEST_TEXT.pageLink($pageName, $pageName).", written ".getDateForPage($pageName, $pageDate)."</p>"; $html .= " <p class=\"latest\"><strong>$num</strong>".BLOG_LATEST_TEXT.postLink($postName, $postName).", written ".getDateForPost($postName, $postDate)."</p>";
$f = 1; $f = 1;
} else { } else {
$html .= " <p><strong>$num</strong> ".pageLink($pageName, $pageName).", written ".getDateForPage($pageName, $pageDate)."</p>"; $html .= " <p><strong>$num</strong> ".postLink($postName, $postName).", written ".getDateForPost($postName, $postDate)."</p>";
} }
$html .= "</li>\n"; $html .= "</li>\n";
@ -291,7 +292,7 @@ if ( $action === 'home') {
$html .= "</ul>\n"; $html .= "</ul>\n";
// Write end of the page // Write end of the post
$html .= "<h3>Archived blog posts</h3>\n"; $html .= "<h3>Archived blog posts</h3>\n";
$html .= " <p>Some blog posts have been archived because they're written out of frustration, are irrelevant or just aren't worth reading. You can still access the blog posts through these RSS feeds:</p>\n"; $html .= " <p>Some blog posts have been archived because they're written out of frustration, are irrelevant or just aren't worth reading. You can still access the blog posts through these RSS feeds:</p>\n";
$html .= "<ul>\n"; $html .= "<ul>\n";
@ -306,15 +307,15 @@ if ( $action === 'home') {
if (file_get_contents('rss.xml') != $rss) { if (file_get_contents('rss.xml') != $rss) {
file_put_contents('rss.xml', $rss); file_put_contents('rss.xml', $rss);
} }
} else { // convert the page and view it } else { // convert the post and view it
$html .= empty($text) ? '' : toHTML($text); $html .= empty($text) ? '' : toHTML($text);
} }
// All blog posts // All blog posts
if (($action === 'home')) { if (($action === 'home')) {
$title = __("Blog posts"); $title = isntNull("Blog posts");
} else if ($filename != '') { } else if ($filename != '') {
$title = $page; $title = $post;
} }
// print text // print text