From 3e3811c4be44833c984233ad46615f105f5767ac Mon Sep 17 00:00:00 2001 From: "David T. Sadler" Date: Sat, 22 May 2021 13:29:49 +0100 Subject: Generate sitemap --- scripts/build_www_site.php | 2 +- scripts/functions.php | 50 +++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 50 insertions(+), 2 deletions(-) (limited to 'scripts') diff --git a/scripts/build_www_site.php b/scripts/build_www_site.php index e610298..f6a5806 100644 --- a/scripts/build_www_site.php +++ b/scripts/build_www_site.php @@ -42,5 +42,5 @@ if (!is_readable($htmlTemplateDiretory)) { $pages = getPages($geminiSrc, $output); -buildWWWSite($pages, $hostname, $htmlTemplateDiretory); +buildWWWSite($pages, $hostname, $htmlTemplateDiretory, $output); diff --git a/scripts/functions.php b/scripts/functions.php index 9b72803..3e8e7a4 100644 --- a/scripts/functions.php +++ b/scripts/functions.php @@ -102,7 +102,7 @@ function parseContent(string $content): array ]; } -function buildWWWSite(array $pages, string $hostname, string $htmlTemplateDiretory): void +function buildWWWSite(array $pages, string $hostname, string $htmlTemplateDiretory, string $output): void { foreach ($pages as $page) { $destDirectory = dirname($page['output']); @@ -123,6 +123,8 @@ function buildWWWSite(array $pages, string $hostname, string $htmlTemplateDireto } generateAtomFeeds($pages, $hostname); + + generateSiteMap($pages, $hostname, $output); } function buildHtmlFile(string $title, string $contents, string $template): string @@ -313,3 +315,49 @@ function buildAtomEntry(string $title, string $href, string $author, string $dat EOF_STR; } +function generateSiteMap(array $pages, string $hostname, $output): void +{ + $posts = array_filter($pages, fn ($post) => $post['isPost']); + + /** + * Sort by latest to previous date. + */ + usort($posts, fn ($a, $b) => $b['date'] <=> $a['date']); + + file_put_contents( + $output.DIRECTORY_SEPARATOR.'sitemap.xml', + buildSiteMap( + implode('', array_map(fn ($post) => postToSiteMapUrl($post, $hostname), $posts)) + ) + ); + +} + +function postToSiteMapUrl(array $post, string $hostname): string +{ + return buildSiteMapUrl( + "https://$hostname{$post['url']}", + "{$post['date']}T12:00:00Z" + ); +} + +function buildSiteMap(string $urls): string +{ + return << + + $urls + +EOF_STR; +} + +function buildSiteMapUrl(string $loc, string $lastmod): string +{ + return << + $loc + $lastmod + never + +EOF_STR; +} -- cgit v1.2.3-13-gbd6f