summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDavid T. Sadler <davidtsadler@googlemail.com>2021-05-22 13:29:49 +0100
committerDavid T. Sadler <davidtsadler@googlemail.com>2021-05-22 13:29:49 +0100
commit3e3811c4be44833c984233ad46615f105f5767ac (patch)
tree9158650d9393ac2cb457cf50431d57f5e58774db
parent4bbe114c1a859acd12f1dfdcdb04c53ba2d1dc0b (diff)
Generate sitemap
-rw-r--r--scripts/build_www_site.php2
-rw-r--r--scripts/functions.php50
-rw-r--r--www/sitemap.xml68
3 files changed, 118 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;
+}
diff --git a/www/sitemap.xml b/www/sitemap.xml
new file mode 100644
index 0000000..858dd4f
--- /dev/null
+++ b/www/sitemap.xml
@@ -0,0 +1,68 @@
+<?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">
+ <url>
+ <loc>https://davidtsadler.com/posts/nextcloud/2021-02-15/accessing-nextcloud-with-webdav-on-arch/index.html</loc>
+ <lastmod>2021-02-15T12:00:00Z</lastmod>
+ <changefreq>never</changefreq>
+</url><url>
+ <loc>https://davidtsadler.com/posts/gemini/2021-02-08/how-to-host-your-own-gemini-site-in-the-cloud/index.html</loc>
+ <lastmod>2021-02-08T12:00:00Z</lastmod>
+ <changefreq>never</changefreq>
+</url><url>
+ <loc>https://davidtsadler.com/posts/php/2021-01-18/installing-php-8-for-windows-10/index.html</loc>
+ <lastmod>2021-01-18T12:00:00Z</lastmod>
+ <changefreq>never</changefreq>
+</url><url>
+ <loc>https://davidtsadler.com/posts/laravel/2020-12-21/installing-laravel-homestead-in-arch-linux/index.html</loc>
+ <lastmod>2020-12-21T12:00:00Z</lastmod>
+ <changefreq>never</changefreq>
+</url><url>
+ <loc>https://davidtsadler.com/posts/laravel/2020-12-14/sqlstate-hy000-2002-php-network-getaddresses-getaddrinfo-failed/index.html</loc>
+ <lastmod>2020-12-14T12:00:00Z</lastmod>
+ <changefreq>never</changefreq>
+</url><url>
+ <loc>https://davidtsadler.com/posts/arch/2020-09-07/installing-zsh-and-powerlevel10k-on-arch-linux/index.html</loc>
+ <lastmod>2020-09-07T12:00:00Z</lastmod>
+ <changefreq>never</changefreq>
+</url><url>
+ <loc>https://davidtsadler.com/posts/arch/2020-08-31/enabling-audio-in-arch-linux/index.html</loc>
+ <lastmod>2020-08-31T12:00:00Z</lastmod>
+ <changefreq>never</changefreq>
+</url><url>
+ <loc>https://davidtsadler.com/posts/arch/2020-08-24/pacman-cheat-sheet-for-ubuntu-users/index.html</loc>
+ <lastmod>2020-08-24T12:00:00Z</lastmod>
+ <changefreq>never</changefreq>
+</url><url>
+ <loc>https://davidtsadler.com/posts/arch/2020-08-17/installing-st-dmenu-dwm-in-arch-linux/index.html</loc>
+ <lastmod>2020-08-17T12:00:00Z</lastmod>
+ <changefreq>never</changefreq>
+</url><url>
+ <loc>https://davidtsadler.com/posts/linux/2020-07-13/sudo-sorry-you-must-have-a-tty-to-run-sudo/index.html</loc>
+ <lastmod>2020-07-13T12:00:00Z</lastmod>
+ <changefreq>never</changefreq>
+</url><url>
+ <loc>https://davidtsadler.com/posts/arch/2020-06-22/granting-sudo-access-to-a-user-in-arch-linux/index.html</loc>
+ <lastmod>2020-06-22T12:00:00Z</lastmod>
+ <changefreq>never</changefreq>
+</url><url>
+ <loc>https://davidtsadler.com/posts/arch/2020-06-15/adding-a-user-in-arch-linux/index.html</loc>
+ <lastmod>2020-06-15T12:00:00Z</lastmod>
+ <changefreq>never</changefreq>
+</url><url>
+ <loc>https://davidtsadler.com/posts/netlify/2020-06-08/publishing-jigsaw-posts-with-netlify-build-hooks/index.html</loc>
+ <lastmod>2020-06-08T12:00:00Z</lastmod>
+ <changefreq>never</changefreq>
+</url><url>
+ <loc>https://davidtsadler.com/posts/jigsaw/2020-06-01/scheduling-posts-in-jigsaw/index.html</loc>
+ <lastmod>2020-06-01T12:00:00Z</lastmod>
+ <changefreq>never</changefreq>
+</url><url>
+ <loc>https://davidtsadler.com/posts/arch/2020-05-25/installing-arch-linux-on-a-thinkpad-x220/index.html</loc>
+ <lastmod>2020-05-25T12:00:00Z</lastmod>
+ <changefreq>never</changefreq>
+</url><url>
+ <loc>https://davidtsadler.com/posts/markdown/2020-03-30/creating-an-ebook-with-markdown/index.html</loc>
+ <lastmod>2020-03-30T12:00:00Z</lastmod>
+ <changefreq>never</changefreq>
+</url>
+</urlset> \ No newline at end of file