Automated page list generation

This commit is contained in:
Belle Aerni 2023-01-06 16:16:11 -08:00
parent bb5da2fc50
commit 7ab664e18f
6 changed files with 97 additions and 37 deletions

View file

@ -5,7 +5,8 @@
"minimum-stability": "stable",
"require": {
"michelf/php-markdown": "^2.0",
"symfony/yaml": "^6.0"
"symfony/yaml": "^6.0",
"php": ">=7.4"
},
"authors": [
{

20
composer.lock generated
View file

@ -4,7 +4,7 @@
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
"This file is @generated automatically"
],
"content-hash": "23fb12ebd55b99f3fce96f880a2f579e",
"content-hash": "9fa90e5bf5828bb52c3654d04fd7383d",
"packages": [
{
"name": "michelf/php-markdown",
@ -146,20 +146,20 @@
},
{
"name": "symfony/yaml",
"version": "v6.0.17",
"version": "v6.2.2",
"source": {
"type": "git",
"url": "https://github.com/symfony/yaml.git",
"reference": "76c08913ea1c50541503a4563b2172710189fa29"
"reference": "6ed8243aa5f2cb5a57009f826b5e7fb3c4200cf3"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/symfony/yaml/zipball/76c08913ea1c50541503a4563b2172710189fa29",
"reference": "76c08913ea1c50541503a4563b2172710189fa29",
"url": "https://api.github.com/repos/symfony/yaml/zipball/6ed8243aa5f2cb5a57009f826b5e7fb3c4200cf3",
"reference": "6ed8243aa5f2cb5a57009f826b5e7fb3c4200cf3",
"shasum": ""
},
"require": {
"php": ">=8.0.2",
"php": ">=8.1",
"symfony/polyfill-ctype": "^1.8"
},
"conflict": {
@ -200,7 +200,7 @@
"description": "Loads and dumps YAML files",
"homepage": "https://symfony.com",
"support": {
"source": "https://github.com/symfony/yaml/tree/v6.0.17"
"source": "https://github.com/symfony/yaml/tree/v6.2.2"
},
"funding": [
{
@ -216,7 +216,7 @@
"type": "tidelift"
}
],
"time": "2022-12-14T15:52:41+00:00"
"time": "2022-12-14T16:11:27+00:00"
}
],
"packages-dev": [],
@ -225,7 +225,9 @@
"stability-flags": [],
"prefer-stable": false,
"prefer-lowest": false,
"platform": [],
"platform": {
"php": ">=7.4"
},
"platform-dev": [],
"plugin-api-version": "2.3.0"
}

View file

@ -47,9 +47,8 @@ class AntCMS
$page = strtolower($page);
$pagePath = AntDir . "/Content/$page";
$pagePath = str_replace('//', '/', $pagePath);
$AntKeywords = new AntKeywords();
if(is_dir($pagePath)){
if (is_dir($pagePath)) {
$pagePath = $pagePath . '/index.md';
} else {
$pagePath = $pagePath . '.md';
@ -58,30 +57,10 @@ class AntCMS
if (file_exists($pagePath)) {
try {
$pageContent = file_get_contents($pagePath);
preg_match('/--AntCMS--.*--AntCMS--/s', $pageContent, $matches);
$pageHeaders = AntCMS::getPageHeaders($pageContent);
// Remove the AntCMS section from the content
$pageContent = preg_replace('/--AntCMS--.*--AntCMS--/s', '', $pageContent);
if ($matches) {
$header = $matches[0];
preg_match('/Title: (.*)/', $header, $matches);
$title = trim($matches[1] ?? 'AntCMS');
preg_match('/Author: (.*)/', $header, $matches);
$author = trim($matches[1] ?? 'AntCMS');
preg_match('/Description: (.*)/', $header, $matches);
$description = trim($matches[1]) ?? 'AntCMS';
preg_match('/Keywords: (.*)/', $header, $matches);
$keywords = trim($matches[1] ?? $AntKeywords->generateKeywords($pageContent));
} else {
[$title, $author, $description, $keywords] = ['AntCMS','AntCMS','AntCMS', trim($AntKeywords->generateKeywords($pageContent))];
}
$result = ['content' => $pageContent, 'title' => $title, 'author' => $author, 'description' => $description, 'keywords' => $keywords];
$result = ['content' => $pageContent, 'title' => $pageHeaders['title'], 'author' => $pageHeaders['author'], 'description' => $pageHeaders['description'], 'keywords' => $pageHeaders['keywords']];
return $result;
} catch (\Exception $e) {
return false;
@ -114,4 +93,38 @@ class AntCMS
return $themeContent;
}
public static function getPageHeaders($pageContent)
{
$AntKeywords = new AntKeywords();
preg_match('/--AntCMS--.*--AntCMS--/s', $pageContent, $matches);
// Remove the AntCMS section from the content
$pageContent = preg_replace('/--AntCMS--.*--AntCMS--/s', '', $pageContent);
$pageHeaders = [];
if ($matches) {
$header = $matches[0];
preg_match('/Title: (.*)/', $header, $matches);
$pageHeaders['title'] = trim($matches[1] ?? 'AntCMS');
preg_match('/Author: (.*)/', $header, $matches);
$pageHeaders['author'] = trim($matches[1] ?? 'AntCMS');
preg_match('/Description: (.*)/', $header, $matches);
$pageHeaders['description'] = trim($matches[1]) ?? 'AntCMS';
preg_match('/Keywords: (.*)/', $header, $matches);
$pageHeaders['keywords'] = trim($matches[1] ?? $AntKeywords->generateKeywords($pageContent));
} else {
$pageHeaders = [
'title' => 'AntCMS',
'author' => 'AntCMS',
'description' => 'AntCMS',
'keywords' => trim($AntKeywords->generateKeywords($pageContent)),
];
}
return $pageHeaders;
}
}

View file

@ -12,9 +12,7 @@ class AntCache
$config = AntConfig::currentConfig();
if ($config['enableCache']) {
try {
$cache = fopen($cachePath, "w");
fwrite($cache, (string)$content);
fclose($cache);
file_put_contents($cachePath, (string)$content);
return true;
} catch (\Exception $e) {
return false;

39
src/AntCMS/AntPages.php Normal file
View file

@ -0,0 +1,39 @@
<?php
namespace AntCMS;
use AntCMS\AntCMS;
use AntCMS\AntYaml;
class AntPages
{
public static function generatePages()
{
$dir = new \RecursiveDirectoryIterator(antContentPath);
$iterator = new \RecursiveIteratorIterator($dir);
$pages = array();
$pageList = array();
foreach ($iterator as $file) {
if (pathinfo($file, PATHINFO_EXTENSION) == "md") {
$pages[] = $file->getPathname();
}
}
foreach ($pages as $page) {
$pageContent = file_get_contents($page);
$pageHeader = AntCMS::getPageHeaders($pageContent);
$pageFunctionalPath = str_replace(antContentPath, "", $page);
$currentPage = array(
'pageTitle' => $pageHeader['title'],
'fullPagePath' => $page,
'functionalPagePath' => $pageFunctionalPath,
);
$pageList[] = $currentPage;
}
AntYaml::saveFile(antPagesList, $pageList);
}
public static function getPages()
{
}
}

View file

@ -6,11 +6,14 @@ ini_set('display_errors', 1);
const AntDir = __DIR__;
const AntCachePath = __DIR__ . '/Cache';
const antConfigFile = __DIR__ . '/config.ymal';
const antPagesList = __DIR__ . '/pages.ymal';
const antContentPath = __DIR__ . '/Content';
require_once __DIR__ . '/Vendor/autoload.php';
require_once __DIR__ . '/Autoload.php';
use AntCMS\AntCMS;
use AntCMS\AntConfig;
use AntCMS\AntPages;
$antCms = new AntCMS();
@ -18,6 +21,10 @@ if(!file_exists(antConfigFile)){
AntConfig::generateConfig();
}
if(!file_exists(antPagesList)){
AntPages::generatePages();
}
$currentConfg = AntConfig::currentConfig();
if ($currentConfg['forceHTTPS'] && 'cli' !== PHP_SAPI){