diff options
| author | David T. Sadler <davidtsadler@googlemail.com> | 2021-05-22 13:29:49 +0100 |
|---|---|---|
| committer | David T. Sadler <davidtsadler@googlemail.com> | 2021-05-22 13:29:49 +0100 |
| commit | 3e3811c4be44833c984233ad46615f105f5767ac (patch) | |
| tree | 9158650d9393ac2cb457cf50431d57f5e58774db /scripts | |
| parent | 4bbe114c1a859acd12f1dfdcdb04c53ba2d1dc0b (diff) | |
Generate sitemap
Diffstat (limited to 'scripts')
| -rw-r--r-- | scripts/build_www_site.php | 2 | ||||
| -rw-r--r-- | scripts/functions.php | 50 |
2 files changed, 50 insertions, 2 deletions
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 <<<EOF_STR +<?xml version="1.0" encoding="utf-8"?> +<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd"> + $urls +</urlset> +EOF_STR; +} + +function buildSiteMapUrl(string $loc, string $lastmod): string +{ + return <<<EOF_STR +<url> + <loc>$loc</loc> + <lastmod>$lastmod</lastmod> + <changefreq>never</changefreq> +</url> +EOF_STR; +} |
