Skip to content

Commit

Permalink
code refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
atas committed Oct 12, 2023
1 parent 7580812 commit a0eaeeb
Show file tree
Hide file tree
Showing 11 changed files with 114 additions and 73 deletions.
6 changes: 6 additions & 0 deletions 404.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
<?php
require_once 'system/bootstrap.php';

global $page_meta;

$page_meta->title = "404 Not Found";
$page_meta->desc = "This page has not been found.";

require_once 'layout/header.php';
?>

Expand Down
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@

# You would only need to use these if you want to build your own custom builder image.
# Otherwise, by default the workflow uses the pre-built image from ghcr.io/atas/ssg-builder:latest

build-local-image:
docker build system/workflow-image -t atas-ssg-builder:latest

Expand Down
4 changes: 2 additions & 2 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
"site_top_about_line1": "Simple PHP-based Static Site generator",
"site_top_about_line2": "Supports Markdown and deployed to GitHub Pages by GitHub Actions",
"hostname": "ssg-test.atasasmaz.com",
"local_hostname": "ssg.local",
"local_hostname": "ssg-local.atasasmaz.com",
"email": "[email protected]",
"full_title": "Ata's SSG - a Simple PHP-based SSG for GitHub Pages",
"appended_title": " - Ata's SSG'",
"appended_title": " - Ata's SSG",
"site_desc": "Welcome to Ata's SSG. This is a simple PHP-based Static Site Generator that supports Markdown and deployed to GitHub Pages by GitHub Actions.",
"linkedin_url": "https://www.linkedin.com/in/atasasmaz",
"twitter_url": "https://twitter.com/AtaSasmaz",
Expand Down
29 changes: 16 additions & 13 deletions index.php
Original file line number Diff line number Diff line change
@@ -1,26 +1,29 @@
<?php

require_once "system/bootstrap.php";
global $og;

$og->title = $tpl->meta->title ?? "";
$og->desc = $tpl->meta->desc ?? "";
$selectedTab = "home";
// @var $page_meta PageMeta
global $page_meta;
$page_meta->selectedTab = "home";

require_once 'layout/header.php';

$posts = get_posts();
$posts = get_all_posts();

echo "<div id='posts'>";
foreach ($posts as $post) {
echo "<article class='post'><a href='/p/$post->slug'>";
echo "<div class='left'>";
echo "<h2>$post->title</h2>";
echo "<div class='desc'>$post->desc</div>";
echo "</div>";
echo '<div class="right"><img src="/assets/images/right-arrow.svg" /></div>';
echo "</a></article>";
?>
<article class='post'>
<a href='/p/<?= $post->slug ?>'>
<div class='left'>
<h2><?= $post->title ?></h2>
<div class='desc'><?= $post->desc ?></div>
</div>
<div class="right"><img src="/assets/images/right-arrow.svg" alt="Right arrow"/></div>
</a>
</article>
<?php
}
echo "</div>";

require_once 'layout/footer.php';

14 changes: 7 additions & 7 deletions layout/header.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
<?php
require_once __DIR__ . "/../system/layoutUtils.php";

global $og, $config;
global $page_meta, $config;

$title = !isset($og->title) || strlen($og->title) == 0 ? $config->full_title : "$og->title$config->appended_title";
$desc = !isset($og->desc) || strlen($og->desc) == 0 ? $config->site_desc : $og->desc;
$title = !isset($page_meta->title) || strlen($page_meta->title) == 0 ? $config->full_title : "$page_meta->title$config->appended_title";
$desc = !isset($page_meta->desc) || strlen($page_meta->desc) == 0 ? $config->site_desc : $page_meta->desc;

?>
<!-- Built with Ata's SSG https://www.github.com/atas/ssg -->
Expand All @@ -22,7 +22,7 @@
<meta name="description" content="<?= $desc ?>"/>

<meta property="og:url" content="<?= getCurrentFullUrl() ?>"/>
<meta property="og:type" content="<?= $og->type ?>"/>
<meta property="og:type" content="<?= $page_meta->type ?>"/>
<meta property="og:title" content="<?= $title ?>"/>
<meta property="og:description" content="<?= $desc ?>"/>
<meta property="og:image" content="<?= getCurrentHostnameWithProtocol() ?>/assets/images/site-icon-big.jpg"/>
Expand Down Expand Up @@ -82,7 +82,7 @@

<div id="tabs">
<div class="tabsContent">
<a href="/" class="<?= selectedTab("home") ?>">
<a href="/" class="<?= selectedTabCss("home") ?>">
<svg version="1.2" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" width="24" height="24">
<title>216242_home_icon-svg</title>
<style>
Expand All @@ -99,7 +99,7 @@
</div>
</a>

