forked from tsgates/arch-wiki-markdown
-
Notifications
You must be signed in to change notification settings - Fork 0
/
arch-wiki
executable file
·151 lines (132 loc) · 3.21 KB
/
arch-wiki
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
#!/usr/bin/env bash
SHARE="/usr/share/arch-wiki-markdown"
WIKI_ROOT="/usr/share/doc/arch-wiki-markdown"
LESS="vim -u /usr/share/vim/vim7*/macros/less.vim -n"
SCRIPT_NAME="$(grep -o -e '[^\/]*$' <<< $0)"
usage() {
echo "USAGE:
$SCRIPT_NAME [-n|-#] {STRING}
Search for wiki article names containing the given {STRING},
and optionally disable interactive search with the -n flag,
or choose to directly view a given result using the -#
flag, where # is the search result number you wish to view.
-n | --normal
Normal mode (no interactive searching)
-#
View the #th result
$SCRIPT_NAME [OPTION]
OPTIONS:
-g {STRING} | --grep {STRING}
Search the wiki for the given string
-v | --version
Display the version and exit
-h | --help
Show this help dialog
EXAMPLES:
$SCRIPT_NAME PKGBUILD
Search for wiki articles with names containing "PKGBUILD"
$SCRIPT_NAME -3 install
View the 3rd wiki article with a name containing "install"
Wiki files are located in $WIKI_ROOT"
}
# we require input
if [[ $# == 0 ]]; then
usage
exit 0
fi
# parse options
AWESOME_STYLE=1
while [[ $# > 0 ]]; do
case $1 in
-h|--help)
usage
exit
;;
-g|--grep)
shift
GLOBAL_SEARCH="$1"
shift
;;
-n|--normal)
AWESOME_STYLE=0
shift
;;
-l|--list)
find -type f "$WIKI_ROOT"
exit 0
;;
-v|--version)
cat "${SHARE}/version"
exit 0
;;
*)
break
;;
esac
done
# grep
if [[ -n $GLOBAL_SEARCH ]]; then
echo "Searching for $GLOBAL_SEARCH"
(cd "$WIKI_ROOT"; grep --color=always -rn "$GLOBAL_SEARCH" | less -X -F -R)
exit
fi
# search title
KEY=N/A
NTH=0
while [ -n "$1" ]; do
case $1 in
-*) NTH=${1##-}; AWESOME_STYLE=0 ;;
*) KEY=$1 ;;
esac
shift
done
# trim/search
NTH=$((NTH - 1))
DOC=( $(ls "$WIKI_ROOT" | grep -i $KEY) )
# open a doc
open() {
$LESS -c 'let &runtimepath=printf('"'%s,${SHARE}'"',&runtimepath)|runtime arch-wiki-markdown.vimrc' "${WIKI_ROOT}/${DOC[$1]}"
exit 0
}
# no match
if [[ ${#DOC[*]} == 0 ]]; then
echo "Can't find $KEY"
exit 1
fi
if [[ ! "$TERM" = "linux" ]] && [[ $AWESOME_STYLE -eq 1 ]]; then
C1="\e[44m"
C2="\e[1;31m"
CR="\e[0m"
fi
# single match or nth is specified
if [[ ${#DOC[*]} == 1 ]] || [[ $NTH != -1 ]]; then
open $NTH
else
# menu
echo
for ((cnt=0; cnt<${#DOC[*]}; cnt ++)); do
[[ $AWESOME_STYLE -eq 1 ]] && echo -e -n "${C1}[$((cnt+1))]${CR} ${C2}"
echo -e -n $(sed 's|\.md$||' <<< ${DOC[$cnt]})
[[ $AWESOME_STYLE -eq 1 ]] && echo -e ${CR} || echo
done
# now ask the user for number
if [ $AWESOME_STYLE -eq 1 ] ; then
upto=${#DOC[*]}
echo ; # just for nice formatting
while true; do
read -p "Please choose a number from the list [1-$upto|q]: " input
case $input in
[Qq]*) exit;;
[0-9]*)
if ((1<=$input && $input<=$upto)) ; then
echo "Opening" ${DOC[$input-1]};
open $(($input-1))
else
echo "Invalid number" $input;
fi
;;
*) echo "Invalid option. Use q to quit.";;
esac
done
fi
fi