quartz-research-note/add_timestamp.sh

28 lines
898 B
Bash
Raw Normal View History

2024-02-08 09:01:27 +00:00
#!/bin/bash
cwd=`dirname "${0}"`
function rewritedate () {
echo "$1 : $2"
echo $2 | xargs -I{} yq -i --front-matter="process" '.date="{}"' "$1"
}
2024-02-08 09:01:27 +00:00
FILES=$(find $cwd/content -name "*.md")
IFS=$'\n'
for f in $FILES
do
BDATE=$(stat -f "%SB" "$f");
DATE=$(LC_ALL=C date -j -Iseconds -f "%b %d %H:%M:%S %Y" "$BDATE");
2024-02-08 09:01:27 +00:00
FLINE=$(head -n 1 "$f");
ORIGINDATE=$(yq --front-matter=extract '.date' "$f" | date "+%s")
LOCALDATE=$(LC_ALL=C date -j -f "%b %d %H:%M:%S %Y" "$BDATE" | date "+%s");
GITDATE=$(git blame $f | grep -Eo '\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}'| xargs -I{} date -j -f "%Y-%m-%d %H:%M:%S" "{}" | date "+%s");
if [[ "$FLINE" == "---" ]];
then
if [ $LOCALDATE -lt $ORIGINDATE ]; then
echo "$f has frontmatter and date of local file is older. overwriting."
rewritedate $f $BDATE
2024-02-08 09:01:27 +00:00
fi
else
echo "$f has no frontmatter, adding to it";
2024-02-08 09:01:27 +00:00
gsed -i '1i---\n---' "$f"
rewritedate $f $DATE
2024-02-08 09:01:27 +00:00
fi
done