-
Notifications
You must be signed in to change notification settings - Fork 0
/
mkay
executable file
·146 lines (131 loc) · 3.76 KB
/
mkay
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
#!/bin/bash
BASE_DIR=.diary
CONFIG_FILE=${BASE_DIR}/config
ENTRIES_DIR=${BASE_DIR}/entries
FULL_DIARY_FILE=${BASE_DIR}/full.md
[ ! -d ${BASE_DIR} ] && mkdir ${BASE_DIR}
if [ -f ${CONFIG_FILE} ]; then
# echo "Loading config file.."
source ${CONFIG_FILE}
fi
FILES=${FILES:-Readme.md}
START_TAG="<!-- ${HINT}START mkay-diary -->"
END_TAG="<!-- ${HINT}END mkay-diary -->"
START_EXPLICIT_TAG="<!-- ${HINT}START mkay-diary generated diary please keep comment here to allow auto update -->"
setup() {
local component=$1
local value=$2
if [ -z "$component" ]; then
# List config
[ -f ${CONFIG_FILE} ] && cat ${CONFIG_FILE}
else
touch ${CONFIG_FILE}
escaped_value=$(echo -n "$value" | sed 's/"/\\"/g')
case $component in
editor)
echo "DIARY_EDITOR=\"${escaped_value}\"" >> ${CONFIG_FILE}
;;
esac
fi
}
edit_or_new_entry() {
#local arg1=$1
local month="./.diary/entries/$( date +%Y/%m )"
#if [ -z "$arg1" ]; then
# local folder=`basename $(pwd)`
# echo "folder=${folder}"
# month="${HOME}/.diary/${folder}/$( date +%Y/%m )"
#fi
local day="$( date +%d )"
local entry="$month/$day.md"
#echo "month=${month}"
#echo "entry=${entry}"
mkdir -p "$month"
[ -f "$entry" ] || echo "## Diary for $(date +%D )" > "$entry"
#bash -c "${DIARY_EDITOR} $entry"
#echo "DIARY_EDITOR=[${DIARY_EDITOR}]"
# Not working:
#cmd=()
#for i in ${DIARY_EDITOR}; do
# cmd+=($i)
#done
#cmd+=(${DIARY_EDITOR})
#cmd+=("$entry")
#"${cmd[@]}"
bash -c "${DIARY_EDITOR} \"$entry\""
}
search() {
local terms=$1
echo "Searching entries with [$terms]..."
# TODO: make it work on linux too...
find ${ENTRIES_DIR} -type f -name "*.md" -print0 | xargs -0 grep -i "${terms}"
echo -e ""
}
index() {
echo -e "\n[//]: # (DO NOT EDIT THIS FILE. Its content is automatically generated from other files.)\n" > ${FULL_DIARY_FILE}
find ${ENTRIES_DIR} -type f -name "*.md" | sort | xargs cat >> ${FULL_DIARY_FILE}
}
generate() {
local files="Readme.md"
if [ ! -z "$1" ]; then
files=$@
fi
index
local payload=`cat ${FULL_DIARY_FILE}`
local file
for file in ${files}; do
if [ ! -f $file ]; then
echo "Cannot not find $file."
return 1
fi
explicitTagPresent=$(cat ${file} | grep "${START_EXPLICIT_TAG}")
if [ ! -z "${explicitTagPresent}" ]; then
sed -n "1,/${START_EXPLICIT_TAG}/p;/${END_TAG}/,\$p" ${file} > /tmp/diary.1.tmp
local header="\n# Diary"
if [ "${INCLUDE_HEADER}" = "false" ]; then
header=""
fi
sed -e "/${START_EXPLICIT_TAG}/{s/${START_EXPLICIT_TAG}/${START_EXPLICIT_TAG}${header}/;" -e "r ${FULL_DIARY_FILE}" -e "}" /tmp/diary.1.tmp > /tmp/diary.2.tmp # > ${file}.tmp
mv /tmp/diary.2.tmp $file
else
sed -e "/${START_TAG}/{s/${START_TAG}/${START_EXPLICIT_TAG}\n# Diary\n/g;" -e "r ${FULL_DIARY_FILE}" -e "}" ${file} > /tmp/diary.0.tmp # > ${file}.tmp
mv /tmp/diary.0.tmp $file
fi
# cat ${file} | sed '/<!-- START mkay-diary -->/{ s/<!-- START mkay-diary -->//g ; r '${FULL_DIARY_FILE}' ; }' > ${file}.tmp
done
return 0
}
help() {
LINE=()
LINE+=("Usage:\n$0 [command] [options]")
LINE+=("\nwhere commands can be:")
LINE+=("\n i|index\t\t\t# Concat all entries into one diary file.")
LINE+=("\n e|entry|edit\t\t# Create or edit daily entry.")
LINE+=("\n c|cat\t\t\t# Display all entries in chronological order")
LINE+=("\n s|search [terms]\t\t# Search for terms in all entries.")
LINE+=("\n c|config [component] [value]\t\t# List or set configuration.")
LINE+=("\n g|generate \t\t\t\t# Replace tags in Readme.md file.")
LINE+=("\n")
echo -e "${LINE[@]}"
}
case $1 in
i | index)
index
;;
e | entry | edit)
edit_or_new_entry $2
;;
s | search)
search $2
;;
c | config)
setup "$2" "$3"
;;
g | generate)
shift
generate $@
;;
*)
help
;;
esac