<a href="/about" class="<?= selectedTab("about") ?>">
<a href="/about" class="<?= selectedTabCss("about") ?>">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><title/>
<path d="M12,2A10,10,0,1,0,22,12,10,10,0,0,0,12,2Zm-.5,3A1.5,1.5,0,1,1,10,6.5,1.5,1.5,0,0,1,11.5,5ZM14,18H13a2,2,0,0,1-2-2V12a1,1,0,0,1,0-2h1a1,1,0,0,1,1,1v5h1a1,1,0,0,1,0,2Z"
fill="#ffffff"/>
Expand All @@ -109,7 +109,7 @@
</div>
</a>

<a href="/contact" class="<?= selectedTab("contact") ?>">
<a href="/contact" class="<?= selectedTabCss("contact") ?>">
<svg xmlns="http://www.w3.org/2000/svg" height="1792" viewBox="0 0 1792 1792" width="1792">
<path d="M1100 775q0-108-53.5-169t-147.5-61q-63 0-124 30.5t-110 84.5-79.5 137-30.5 180q0 112 53.5 173t150.5 61q96 0 176-66.5t122.5-166 42.5-203.5zm564 121q0 111-37 197t-98.5 135-131.5 74.5-145 27.5q-6 0-15.5.5t-16.5.5q-95 0-142-53-28-33-33-83-52 66-131.5 110t-173.5 44q-161 0-249.5-95.5t-88.5-269.5q0-157 66-290t179-210.5 246-77.5q87 0 155 35.5t106 99.5l2-19 11-56q1-6 5.5-12t9.5-6h118q5 0 13 11 5 5 3 16l-120 614q-5 24-5 48 0 39 12.5 52t44.5 13q28-1 57-5.5t73-24 77-50 57-89.5 24-137q0-292-174-466t-466-174q-130 0-248.5 51t-204 136.5-136.5 204-51 248.5 51 248.5 136.5 204 204 136.5 248.5 51q228 0 405-144 11-9 24-8t21 12l41 49q8 12 7 24-2 13-12 22-102 83-227.5 128t-258.5 45q-156 0-298-61t-245-164-164-245-61-298 61-298 164-245 245-164 298-61q344 0 556 212t212 556z"
fill="#ffffff"/>
Expand Down
27 changes: 15 additions & 12 deletions page.php
Original file line number Diff line number Diff line change
@@ -1,27 +1,30 @@
<?php

include "system/bootstrap.php";
global $og;
global $page_meta;

$md_path = "pages/$_GET[page].md";

/**
* Page parameter can only be alphanumeric and hyphen and .md file must exist.
* If not, return 404.
*/
if (!preg_match('/^[a-zA-Z0-9\-]+$/', $_GET['page']) || !file_exists($md_path)) {
header('HTTP/1.0 404 Not Found');
include '404.php';
exit;
}

$tpl = get_md($md_path);
$tpl = get_markdown($md_path);

$og->title = $tpl->meta->title ?? null;
$og->desc = $tpl->meta->desc ?? null;
$selectedTab = $tpl->meta->selectedTab ?? "index";
$page_meta->title = $tpl->meta->title ?? null;
$page_meta->desc = $tpl->meta->desc ?? null;
$page_meta->selectedTab = $tpl->meta->selectedTab ?? "index";
include 'layout/header.php';

echo "<div class='box singlePage'>";
echo $tpl->content;
echo "</div>";
include 'layout/footer.php';

?>

<div class='box singlePage'>
<?= $tpl->content ?>
</div>

<?php
include 'layout/footer.php';
33 changes: 20 additions & 13 deletions post.php
Original file line number Diff line number Diff line change
@@ -1,34 +1,41 @@
<?php

require_once "system/bootstrap.php";
global $og;
global $page_meta;

/**
* Slug querystring can only be alphanumeric and hyphen
* If not, return 404.
*/
if (!preg_match('/^[a-zA-Z0-9\-]+$/', $_GET['slug'])) {
echo "<!--POST PATTERN IS WRONG, 404, SLUG: $_GET[slug]-->";
not_found();
exit_with_not_found();
}

