commit fb2c7506ecb0ec580f6dec344549474a9984a68d
parent 94fdb58598991f5bcdf3d02befbbcb1d6994c52e
Author: Oscar Benedito <oscar@oscarbenedito.com>
Date:   Mon, 17 Aug 2020 19:51:30 +0200
Merge post-hugo-script into deploy-website
Diffstat:
3 files changed, 24 insertions(+), 48 deletions(-)
diff --git a/.gitignore b/.gitignore
@@ -1,4 +1,3 @@
 aun_config.json
 gotify_token.txt
-post_hugo_script.json
 urls.txt
diff --git a/deploy-website.sh b/deploy-website.sh
@@ -21,15 +21,38 @@ WEB_PATH="/srv/oscarbenedito.com"
 GOTIFY_DOMAIN="gotify.oscarbenedito.com"
 FILE_DIR="$(dirname "$(realpath "$0")")"
 
+# Pull rewritting history if needed, check that commit is PGP signed by a known
+# key and if so, rebuild the website
 git -C $HUGO_PATH fetch origin master
 git -C $HUGO_PATH reset --hard origin/master
 git -C $HUGO_PATH verify-commit master || exit 1
 rm -rf $HUGO_PATH/public
 rm -rf $HUGO_PATH/resources
 hugo -s $HUGO_PATH --gc
-$FILE_DIR/post-hugo-script.py "$FILE_DIR/post_hugo_script.json"
+
+# Edit Hugo output: delete unwanted files (also from sitemap)
+rm -rf "$HUGO_PATH/public/licenses/page/"
+rm -f "$HUGO_PATH/public/index.xml" "$HUGO_PATH/public/licenses/index.html" \
+    "$HUGO_PATH/public/licenses/index.xml"
+path="\\/licenses\\/"
+sed -i "/<url>/{:a;N;/<\/url>/!ba};N;/<loc>https:\/\/oscarbenedito\.com${path}<\/loc>/d" \
+    "$HUGO_PATH/public/sitemap.xml"
+# Explanation of RegEx:
+# /<url>/       # find <url>
+# {             # start command declaration
+#   :a          # create label "a"
+#   N           # read next line into pattern space
+#   /<\/url>/!  # if not match </url>...
+#   ba          # goto "a"
+# }             # end command
+# N             # read next line into pattern space (the empty line on sitemap.xml)
+# /<lo....oc>/  # if pattern matches...
+# d             # delete
+
+# Sync new build to production
 rsync --perms --recursive --checksum --delete "$HUGO_PATH/public/" "$WEB_PATH"
 
+# Notify
 API_TOKEN="$(cat "$FILE_DIR/gotify_token.txt")"
 TITLE="Web update triggered"
 MESSAGE="Git hooks triggered an update of the website."
diff --git a/post-hugo-script.py b/post-hugo-script.py
@@ -1,46 +0,0 @@
-#!/usr/bin/env python3
-# Copyright (C) 2020 Oscar Benedito <oscar@oscarbenedito.com>
-#
-# This program is free software: you can redistribute it and/or modify
-# it under the terms of the GNU Affero General Public License as
-# published by the Free Software Foundation, either version 3 of the
-# License, or (at your option) any later version.
-#
-# This program is distributed in the hope that it will be useful,
-# but WITHOUT ANY WARRANTY; without even the implied warranty of
-# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-# GNU Affero General Public License for more details.
-#
-# You should have received a copy of the GNU Affero General Public License
-# along with this program.  If not, see <https://www.gnu.org/licenses/>.
-
-import os
-import sys
-import re
-import json
-import shutil
-
-HUGO_OUT_DIR = '/root/oscarbenedito.com/public'
-
-if len(sys.argv) != 2:
-    print("Usage:\n", sys.argv[0], "file.json")
-    exit(1)
-
-with open(sys.argv[1], 'r') as f:
-    data = json.load(f)
-
-for dir in data['directories']:
-    shutil.rmtree(os.path.join(HUGO_OUT_DIR, dir))
-
-for file in data['files']:
-    os.remove(os.path.join(HUGO_OUT_DIR, file))
-
-with open(os.path.join(HUGO_OUT_DIR, 'sitemap.xml'), 'r') as f:
-    sitemap = f.read()
-
-for line in data['sitemap']:
-    block = '\n[ \t]*<url>\n[ \t]*<loc>https://oscarbenedito\.com' + line + '</loc>\n(?:[ \t]*<lastmod>.*</lastmod>\n)?[ \t]*</url>\n[ \t]*'
-    sitemap = re.sub(block, '', sitemap)
-
-with open(os.path.join(HUGO_OUT_DIR, 'sitemap.xml'), 'w') as f:
-    f.write(sitemap)