quartz-research-note/add_timestamp.sh

29 lines
1007 B
Bash
Raw Normal View History

2024-02-08 09:01:27 +00:00
#!/bin/bash
#別のマシンで作ったファイルはわからんので、それぞれのでやって古い方で上書きする必要がある
cwd=`dirname "${0}"`
FILES=$(find $cwd/content -name "*.md")
IFS=$'\n'
for f in $FILES
do
BDATE=$(stat -f "%SB" "$f");
FLINE=$(head -n 1 "$f");
if [[ "$FLINE" == "---" ]];
then
echo "$f has frontmatter";
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");
if [ $LOCALDATE -lt $ORIGINDATE ]; then # -lt : '<'
echo "date of local file is older. overwriting."
DATE=$(LC_ALL=C date -j -Iseconds -f "%b %d %H:%M:%S %Y" "$BDATE");
echo "$f : $DATE"
echo $DATE | xargs -I{} yq -i --front-matter="process" '.date="{}"' "$f"
fi
else
echo "$f has no frontmatter, adding";
gsed -i '1i---\n---' "$f"
DATE=$(LC_ALL=C date -j -Iseconds -f "%b %d %H:%M:%S %Y" "$BDATE");
echo "$f : $DATE"
echo $DATE | xargs -I{} yq -i --front-matter="process" '.date="{}"' "$f"
fi
done