summarylogtreecommitdiffstats
path: root/wiki-docs-search
blob: 3fde0990a1b9df0824af2003b40a7c13abf8aaf1 (plain)
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
#!/bin/bash

# fogobogo> keenerd: we want edit + submit for wiki-search-html

# Written by keenerd for the arch-wiki-lite package
# Mostly rewritten by magnus@iastate.edu for use with only the arch-wiki-docs package

# Known bugs: does not de-encode encoded names (if an arch wiki page contains unicode)
# I don't know what they are encoded in so I don't know how to fix it yet
# If you search for HTML or common stuff (like the phrase AUR) you will get garbage results

language=${language:-"en"}
tmp=${tmp:-"/tmp/.wiki-docs-search.${USER}.tmp"}
num_hits=${num_hits:-15}
html_path="/usr/share/doc/arch-wiki/html"

save_search() {  # mostly for scripting wiki-search-html
	query="$*"
	if [[ "$#" > "1" ]]; then
		query="$(echo "$*" | sed 's/^/\\\(/; s/$/\\\)/; s/ /\\\|/g')"
	fi
	regex_search "$query" > $tmp
}
pretty_print() {
	path_prefix="$html_path/$language/"
	path_prefix_length=${#path_prefix}
	pretty_name=${1:$path_prefix_length}
	pretty_name=${pretty_name/.html/}
	pretty_name=${pretty_name//_/ }
	printf "| %-5s | $pretty_name\n" $2
}
show_hits() {
	i=0
	while read line; do
		pretty_print $line $i
		i="$((i+1))"
	done <$tmp
}
regex_search()  {  # regex -> paths
	# this stupid thing is 50 times faster than grep -ic
	#query=$(tr 'A-Z' 'a-z' <<< "$1")
	#zcat "$wikiball" | tr 'A-Z' 'a-z' | sed 's/@@[bw]//g' | grep "$query" | cut -d: -f1 | uniq -c | sort -nr | sed 's/^ *[0-9]* //'

	# While keenerd must have had a rare moment of genius there, I think grep is probably fast enough and we don't want to download the wikiball
	query=$(tr 'A-Z' 'a-z' <<< "$1")
	grep -icR "$query" "$html_path/$language"     |
		sed 's/\(.*\):/\1\t/'                 | # replace the last : (from grep -c) to a tabstop since some of the wiki pages have colons in their names
		awk -F '	' '{print $2 " " $1}' | # Reverse the order so sort can sort the hits
		sort -r -g                            | # Sort the hits
		head -n $num_hits                     | # Trim the number of hits
		awk '{print $2}'
}
usage() {
	printf 'Usage: wiki-docs-search [-h] [q] search query terms\n\nA script to browse the arch-wiki-docs package more easily, inspired from the one in arch-wiki-lite\n\n\t-q\tDo not prompt interactively or produce output; only create a /tmp/.wiki-search file\n\t-h\tShow this help and exit\n'
}

#Process arguments
OPTIND=1
while getopts "hq" opt; do
	case "$opt" in
	q)  quiet="t" ;;
	h)
		usage
		exit 0
		;;
	esac
done
shift $((OPTIND-1))

if [[ -z "$@" ]]; then
	usage
	exit 1
fi

if [[ ! -d "$html_path" ]]; then
	echo 'Please run `pacman -S arch-wiki-docs` to be able to browse the arch wiki offline'
	exit 1
fi

if [[ -z "$wiki_browser" ]]; then
	which xdg-open &> /dev/null && wiki_browser="xdg-open"
fi

if [[ -z "$wiki_browser" ]]; then
	echo 'Install xdg-utils or export the $wiki_browser variable first'
	exit 1
fi

to_open=""
case "$1" in
	[0-9]*)
		if [ ! -f "$tmp" ]; then
			echo "Error: no previous query"
			exit 1
		fi
		to_open=$(($1 + 1))
		;;
	*)
		save_search $@
		[[ -z "$quiet" ]] || exit 0
		show_hits
		printf 'Enter a selection: '
		read selection
		to_open=$(($selection + 1))
		;;
esac
echo $to_open
cat $tmp
file_to_open=`sed "${to_open}"'q;d' $tmp`
path_prefix="$html_path/$language/"
path_prefix_length=${#path_prefix}
url_file_to_open=${file_to_open:$path_prefix_length}

random_port=''
while true; do
	port=$(shuf -i 2000-65000 -n 1)
	echo trying $port
	netstat -lat | grep '^'"$port"'$' > /dev/null
	if [[ $? == 1 ]] ; then
		random_port=$port
		break
	fi
done

url_to_open="http://0.0.0.0:$random_port/$url_file_to_open"
echo $url_to_open

simple-http-server -p $random_port $path_prefix &

#TODO Still does not work. Opening a link doesn't append .html
$wiki_browser $url_to_open