blob: a0d661fd6ccc0d1f69330c77588fc0f7774794cd (
about) (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
|
#!/usr/bin/env bash
set -Eeuo pipefail
end="\033[0m"
red="\033[0;31m"
red() { echo -e "${red}${1}${end}"; }
## Constant definitions
JSON="${1:-}"
[[ -z "${JSON}" ]] && {
red 'Missing input JSON file.'
cat <<EOF
Usage:
$0 <SITE_JSON_PATH>
Arguments
SITE_JSON_PATH Path to the site.json file which contains data and metadata about pages of the site.
Examples:
$0 _site/site.json
$0 result/site.json
$0 \$(nix-build -A subtasks.docs)/site.json
EOF
exit 2
}
LANGS=(en pt fr)
IGNORED_PAGES=(site.json sitemap.xml *.atom)
## Helper function definitions
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
}
get-lang() {
echo "${1}" | base64 --decode | jq -r .lang
}
get-ref() {
echo "${1}" | base64 --decode | jq -r .ref
}
get-url() {
# Remove leading / to match more closely the filesystem hierarchy
echo "${1}" | base64 --decode | jq -r .url | sed 's_^/__'
}
get-date() {
echo "${1}" | base64 --decode | jq -r .date
}
get-title() {
echo "${1}" | base64 --decode | jq -r .title
}
get-layout() {
echo "${1}" | base64 --decode | jq -r .layout
}
## Assertions
assert-frontmatter() {
F="$1"
DESIRED_LAYOUT="$2"
PREFIX="${3:-}"
LANG="$(get-lang "$F")"
REF="$(get-ref "$F")"
URL="$(get-url "$F")"
LAYOUT="$(get-layout "$F")"
[[ -z "${LANG}" ]] && fail-attr 'lang' "${URL}"
[[ -z "${REF}" ]] && fail-attr 'ref' "${URL}"
if ! contains-element "${URL}" "${IGNORED_PAGES[@]}"; then
TITLE="$(get-title "$F")"
[[ -z "${TITLE}" ]] && fail-attr 'title' "${URL}"
fi
if ! contains-element "${LANG}" "${LANGS[@]}"; then
red "Invalid lang '${LANG}' in ${URL}." >&2
exit 1
fi
if [[ "${DESIRED_LAYOUT}" != "${LAYOUT}" ]]; then
red "Layout mismatch: expected '${DESIRED_LAYOUT}', got '${LAYOUT}'."
red "Page: ${URL}."
exit 1
fi
if [[ -n "${PREFIX}" ]]; then
DATE="$(get-date "$F" | awk '{print $1}')"
URL_BASENAME="$(basename "$(get-url "$F")")"
FILE="${PREFIX}/${DATE}-${URL_BASENAME%.html}.md"
[[ -f "${FILE}" ]] || {
red "date/filename mismatch: '${FILE}' does not exist. To fix, run:"
echo " mv '${PREFIX}/${URL_BASENAME%.html}.md' '${FILE}'"
exit 1
}
fi
}
echo Linting articles... >&2
for article in $(jq -r '.articles[] | @base64' "${JSON}"); do
assert-frontmatter "$article" 'post' '_articles'
done
echo Linting pages... >&2
for page in $(jq -r '.pages[] | @base64' "${JSON}"); do
URL="$(get-url "$page")"
if ! contains-element "${URL}" "${IGNORED_PAGES[@]}"; then
assert-frontmatter "${page}" 'page'
fi
done
echo Linting pastebins... >&2
for pastebin in $(jq -r '.pastebins[] | @base64' "${JSON}"); do
assert-frontmatter "$pastebin" 'pastebin' '_pastebins'
done
echo Linting tils... >&2
for til in $(jq -r '.tils[] | @base64' "${JSON}"); do
assert-frontmatter "$til" 'post' '_tils'
done
echo Asserting unique refs... >&2
KNOWN_IDS=()
assert-unique-ref() {
for page in $1; do
URL="$(get-url "$page")"
if ! contains-element "${URL}" "${IGNORED_PAGES[@]}"; then
LANG="$(get-lang "$page")"
REF="$(get-ref "$page")"
ID="${LANG}:${REF}"
if contains-element "${ID}" "${KNOWN_IDS[@]}"; then
printf '%s\n' "${KNOWN_IDS[@]}"
red "Duplicated lang:ref match: '${ID}'." >&2
red "Page: ${URL}." >&2
exit 1
fi
KNOWN_IDS+=("${ID}") # printf '%s\n' "${KNOWN_IDS[@]}"
fi
done
}
assert-unique-ref "$(jq -r '.pages[] | @base64' "${JSON}")"
assert-unique-ref "$(jq -r '.articles[] | @base64' "${JSON}")"
assert-unique-ref "$(jq -r '.tils[] | @base64' "${JSON}")"
echo Done. >&2
|