acoeur/bin/_commands/prop.sh
2022-10-26 23:12:29 +02:00

96 lines
2.0 KiB
Bash
Executable File

#!/bin/sh
. ./_commands/_extract.sh
dir=$1 file=$2
content=$(cat "$dir/$file")
page=${file%.prop}
echo
echo "= $dir/$page.md"
if [ ! -e "$dir/$page.md" ]
then
echo "Create file"
echo "---
---
" > "$dir/$page.md"
fi
property="${content%: *}"
line_number=$(grep -n "$property:" "$dir/$page.md")
line_number="${line_number%%:*}"
case $property in
*\[*\]*)
line_offset=${property##*[}
line_offset=${line_offset%%]*}
property=${property%%[*}
content="- ${content##*: }"
line_number=$(grep -n "$property:" "$dir/$page.md")
line_number="${line_number%%:*}"
line_number=$((line_number+line_offset+1))
# echo "line_offset: $line_offset"
esac
# echo "line_number: $line_number"
case $line_number in
*[0-9]*)
line=$(sed "$line_number!d" "$dir/$page.md")
# echo "<= $line"
esac
# echo "=> $content"
if [ "$content" = "$property: " ] || [ "$content" = "- " ]
then
echo "Delete property '$property'"
operation=d
content=
elif [ "$line" != "" ] && [ "${line%- *}" = "" ]
then
echo "Updating nth element of property '$property'"
operation=c
elif [ "$line_number" != "" ] && [ "${content%- *}" = "" ]
then
if grep -q "^$property:" "$dir/$page.md"
then
echo "Insert nth element of property '$property'"
else
echo "Creating property $property"
line_number=$(sed -n "/---/=" "$dir/$page.md" | tail -1)
content="$property:\n$content"
fi
echo "=> $content"
operation=i
elif grep -q "^$property: " "$dir/$page.md"
then
echo "Updating property '$property'"
operation=c
else
echo "Create property '$property'"
line_number=$(sed -n "/---/=" "$dir/$page.md" | tail -1)
operation=i
fi
# See https://www.thegeekstuff.com/2009/11/unix-sed-tutorial-append-insert-replace-and-count-file-lines/
sed -i "$line_number $operation $content" "$dir/$page.md"
if [ "$property" = "title" ]
then
echo "Reindexing pages"
grep --include=*.md -or "title: .*" ../content/ | grep -v "pages.txt" | grep -v "users/" |
grep -v "_index.md" | grep -o "content/.*" | grep -o "/.*" > ../content/pages.txt
fi