~skiqqy/bin

bin/diary -rwxr-xr-x 2.4 KiB
c3280652Stephen Cochrane Added sandbox script 3 days ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#!/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 "$@"