|
|
@ -2,12 +2,12 @@
|
|
|
|
|
|
|
|
|
|
|
|
#################################################################################
|
|
|
|
#################################################################################
|
|
|
|
# This script is run by git before a commit is made. #
|
|
|
|
# This script is run by git before a commit is made. #
|
|
|
|
# Does some housekeeping tasks before a commit is made. #
|
|
|
|
|
|
|
|
# #
|
|
|
|
# #
|
|
|
|
# FEATURES #
|
|
|
|
# FEATURES #
|
|
|
|
# Updates the "updated" field in the front matter of .md files. #
|
|
|
|
# Updates the "updated" field in the front matter of .md files. #
|
|
|
|
# Compresses PNG files with either oxipng or optipng if available. #
|
|
|
|
# Compresses PNG files with either oxipng or optipng if available. #
|
|
|
|
# Runs subset_font if config.toml has been modified. #
|
|
|
|
# Runs subset_font if config.toml has been modified. #
|
|
|
|
|
|
|
|
# Aborts the commit if a draft .md file is being committed. #
|
|
|
|
# Updates the README if the line numbers for the language section have changed. #
|
|
|
|
# Updates the README if the line numbers for the language section have changed. #
|
|
|
|
#################################################################################
|
|
|
|
#################################################################################
|
|
|
|
|
|
|
|
|
|
|
@ -24,11 +24,16 @@ function extract_date() {
|
|
|
|
grep -m 1 "^$field =" "$file" | sed -e "s/$field = //" -e 's/ *$//'
|
|
|
|
grep -m 1 "^$field =" "$file" | sed -e "s/$field = //" -e 's/ *$//'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
##################################################################
|
|
|
|
# Function to check if the .md file is a draft.
|
|
|
|
# Compress PNG files with either oxipng or optipng if available. #
|
|
|
|
function is_draft() {
|
|
|
|
# Update the "updated" field in the front matter of .md files. #
|
|
|
|
local file="$1"
|
|
|
|
# https://osc.garden/blog/zola-date-git-hook/ #
|
|
|
|
awk '/^\+\+\+$/,/^\+\+\+$/{if(/draft = true/){exit 1}}' "$file"
|
|
|
|
##################################################################
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# Check if the script is being run from the root of the repo.
|
|
|
|
|
|
|
|
if [[ ! $(git rev-parse --show-toplevel) == $(pwd) ]]; then
|
|
|
|
|
|
|
|
error_exit "This script must be run from the root of the repo."
|
|
|
|
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
|
|
# Check if oxipng is installed.
|
|
|
|
# Check if oxipng is installed.
|
|
|
|
png_compressor=""
|
|
|
|
png_compressor=""
|
|
|
@ -38,11 +43,18 @@ elif command -v optipng &> /dev/null; then
|
|
|
|
png_compressor="optipng -o 7"
|
|
|
|
png_compressor="optipng -o 7"
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
|
|
# Get the modified .md and .png files, ignoring "_index.md" files.
|
|
|
|
##################################################################
|
|
|
|
modified_files=$(git diff --cached --name-only --diff-filter=M | grep -E '\.(md|png)$' | grep -v '_index.md$')
|
|
|
|
# Compress PNG files with either oxipng or optipng if available. #
|
|
|
|
|
|
|
|
# Update the "updated" field in the front matter of .md files. #
|
|
|
|
|
|
|
|
# https://osc.garden/blog/zola-date-git-hook/ #
|
|
|
|
|
|
|
|
# Interrupt the commit if a draft .md file is being committed. #
|
|
|
|
|
|
|
|
##################################################################
|
|
|
|
|
|
|
|
|
|
|
|
# Loop through each modified .md or .png file.
|
|
|
|
# Get the newly added and modified .md and .png files, ignoring "_index.md" files.
|
|
|
|
for file in $modified_files; do
|
|
|
|
all_changed_files=$(git diff --cached --name-only --diff-filter=AM | grep -E '\.(md|png)$' | grep -v '_index.md$')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# Loop through each newly added or modified .md or .png file for PNG optimization and draft checking.
|
|
|
|
|
|
|
|
for file in $all_changed_files; do
|
|
|
|
# If the file is a PNG and png_compressor is set, compress it and add it to the commit.
|
|
|
|
# If the file is a PNG and png_compressor is set, compress it and add it to the commit.
|
|
|
|
if [[ "$file" == *.png ]] && [[ -n "$png_compressor" ]]; then
|
|
|
|
if [[ "$file" == *.png ]] && [[ -n "$png_compressor" ]]; then
|
|
|
|
$png_compressor "$file" || error_exit "Failed to compress PNG file $file"
|
|
|
|
$png_compressor "$file" || error_exit "Failed to compress PNG file $file"
|
|
|
@ -50,40 +62,49 @@ for file in $modified_files; do
|
|
|
|
continue
|
|
|
|
continue
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
|
|
# If the file is an .md file, update the "updated" field in the front matter.
|
|
|
|
# If the file is an .md file and it's a draft, abort the commit.
|
|
|
|
if [[ "$file" == *.md ]]; then
|
|
|
|
if [[ "$file" == *.md ]]; then
|
|
|
|
# Get the last modified date from the filesystem.
|
|
|
|
if is_draft "$file"; then
|
|
|
|
last_modified_date=$(date -r "$file" +'%Y-%m-%d')
|
|
|
|
error_exit "Draft file $file is being committed!"
|
|
|
|
|
|
|
|
|
|
|
|
# Extract the "date" field from the front matter.
|
|
|
|
|
|
|
|
date_value=$(extract_date "$file" "date")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# Skip the file if the last modified date is the same as the "date" field.
|
|
|
|
|
|
|
|
if [[ "$last_modified_date" == "$date_value" ]]; then
|
|
|
|
|
|
|
|
continue
|
|
|
|
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
|
|
|
|
fi
|
|
|
|
|
|
|
|
done
|
|
|
|
|
|
|
|
|
|
|
|
# Update the "updated" field with the last modified date.
|
|
|
|
# Get the modified .md to update the "updated" field in the front matter.
|
|
|
|
# If the "updated" field doesn't exist, create it below the "date" field.
|
|
|
|
modified_files=$(git diff --cached --name-only --diff-filter=M | grep -E '\.md$' | grep -v '_index.md$')
|
|
|
|
if ! awk -v date_line="$last_modified_date" 'BEGIN{FS=OFS=" = "; first = 1} {
|
|
|
|
|
|
|
|
if (/^date =/ && first) {
|
|
|
|
|
|
|
|
print; getline;
|
|
|
|
|
|
|
|
if (!/^updated =/) print "updated" OFS date_line;
|
|
|
|
|
|
|
|
first=0;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if (/^updated =/ && !first) gsub(/[^ ]*$/, date_line, $2);
|
|
|
|
|
|
|
|
print;
|
|
|
|
|
|
|
|
}' "$file" > "${file}.tmp"
|
|
|
|
|
|
|
|
then
|
|
|
|
|
|
|
|
error_exit "Failed to process $file with AWK"
|
|
|
|
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
mv "${file}.tmp" "$file" || error_exit "Failed to overwrite $file with updated content"
|
|
|
|
# Loop through each modified .md file.
|
|
|
|
|
|
|
|
for file in $modified_files; do
|
|
|
|
|
|
|
|
# Get the last modified date from the filesystem.
|
|
|
|
|
|
|
|
last_modified_date=$(date -r "$file" +'%Y-%m-%d')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# Extract the "date" field from the front matter.
|
|
|
|
|
|
|
|
date_value=$(extract_date "$file" "date")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# Skip the file if the last modified date is the same as the "date" field.
|
|
|
|
|
|
|
|
if [[ "$last_modified_date" == "$date_value" ]]; then
|
|
|
|
|
|
|
|
continue
|
|
|
|
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
|
|
# Stage the changes.
|
|
|
|
# Update the "updated" field with the last modified date.
|
|
|
|
git add "$file"
|
|
|
|
# If the "updated" field doesn't exist, create it below the "date" field.
|
|
|
|
|
|
|
|
if ! awk -v date_line="$last_modified_date" 'BEGIN{FS=OFS=" = "; first = 1} {
|
|
|
|
|
|
|
|
if (/^date =/ && first) {
|
|
|
|
|
|
|
|
print; getline;
|
|
|
|
|
|
|
|
if (!/^updated =/) print "updated" OFS date_line;
|
|
|
|
|
|
|
|
first=0;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if (/^updated =/ && !first) gsub(/[^ ]*$/, date_line, $2);
|
|
|
|
|
|
|
|
print;
|
|
|
|
|
|
|
|
}' "$file" > "${file}.tmp"
|
|
|
|
|
|
|
|
then
|
|
|
|
|
|
|
|
error_exit "Failed to process $file with AWK"
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
mv "${file}.tmp" "$file" || error_exit "Failed to overwrite $file with updated content"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# Stage the changes.
|
|
|
|
|
|
|
|
git add "$file"
|
|
|
|
done
|
|
|
|
done
|
|
|
|
|
|
|
|
|
|
|
|
#########################################################
|
|
|
|
#########################################################
|
|
|
|