From a0eaeeb4603e7af5cd9618aca25bc3dfc3a5fdce Mon Sep 17 00:00:00 2001 From: AtaS Date: Thu, 12 Oct 2023 23:12:19 +0100 Subject: [PATCH] code refactor --- 404.php | 6 ++++++ Makefile | 3 +++ config.json | 4 ++-- index.php | 29 ++++++++++++++++------------- layout/header.php | 14 +++++++------- page.php | 27 +++++++++++++++------------ post.php | 33 ++++++++++++++++++++------------- system/Types.php | 21 +++++++++++++++++++++ system/bootstrap.php | 33 +++++++++++++++------------------ system/layoutUtils.php | 8 ++++---- system/markdown.php | 9 +++++---- 11 files changed, 114 insertions(+), 73 deletions(-) create mode 100644 system/Types.php diff --git a/404.php b/404.php index c0149d1..b163df2 100644 --- a/404.php +++ b/404.php @@ -1,5 +1,11 @@ title = "404 Not Found"; +$page_meta->desc = "This page has not been found."; + require_once 'layout/header.php'; ?> diff --git a/Makefile b/Makefile index de38a31..480cac2 100644 --- a/Makefile +++ b/Makefile @@ -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 diff --git a/config.json b/config.json index 075be95..622db67 100644 --- a/config.json +++ b/config.json @@ -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": "local-address@your-domain.com", "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", diff --git a/index.php b/index.php index 20ef0d1..57021e3 100644 --- a/index.php +++ b/index.php @@ -1,26 +1,29 @@ 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 "
"; foreach ($posts as $post) { - echo ""; + ?> + + "; require_once 'layout/footer.php'; - diff --git a/layout/header.php b/layout/header.php index bccaa72..4e00306 100644 --- a/layout/header.php +++ b/layout/header.php @@ -1,10 +1,10 @@ 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; ?> @@ -22,7 +22,7 @@ - + @@ -82,7 +82,7 @@ - "> + "> diff --git a/page.php b/page.php index ad66f19..d375924 100644 --- a/page.php +++ b/page.php @@ -1,27 +1,30 @@ 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 "
"; -echo $tpl->content; -echo "
"; -include 'layout/footer.php'; - ?> +
+ content ?> +
+ +"; - 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 ""; - 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 "
"; -echo $tpl->content; -echo "
"; - ?> - +
+content?> +
+ +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; @@ -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'; diff --git a/system/layoutUtils.php b/system/layoutUtils.php index 577b50e..51b1504 100644 --- a/system/layoutUtils.php +++ b/system/layoutUtils.php @@ -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 ""; diff --git a/system/markdown.php b/system/markdown.php index 6b1cb54..f3fd176 100644 --- a/system/markdown.php +++ b/system/markdown.php @@ -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; @@ -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()); @@ -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) {