$post = current(array_filter(get_posts(), function ($post) {
// Iterate through all posts and find the one with the same slug
// Not the most performant but once deployed they will all be static websites.
$post = current(array_filter(get_all_posts(), function ($post) {
return $post->slug == $_GET['slug'];
}));

if (!$post) {
echo "<!--POST NOT FOUND, 404, SLUG: $_GET[slug]-->";
not_found();
exit_with_not_found();
}

$tpl = get_md($post->filename);
$tpl = get_markdown($post->filename);

$og->title = $tpl->meta->title ?? null;
$og->type = "article";
$og->desc = $tpl->meta->desc ?? null;
$page_meta->title = $tpl->meta->title ?? null;
$page_meta->type = "article";
$page_meta->desc = $tpl->meta->desc ?? null;

require_once 'layout/header.php';

echo "<div class='box singlePost'>";
echo $tpl->content;
echo "</div>";

?>

<?php require_once 'layout/footer.php'; ?>
<div class='box singlePost'>
<?=$tpl->content?>
</div>

<?php
require_once 'layout/footer.php';
21 changes: 21 additions & 0 deletions system/Types.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

class Post {
public string $title;
public string $desc;
public string $slug;
public string $filename;
}

class PageMeta
{
public ?string $title = null;
public string $type = "website";
public string $desc = "";
public string $selectedTab = "";
}

class ConvertedMarkdown {
public string $content;
public object $meta;
}
33 changes: 15 additions & 18 deletions system/bootstrap.php
Original file line number Diff line number Diff line change
@@ -1,50 +1,47 @@
<?php

use JetBrains\PhpStorm\NoReturn;
use League\CommonMark\Exception\CommonMarkException;

require_once(__DIR__ . "/../vendor/autoload.php");
require_once(__DIR__ . '/Types.php');

$config = json_decode(file_get_contents(__DIR__ . "/../config.json"));

class OpenGraph
{
public ?string $title = null;
public string $type = "website";
public string $desc = "";
}

$og = new OpenGraph();
$og->desc = $config->site_desc;
$page_meta = new PageMeta();
$page_meta->desc = $config->site_desc;

/**
* Get a markdown file by its path, by converting that to html
* @param $path
* @return stdClass
* @return ConvertedMarkdown
* @throws CommonMarkException
*/
function get_md($path): stdClass
function get_markdown($path): ConvertedMarkdown
{
if (!file_exists($path)) {
not_found();
exit_with_not_found();
}

require_once 'system/markdown.php';

return convert_md($path);
return convert_markdown($path);
}

/**
* Generate and return all posts as an array
* @return array
* @return Post[]
* @throws CommonMarkException
*/
function get_posts(): array
function get_all_posts(): array
{
$posts = [];
$files = glob('posts/*.md');
rsort($files);
foreach ($files as $key => $post_file_path) {
$tpl = get_md($post_file_path);
$tpl = get_markdown($post_file_path);

$postObj = new stdClass();
$postObj = new Post();
$postObj->title = $tpl->meta->title;
$postObj->desc = $tpl->meta->desc;
$postObj->slug = $tpl->meta->slug;
Expand All @@ -60,7 +57,7 @@ function get_posts(): array
* Show not found page
* @return void
*/
#[NoReturn] function not_found(): void
#[NoReturn] function exit_with_not_found(): void
{
header('HTTP/1.0 404 Not Found');
include_once '404.php';
Expand Down
8 changes: 4 additions & 4 deletions system/layoutUtils.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@

/**
* Returns the 'selected' css class if the selectedTab variable matches the given tab name. Or returns empty string.
* @param $tab
* @param $tab string
* @return string
*/
function selectedTab($tab): string
function selectedTabCss(string $tab): string
{
global $selectedTab;
if ($selectedTab == $tab) {
global $page_meta;
if ($page_meta->selectedTab == $tab) {
return "selected";
}
return "";
Expand Down
9 changes: 5 additions & 4 deletions system/markdown.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

require_once(__DIR__."/../vendor/league/commonmark/src/GithubFlavoredMarkdownConverter.php");

use League\CommonMark\Exception\CommonMarkException;
use League\CommonMark\Extension\FrontMatter\FrontMatterExtension;
use League\CommonMark\Extension\FrontMatter\Output\RenderedContentWithFrontMatter;
use League\CommonMark\GithubFlavoredMarkdownConverter;
Expand Down Expand Up @@ -48,10 +49,10 @@ function get_md_config(): array
/**
* Converts a given markdown file to HTML with handy flavours and extensions.
* @param $path
* @return stdClass
* @throws \League\CommonMark\Exception\CommonMarkException
* @return ConvertedMarkdown
* @throws CommonMarkException
*/
function convert_md($path): stdClass
function convert_markdown($path): ConvertedMarkdown
{
$converter = new GithubFlavoredMarkdownConverter(get_md_config());

Expand All @@ -64,7 +65,7 @@ function convert_md($path): stdClass

$output = $converter->convert($md);

$tpl = new stdClass();
$tpl = new ConvertedMarkdown();
$tpl->content = $output->getContent();

if ($output instanceof RenderedContentWithFrontMatter) {
Expand Down

0 comments on commit a0eaeeb

Please sign in to comment.