aboutsummaryrefslogtreecommitdiff
path: root/scripts
diff options
context:
space:
mode:
authorEuAndreh <eu@euandre.org>2020-02-05 22:05:04 -0300
committerEuAndreh <eu@euandre.org>2020-02-05 22:05:04 -0300
commita6ae95d8bd0e9a933160fed6381b215d19d817ee (patch)
treeaad1c7e0d360a6d99d2cc4a9209a075c252e60af /scripts
parentCopy feed.atom from jekyll-feed (diff)
downloadeuandre.org-a6ae95d8bd0e9a933160fed6381b215d19d817ee.tar.gz
euandre.org-a6ae95d8bd0e9a933160fed6381b215d19d817ee.tar.xz
DIY Atom feed
Diffstat (limited to 'scripts')
-rwxr-xr-xscripts/assert-content.sh85
1 files changed, 85 insertions, 0 deletions
diff --git a/scripts/assert-content.sh b/scripts/assert-content.sh
new file mode 100755
index 0000000..a55e9b0
--- /dev/null
+++ b/scripts/assert-content.sh
@@ -0,0 +1,85 @@
+#!/usr/bin/env bash
+set -Eeuo pipefail
+
+JSON="${1:-}"
+[[ -z "${JSON}" ]] && {
+ echo 'Missing input JSON file.'
+ exit 2
+}
+
+contains-element() {
+ local e match="$1"
+ shift
+ for e; do [[ "$e" == "$match" ]] && return 0; done
+ return 1
+}
+
+fail-attr() {
+ ATTRIBUTE="${1}"
+ URL="${2}"
+ red "Undefined '${ATTRIBUTE}' for ${URL}." >&2
+ exit 1
+}
+
+LANGS=(en pt)
+assert-frontmatter() {
+ F="${1}"
+ LANG="$(echo "$F" | base64 --decode | jq -r .lang)"
+ REF="$(echo "$F" | base64 --decode | jq -r .ref)"
+ URL="$(echo "$F" | base64 --decode | jq -r .url)"
+ [[ -z "${LANG}" ]] && fail-attr 'lang' "${URL}"
+ [[ -z "${REF}" ]] && fail-attr 'ref' "${URL}"
+ set +e
+ contains-element "${LANG}" "${LANGS[@]}"
+ [[ $? != 0 ]] && {
+ red "Invalid lang '${LANG}' in ${URL}." >&2
+ exit 1
+ }
+ set -e
+ :
+}
+
+echo Linting posts... >&2
+for post in $(jq -r '.posts[] | @base64' "${JSON}"); do
+ assert-frontmatter "${post}"
+done
+
+echo Linting pages... >&2
+IGNORED_PAGES=(/site.json /sitemap.xml)
+for page in $(jq -r '.pages[] | @base64' "${JSON}"); do
+ URL="$(echo "$page" | base64 --decode | jq -r .url)"
+ set +e
+ contains-element "${URL}" "${IGNORED_PAGES[@]}"
+ if [[ $? != 0 ]]; then
+ set -e
+ assert-frontmatter "${page}"
+ fi
+ set -e
+done
+
+echo Asserting unique refs... >&2
+KNOWN_IDS=()
+for page in $(jq -r '.pages[] | @base64' "${JSON}"); do
+ URL="$(echo "$page" | base64 --decode | jq -r .url)"
+ set +e
+ contains-element "${URL}" "${IGNORED_PAGES[@]}"
+ if [[ $? != 0 ]]; then
+ set -e
+ LANG="$(echo "$page" | base64 --decode | jq -r .lang)"
+ REF="$(echo "$page" | base64 --decode | jq -r .ref)"
+ ID="${LANG}:${REF}"
+
+ set +e
+ contains-element "${ID}" "${KNOWN_IDS[@]}"
+ [[ $? == 0 ]] && {
+ red "Duplicated lang:ref match: '${ID}'." >&2
+ exit 1
+ }
+ set -e
+
+ KNOWN_IDS+=("${ID}") # printf '%s\n' "${KNOWN_IDS[@]}"
+ fi
+ set -e
+done
+
+echo Done. >&2