This repository has been archived by the owner on Sep 5, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pikkuwiki.sh
executable file
·348 lines (278 loc) · 8.13 KB
/
pikkuwiki.sh
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
#!/bin/sh
# Read env variables
[ -z "$PIKKUWIKI_DIR" ] && PIKKUWIKI_DIR="$HOME/pikkuwiki"
[ -z "$EDITOR" ] && EDITOR=vi
[ -z "$PAGER" ] && PAGER=cat
[ -z "$PW_DEFAULT_PAGE" ] && PW_DEFAULT_PAGE="index"
[ -z "$PW_FILE_EXT" ] && PW_FILE_EXT="txt"
# Enable "strict" mode
set -eu
# Pattern for recognizing links to other files
LINK_PATTERN='~\S\+'
grep_links() {
grep -ow "$LINK_PATTERN"
}
filename_to_link() {
local link
link=${1#$PIKKUWIKI_DIR}
link=${link#/}
link=${link%.$PW_FILE_EXT}
echo "$link"
}
starts_with() {
local str="$1"
local prefix="$2"
local prefix_len=${#prefix}
[ "$(echo "$str" | cut -c"$prefix_len")" = "$prefix" ]
}
resolve_link() {
local context=${1#$PIKKUWIKI_DIR}
context="/${context#/}"
context=${context%/*}
local link=${2#\~}
link=${link%.$PW_FILE_EXT}
link=${link:-$PW_DEFAULT_PAGE}
if starts_with "$link" "/" || [ "$context" = "/" ]; then
context="$PIKKUWIKI_DIR"
else
context="$PIKKUWIKI_DIR$context"
fi
link=${link#/}
echo "$context/$link.$PW_FILE_EXT"
}
resolve_links() {
while read line; do
resolve_link "$1" "$line"
done
}
find_links_from_file() {
grep_links < "$1" | grep "$2" | resolve_links "$filename"
}
find_links() {
local filename="${1:-}"
local pattern=${2:-}
if [ ! -f "$filename" ]; then
filename=$(resolve_link "" "$filename")
fi
if [ ! -f "$filename" ]; then
echo "Could not find a file for '$1'!" 1>&2
fi
find_links_from_file "$filename" "$pattern"
}
find_pages() {
if [ "$1" ]; then
find "$PIKKUWIKI_DIR" -iname "*.$PW_FILE_EXT" -iname "*$1*"
else
find "$PIKKUWIKI_DIR" -iname "*.$PW_FILE_EXT"
fi
}
do_to_lines() {
if [ ! "$1" ]; then
cat
fi
while read line; do
$1 "$line"
done
}
format_links() {
local format=${1:-}
local formatter=""
local after_formatter=""
case "$format" in
h|head|header) formatter="formatter_header" ;;
l|link) formatter="filename_to_link" ;;
""|f|file) ;;
p|pretty)
formatter="formatter_pretty"
after_formatter="after_formatter_pretty"
;;
space)
formatter="filename_to_link"
after_formatter="lines_to_words"
;;
*)
echo "Unknown formatter '$format'!" 1>&2
return 1
;;
esac
if [ "$after_formatter" ]; then
do_to_lines "$formatter" | $after_formatter
else
do_to_lines "$formatter"
fi
}
formatter_header() {
local filename=${1:-}
local heading="[No file]"
[ -f "$filename" ] && heading=$(head -n1 "$filename")
echo "$heading"
}
formatter_pretty() {
local filename=${1:-}
local link=$(filename_to_link "$filename")
printf "%s: %s\n" "$link" "$(formatter_header "$filename")"
}
after_formatter_pretty() {
column -t -s" "
}
lines_to_words() {
tr '\n' ' '
}
find_and_format_pages() {
local pattern=""
local format=""
while getopts "p:F:" flag; do
case "$flag" in
p) pattern=${OPTARG:-} ;;
F) format=${OPTARG:-} ;;
esac
done
find_pages "$pattern" | format_links "$format" | sort -u
}
show_and_format_links() {
local pattern=""
local format=""
local link=""
while getopts "l:p:F:" flag; do
case "$flag" in
l) link=${OPTARG:-} ;;
p) pattern=${OPTARG:-} ;;
F) format=${OPTARG:-} ;;
esac
done
find_links "$link" "$pattern" | format_links "$format" | sort -u
}
open_link() {
$EDITOR "$(resolve_link "" "$1")"
}
view_link() {
$PAGER "$(resolve_link "" "$1")"
}
init_pikkuwiki() {
local firstpage="$PIKKUWIKI_DIR/$PW_DEFAULT_PAGE.$PW_FILE_EXT"
if [ ! -d "$PIKKUWIKI_DIR" ]; then
mkdir -p "$PIKKUWIKI_DIR"
fi
if [ ! -f "$firstpage" ]; then
cat <<EOF > "$firstpage"
Homepage
========
Hi, this is your first pikkuwiki page.
Add more .$PW_FILE_EXT files to this directory.
EOF
echo "pikkuwiki initialized successfully!" 1>&2
echo "your first page can be found from: $firstpage" 1>&2
else
echo "pikkuwiki is already initialized!" 1>&2
fi
}
# Main program
run_pikkuwiki() {
local cmd=${1:-}
if [ ! "$cmd" ]; then
unknown_command
exit $?
fi
shift
case "$cmd" in
init) init_pikkuwiki ;;
v|view) view_link "${1:-}" ;;
o|open) open_link "${1:-}" ;;
f|find) find_and_format_pages "$@" ;;
s|show) show_and_format_links "$@" ;;
r|resolve) resolve_link "${1:-}" "${2:-}" ;;
h|help) print_fullhelp ;;
*) unknown_command "$cmd" ;;
esac
}
unknown_command() {
if [ "${1:-}" ]; then
echo "Unknown command '$1'!" 1>&2
else
echo "No command specified!" 1>&2
fi
print_minihelp 1>&2
return 1
}
print_minihelp() {
cat <<'EOF'
usage: pikkuwiki <command> [arguments]
Commands:
init Initialize pikkuwiki. Creates the pikkuwiki directory.
o, open Open a given link using EDITOR. If link is empty,
PW_DEFAULT_PAGE or "index" is opened instead.
v, view Open a given link using PAGER. If link is empty,
PW_DEFAULT_PAGE or "index" is opened instead.
f, find Find pages using the given pattern. Outputs the filenames of
found links unless alternative formatting is provided.
s, show Show links from given page. Outputs the filenames of the found
links unless alternative formatting is provded.
r, resolve Resolve filename for given filename and link combination
h, help Print full help text
Find arguments:
-p RegEx pattern to use for filtering pages.
-F Use alternative formatting.
Show arguments:
-l link or file to search links from.
-p RegEx pattern to use for filtering pages
-F Use alternative formatting. See the available formatters below.
Formatters:
header first line of the page
file file path of the page (default)
link link to the page from root
pretty the link and the header
space links separated by spaces (useful for bash completion list)
EOF
}
print_fullhelp() {
echo "pikkuwiki - Minimal personal wiki tool"
print_minihelp
cat <<'EOF'
Examples
========
Open a page in editor:
pikkuwiki open '~America/Canada'
pikkuwiki o Europe/Germany
Find pages:
pikkuwiki find 'code'
pikkuwiki f 'eng'
Show all links in a page:
pikkuwiki show -l Europe/Germany
pikkuwiki s -l Europe
Show matching links:
pikkuwiki s -l Europe/Germany -p 'Ber'
Resolve link:
pikkuwiki resolve Europe Germany
pikkuwiki r $PIKKUWIKI_DIR/Europe/Germany.txt Berlin
Configuration
=============
Pikkuwiki can be configured through environment variables.
PIKKUWIKI_DIR The directory where pages are located.
Default: $HOME/pikkuwiki
EDITOR The editor that the open command launches.
Default: vi
PAGER The viewer that is view command launches.
Default: cat
PW_DEFAULT_PAGE The default page that is opened if no link
is provided for open command.
Default: index
PW_FILE_EXT The file extension for pages.
Default: txt
Link syntax
===========
All links to other pages start with tilde (~).
All pages point to a .txt file by default (case sensitive).
The file extension can be customized by changing the PW_FILE_EXT variable.
The page which the link refers to depends on where the page that is linking.
Absolute links:
~/Europe => $PIKKUWIKI_DIR/Europe.txt
~/Europe/Germany => $PIKKUWIKI_DIR/Europe/Germany.txt
Relative links in '~/Europe' page:
~America => $PIKKUWIKI_DIR/America.txt
~America/Canada => $PIKKUWIKI_DIR/America/Canada.txt
Relative links in '~/Europe/Germany' page:
~Berlin => $PIKKUWIKI_DIR/Europe/Germany/Berlin.txt
~Munich => $PIKKUWIKI_DIR/Europe/Germany/Munich.txt
EOF
}
run_pikkuwiki "$@"