Added support for page metadata

This commit is contained in:
Belle Aerni 2023-01-06 00:49:49 -08:00
parent 02919b797f
commit 8ac33d3682
4 changed files with 94 additions and 27 deletions

View file

@ -1,42 +1,65 @@
<?php
namespace AntCMS;
use AntCMS\AntMarkdown;
class AntCMS {
public function renderPage($page, $params = null){
class AntCMS
{
public function renderPage($page, $params = null)
{
$start_time = microtime(true);
$content = $this->getPage($page);
if(!$content){
if (!$content || !is_array($content)) {
$this->renderException("404");
}
$markdown = AntMarkdown::renderMarkdown($content);
$page = $this->getThemeContent();
$markdown = AntMarkdown::renderMarkdown($content['content']);
$pageTemplate = $this->getThemeContent();
$page = str_replace('<!--AntCMS-Body-->', $markdown, $page);
$page = str_replace('<!--AntCMS-Title-->', 'AntCMS', $page);
$pageTemplate = str_replace('<!--AntCMS-Body-->', $markdown, $pageTemplate);
$pageTemplate = str_replace('<!--AntCMS-Title-->', $content['title'], $pageTemplate);
$pageTemplate = str_replace('<!--AntCMS-Description-->', $content['description'], $pageTemplate);
$pageTemplate = str_replace('<!--AntCMS-Author-->', $content['author'], $pageTemplate);
$pageTemplate = str_replace('<!--AntCMS-Keywords-->', $content['keywords'], $pageTemplate);
$end_time = microtime(true);
$elapsed_time = round($end_time - $start_time, 4);
$page = str_replace('<!--AntCMS-Debug-->', '<p>Took ' . $elapsed_time . ' seconds to render the page.', $page);
$pageTemplate = str_replace('<!--AntCMS-Debug-->', '<p>Took ' . $elapsed_time . ' seconds to render the page.', $pageTemplate);
echo $page;
echo $pageTemplate;
}
public function renderException($exceptionCode){
public function renderException($exceptionCode)
{
$content = "# Error";
$content .= "That request caused an $exceptionCode";
echo AntMarkdown::renderMarkdown($content);
}
public function getPage($page){
public function getPage($page)
{
$page = strtolower($page);
$pagePath = appDir . "/Content/$page.md";
if(file_exists($pagePath)){
if (file_exists($pagePath)) {
try {
$pageContents = file_get_contents($pagePath);
return $pageContents;
$pageContent = file_get_contents($pagePath);
// Extract the AntCMS header using the regular expression
preg_match('/--AntCMS--\n(?:Title: (.*)\n)?(?:Author: (.*)\n)?(?:Description: (.*)\n)?(?:Keywords: (.*)\n)?--AntCMS--\n/', $pageContent, $matches);
// Extract the values from the $matches array and provide default values if the elements are missing
$title = $matches[1] ?? 'AntCMS';
$author = $matches[2] ?? 'AntCMS';
$description = $matches[3] ?? 'AntCMS';
$keywords = $matches[4] ?? 'AntCMS';
// Remove the AntCMS section from the content
$pageContent = preg_replace('/--AntCMS--.*--AntCMS--/s', '', $pageContent);
$result = ['content' => $pageContent, 'title' => $title, 'author' => $author, 'description' => $description, 'keywords' => $keywords];
return $result;
} catch (\Exception $e) {
return false;
}
@ -45,11 +68,12 @@ class AntCMS {
}
}
public function getThemeContent(){
public function getThemeContent()
{
$themePath = appDir . "/Theme/default_layout.html";
$themeContent = file_get_contents($themePath);
if(!$themeContent){
if (!$themeContent) {
$themeContent = '
<!DOCTYPE html>
<html>

View file

@ -1,3 +1,10 @@
--AntCMS--
Title: Hello World
Author: Belle Nottelling
Description: Hello world test page for AntCMS
Keywords: AntCMS, CMS, fast, tiny
--AntCMS--
# Hello World
This is the index.md file for my AntCMS project!

View file

@ -1,3 +1,10 @@
--AntCMS--
Title: Markdown Test
Author: Belle Nottelling
Description: A test page for AntCMS
Keywords: AntCMS, CMS, fast, tiny
--AntCMS--
# Heading 1
## Heading 2
### Heading 3

View file

@ -1,13 +1,42 @@
<!DOCTYPE html>
<html>
<head>
<title><!--AntCMS-Title--></title>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.3.1/dist/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
</style>
</head>
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<meta name="description" content="<!--AntCMS-Description-->">
<meta name="author" content="<!--AntCMS-Author-->">
<meta name="keywords" content="<!--AntCMS-Keywords-->">
<body>
<!--AntCMS-Body-->
<!--AntCMS-Debug-->
</body>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.3.1/dist/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
<title><!--AntCMS-Title--></title>
</head>
<body>
<!-- Navigation -->
<nav class="navbar navbar-expand-lg navbar-light bg-light">
<a class="navbar-brand" href="#">My Website</a>
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarNav" aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarNav">
<ul class="navbar-nav">
<li class="nav-item active">
<a class="nav-link" href="#">Home</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">About</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Contact</a>
</li>
</ul>
</div>
</nav>
<!-- Content -->
<div class="container mt-5">
<!--AntCMS-Body-->
<!--AntCMS-Debug-->
</div>
</body>
</html>