minor cleanup

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

121
blog.php
View file

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