2021-03-22 01:08:50 +00:00
|
|
|
#!/bin/sh
|
|
|
|
set -eu
|
|
|
|
|
|
|
|
SIZE_LIMIT=150000
|
2021-03-23 01:19:06 +00:00
|
|
|
FAIL=0
|
2021-03-22 01:08:50 +00:00
|
|
|
|
|
|
|
check_size() {
|
|
|
|
size="$(stat --printf="%s" "$1")"
|
|
|
|
if [ "$size" -gt "$SIZE_LIMIT" ]; then
|
|
|
|
echo "File $1 is bigger than specified $SIZE_LIMIT limit"
|
2021-03-23 01:19:06 +00:00
|
|
|
FAIL=1
|
2021-03-22 01:08:50 +00:00
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2021-03-23 01:19:06 +00:00
|
|
|
check_file_name() {
|
|
|
|
fileName="$1"
|
|
|
|
expectedFolder="$2"
|
|
|
|
|
|
|
|
shouldname="${expectedFolder}/$(basename "$fileName" |
|
2021-03-22 01:08:50 +00:00
|
|
|
iconv --to-code=utf-8 |
|
|
|
|
tr '[:upper:]' '[:lower:]' |
|
|
|
|
tr '_ ' '-')"
|
2021-03-23 01:19:06 +00:00
|
|
|
|
|
|
|
if [ "$shouldname" != "$fileName" ]; then
|
2021-03-22 01:08:50 +00:00
|
|
|
echo "$1 should be named $shouldname."
|
2021-03-23 01:19:06 +00:00
|
|
|
FAIL=1
|
2021-03-22 01:08:50 +00:00
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2021-03-23 01:19:06 +00:00
|
|
|
check_webp_name() {
|
|
|
|
check_file_name "$1" "data/pix"
|
|
|
|
}
|
|
|
|
|
2021-03-22 01:08:50 +00:00
|
|
|
check_recipe_name() {
|
2021-03-23 01:19:06 +00:00
|
|
|
check_file_name "$1" "src"
|
2021-03-22 01:08:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
check_recipe_content() {
|
2021-03-23 12:39:30 +00:00
|
|
|
errMsgs="$(awk '
|
2021-03-22 01:08:50 +00:00
|
|
|
BEGIN {
|
2021-03-23 12:39:30 +00:00
|
|
|
HAS_TITLE = 0;
|
|
|
|
HAS_TAGS = 0;
|
|
|
|
HAS_INVALID_TAGS = 0;
|
|
|
|
NUM_TAGS = 0;
|
|
|
|
HAS_INGREDIENTS = 0;
|
|
|
|
HAS_DIRECTIONS = 0;
|
|
|
|
HAS_CONSECUTIVE_EMPTY_LINES = 0;
|
|
|
|
|
|
|
|
CONSECUTIVE_EMPTY_LINES = 0;
|
2021-03-22 01:08:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
# First line should be the title
|
|
|
|
NR == 1 && /^# / {
|
|
|
|
HAS_TITLE = 1;
|
|
|
|
next;
|
|
|
|
}
|
|
|
|
|
2021-03-23 12:39:30 +00:00
|
|
|
$0 == "## Ingredients" {
|
2021-03-22 01:08:50 +00:00
|
|
|
HAS_INGREDIENTS = 1;
|
|
|
|
}
|
|
|
|
|
2021-03-23 12:39:30 +00:00
|
|
|
$0 == "## Directions" {
|
2021-03-22 01:08:50 +00:00
|
|
|
HAS_DIRECTIONS = 1;
|
2021-03-23 12:39:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$0 == "" {
|
|
|
|
CONSECUTIVE_EMPTY_LINES++
|
|
|
|
if (CONSECUTIVE_EMPTY_LINES >= 2) {
|
|
|
|
HAS_CONSECUTIVE_EMPTY_LINES = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$0 != "" {
|
|
|
|
CONSECUTIVE_EMPTY_LINES = 0;
|
2021-03-22 01:08:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
END {
|
|
|
|
# Last line should be the tags list
|
|
|
|
if ($1 == ";tags:") {
|
|
|
|
HAS_TAGS = 1;
|
|
|
|
NUM_TAGS = NF - 1;
|
|
|
|
|
2021-03-23 12:39:30 +00:00
|
|
|
# Loop through all the tags
|
|
|
|
for (i = 2; i <= NF; i++) {
|
|
|
|
# Make sure that each tag only contains lowercase letters and hyphens
|
|
|
|
if ($i !~ "^[a-z-]+$") {
|
|
|
|
HAS_INVALID_TAGS = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-03-22 01:08:50 +00:00
|
|
|
|
|
|
|
if (!HAS_TITLE) {
|
|
|
|
print "Recipe does not have a properly formatted title on the first line."
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!HAS_TAGS) {
|
|
|
|
print "Recipe does not have a properly formatted tags on the last line."
|
2021-03-23 12:39:30 +00:00
|
|
|
} else {
|
|
|
|
if (HAS_INVALID_TAGS) {
|
|
|
|
print "Recipe has invalid tags. Tags must be separated by spaces and contain only lowercase letters or hyphens (-)";
|
|
|
|
}
|
|
|
|
|
|
|
|
if (NUM_TAGS < 2) {
|
|
|
|
print "Recipe only has " NUM_TAGS " tags. Add some more."
|
|
|
|
} else if (NUM_TAGS > 5) {
|
|
|
|
print "Recipe has " NUM_TAGS " tags which is too many. Remove some tags."
|
|
|
|
}
|
2021-03-22 01:08:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!HAS_INGREDIENTS) {
|
|
|
|
print "Recipe does not have an ingredients list."
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!HAS_DIRECTIONS) {
|
|
|
|
print "Recipe does not have a directions section."
|
|
|
|
}
|
|
|
|
|
2021-03-23 12:39:30 +00:00
|
|
|
if (HAS_CONSECUTIVE_EMPTY_LINES) {
|
|
|
|
print "Recipe has at least 2 consecutive empty lines.";
|
2021-03-22 01:08:50 +00:00
|
|
|
}
|
|
|
|
}
|
2021-03-23 12:39:30 +00:00
|
|
|
' "$1")"
|
2021-03-22 01:08:50 +00:00
|
|
|
|
2021-03-23 12:39:30 +00:00
|
|
|
if [ -n "$errMsgs" ]; then
|
|
|
|
echo "$errMsgs"
|
2021-03-23 01:19:06 +00:00
|
|
|
FAIL=1
|
2021-03-22 01:08:50 +00:00
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2021-03-23 12:39:30 +00:00
|
|
|
while IFS= read -r file; do
|
|
|
|
echo "Checking '$file'"
|
2021-03-22 01:08:50 +00:00
|
|
|
case "$file" in
|
2021-03-23 12:39:30 +00:00
|
|
|
# Ignore these files
|
|
|
|
index.md) ;;
|
|
|
|
.github/*.md) ;;
|
|
|
|
|
2021-03-22 01:08:50 +00:00
|
|
|
*.webp)
|
|
|
|
check_size "$file"
|
|
|
|
check_webp_name "$file"
|
|
|
|
;;
|
|
|
|
*.md)
|
|
|
|
check_recipe_name "$file"
|
|
|
|
check_recipe_content "$file"
|
|
|
|
;;
|
2021-03-23 12:39:30 +00:00
|
|
|
esac
|
|
|
|
# Separate each file for easier reading.
|
|
|
|
echo ""
|
|
|
|
done <<EOF
|
|
|
|
$(git diff --name-only "$(git merge-base origin/master HEAD)")
|
|
|
|
EOF
|
2021-03-23 01:19:06 +00:00
|
|
|
|
|
|
|
exit $FAIL
|