My Scripts.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
bin/diary

147 lines
2.4 KiB

#!/usr/bin/env bash
#Simple diary script
#Auther: Stephen Skiqqy Cochrane
# See $ ./diary help
declare -A commands
# Available user commands
commands[help]='help COMMAND\nShows helpful information for a specific command.'
commands[list]='list\nShows available commands.'
commands[open]='open\nOpen current diary entry for editing.'
commands[compile]='compile [PASSWORD]\nCompile diary entries to a single pdf, with an option password.'
### Helper Funcs
usage()
{
cat << EOF
$(basename "$0") ~ Diary Toolkit
Usage: $ $(basename "$0") [OPTIONS] COMMAND [ARGS...]
Options:
-h Shows this message
Commands:
$ $(basename "$0") list
Written by skiqqy.
EOF
exit "${1:-0}"
}
conv_dates()
{
while read -r line
do
date -d "$line" "+[%e/%m/%Y](## %e %B %Y, %A)"
done
}
### End Helper Funcs
dlist()
{
local msg
for key in "${!commands[@]}"
do
msg="$msg\\n$key : $(cut -d '\' -f 2 <<< "${commands[$key]}" | cut -b 2- )"
done
printf 'Commands:\n%b\n\nSee: $ %s help COMMAND\n' "$msg" "$(basename "$0")"
}
dhelp()
{
[ -z "$1" ] && usage && return
local msg
msg="${commands[$1]}"
if [ -n "$msg" ]
then
printf 'Usage: $ %s %b\n' "$(basename "$0")" "$msg"
else
printf 'Unknown command "%s", see: $ %s list\n' "$1" "$(basename "$0")"
fi
}
dopen()
{
local entry
local count
local date
date=$(date "+%d%m%Y")
count="$(dir -A1 "$workdir/entries/"*.md | wc -l | cut -d " " -f 1)"
entry="$(find "$workdir/entries" -regex ".*/[0-9]+_$date.md")"
if [ ! -f "$entry" ]
then
entry="$workdir/entries/$count""_$date.md"
cat << EOF > "$entry"
## $(date "+%e %B %Y, %A")
Dear Diary...
EOF
fi
$ed "$entry"
}
# TODO: Add password.
dcompile()
{
cat << EOF > "$workdir/diary.md"
# $(whoami)'s Diary
## TOC
$(dir -v1 "$workdir/entries/"*.md |
sed -E "s|$workdir/entries/[0-9]+_(.*)\.md|\1|g" |
sed -E "s|([0-9]{2})([0-9]{2})([0-9]{4})|\2/\1/\3|g" | conv_dates)
EOF
for file in $(dir -v1 "$workdir/entries/"*.md)
do
cat "$file" >> "$workdir/diary.md"
done
command -v pandoc && pandoc "$workdir/diary.md" -o "$workdir/diary.pdf"
}
main()
{
workdir="$HOME/.diary"
ed="${EDITOR:-vim}"
while getopts 'h' opt
do
case "$opt" in
h)
usage
;;
*)
usage 1
;;
esac
done
shift "$((OPTIND-1))" # Get rid of parsed args
local com
com="${1:-open}"
shift 1
mkdir -p "$workdir/entries"
if [ -n "${commands[$com]}" ]
then
d"$com" "$@"
else
usage 1
fi
}
main "$@"