-
Notifications
You must be signed in to change notification settings - Fork 0
/
zk
executable file
·207 lines (164 loc) · 4.59 KB
/
zk
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
#!/bin/bash
set -eo pipefail
function usage {
echo "Usage: zk {new,search,link,pull,push,help}"
echo ""
echo "Create zettels"
echo " zk new TITLE [tag1,tag2]"
echo ""
echo "Search your kasten"
echo " zk search [tag]"
echo ""
echo "Link zettels"
echo " zk link [src] [dst]"
echo ""
echo "Push changes"
echo " zk push"
echo ""
echo "Pull changes"
echo " zk pull"
echo ""
}
function commit {
local mode=$1
local file=$2
cd $ZETTELKASTEN_PATH &>/dev/null
git add $file README.md
git commit -m "$mode $file" &>/dev/null
cd - &>/dev/null
}
function push {
cd $ZETTELKASTEN_PATH &>/dev/null
git push origin master
cd - &>/dev/null
}
function pull {
cd $ZETTELKASTEN_PATH &>/dev/null
git pull origin master
cd - &>/dev/null
}
function log {
cd $ZETTELKASTEN_PATH &>/dev/null
git log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%ci)%Creset'
cd - &>/dev/null
}
function index {
local index_file=$ZETTELKASTEN_PATH/README.md
local tags=$(awk '$1 == "-" {print $2}' $ZETTELKASTEN_PATH/*.md | sort | uniq)
local files
echo "# Index" > $index_file
while IFS=$'\n' read -ra tag; do
for i in "${tag[@]}"; do
printf "\n## $i\n\n" >> $index_file
grep "$i" -l $ZETTELKASTEN_PATH/*.md | xargs -r grep "title:" --with-filename | awk 'BEGIN {FS="/"};{print $5}' | awk 'BEGIN {FS=":"};{print "* ["$3"]("$1")"}' | sed 's/\[ /\[/g' >> $index_file
done
done <<< "$tags"
}
function link {
local src=$1
local dst=$2
local files
if [ -z "$src" ]; then
src=$(grep "title:" $ZETTELKASTEN_PATH/*.md | awk 'BEGIN {FS="/"};{print $5}' | fzf -d ":" --preview "cat $ZETTELKASTEN_PATH/{1}" --preview-window=up:40%)
if [ -z "$src" ]; then
exit 1
fi
src=$(echo $src | awk 'BEGIN {FS=":"};{print $1}')
fi
if [ -z "$dst" ]; then
dst=$(grep "title:" $ZETTELKASTEN_PATH/*.md | awk 'BEGIN {FS="/"};{print $5}' | fzf -d ":" --preview "cat $ZETTELKASTEN_PATH/{1}" --preview-window=up:40%)
if [ -z "$dst" ]; then
exit 1
fi
dst=$(echo $dst | awk 'BEGIN {FS=":"};{print $1}')
fi
local src_title=$(grep "title:" $ZETTELKASTEN_PATH/$src | cut -d " " -f2-)
local dst_title=$(grep "title:" $ZETTELKASTEN_PATH/$dst | cut -d " " -f2-)
echo "[[$dst]]" >> $ZETTELKASTEN_PATH/$src
echo "[[$src]]" >> $ZETTELKASTEN_PATH/$dst
index
commit "link" $ZETTELKASTEN_PATH/$src
commit "link" $ZETTELKASTEN_PATH/$dst
}
function search {
local tag=$1
local files
if [ ! -z "$tag" ]; then
files=$(grep -l "$tag" $ZETTELKASTEN_PATH/*.md)
else
files=$(ls -1 $ZETTELKASTEN_PATH/*.md)
fi
files=$(printf "$files" | xargs -r grep "title:" --with-filename | awk -F '/' '{print $(NF)}')
local filename=$(printf "$files" | fzf -d ":" --preview "cat $ZETTELKASTEN_PATH/{1}" --preview-window=up:40%)
if [ -z "$filename" ]; then
exit 1
fi
filename=$(echo $filename | awk 'BEGIN {FS=":"};{print $1}')
$EDITOR "$ZETTELKASTEN_PATH/$filename"
index
commit "edit" $filename
}
function new {
local title="$1"
local tags="$2"
local date=$(date +%Y-%m-%d)
local timestamp=$(date +%Y%m%d%H%M%S)
local tmpfile=$(mktemp --suffix=".md")
local filename="$timestamp.md"
local zettel="$ZETTELKASTEN_PATH/$filename"
echo "---" > $tmpfile
echo "title: $title" >> $tmpfile
echo "date: $date" >> $tmpfile
if [ ! -z "$tags" ]; then
echo "tags:" >> $tmpfile
while IFS="," read -ra tag; do
for i in "${tag[@]}"; do
echo " - $i" >> $tmpfile
done
done <<< "$tags"
fi
echo "---" >> $tmpfile
$EDITOR $tmpfile
if [ $? != 0 ]; then
echo "Not saving zettel"
exit 1
fi
cp $tmpfile $zettel
index
commit "add" $filename
}
function main {
local cmd="$1"
if [[ $cmd == "help" ]]; then
usage
exit 0
fi
if [ -z $ZETTELKASTEN_PATH ]; then
echo "Set ZETTELKASTEN_PATH environment variable"
exit 1
fi
case $cmd in
"new")
new "$2" "$3"
;;
"" | "search")
search "$2"
;;
"link")
link "$2" "$3"
;;
"log")
log
;;
"push")
push
;;
"pull")
pull
;;
*)
echo "Unknown command"
exit 1
esac
}
main "$@"