]> git.plutz.net Git - shellwiki/blob - macros/search
define search function as macro
[shellwiki] / macros / search
1 #!/bin/sh
2
3 # Copyright 2023, 2024 Paul Hänsch
4
5 # Permission to use, copy, modify, and/or distribute this software for any
6 # purpose with or without fee is hereby granted, provided that the above
7 # copyright notice and this permission notice appear in all copies.
8
9 # THE SOFTWARE IS PROVIDED “AS IS” AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 # WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 # MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
12 # SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 # WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 # ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR
15 # IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16
17 I="$_DATA/index"
18
19 . "$_EXEC/cgilite/cgilite.sh"
20 . "$_EXEC/cgilite/storage.sh"
21 . "$_EXEC/cgilite/db23.sh"
22 . "$_EXEC/tools.sh"
23 . "$_EXEC/acl.sh"
24
25 _(){ printf %s\\n "$*"; }
26 [ "${LANGUAGE}" -a -r "${_EXEC}/l10n/${LANGUAGE}.sh" ] && . "${_EXEC}/l10n/${LANGUAGE}.sh"
27
28 show_form=true show_hits='' action=''
29
30 set -- "$@" --
31 while [ $# -gt 0 ]; do case $1 in
32   --no-form|--noform)
33     show_form=''; shift 1;;
34   --hits|--results)
35     show_hits='true'; shift 1;;
36   --action)
37     action="$(HTML "$2")"; shift 2;;
38   --action=*|action=*)
39     action="$(HTML "${2#*=}")"; shift 1;;
40   --) shift 1; break;;
41   *) set -- "$@" "$1"; shift 1;;
42 esac; done
43
44 searchteaser() {
45   local file="$1" words db3_data
46   local w l nc nl hits mhits cont mcont
47   shift 1; words="$*"
48
49   for w in ${words}; do
50     grep -hiwnF "$w" "$file"
51   done \
52   | sort -t: -k1 -n \
53   | { nc=-1 hits=0 mhits=0
54     while read -r l; do
55       nl="$nc" nc="${l%%:*}"
56       if [ $nc -eq $nl ]; then
57         hits=$((hits + 1))
58       elif [ $nc -eq $((nl + 1 )) ]; then
59         hits=$((hits + 1))
60         cont="${cont}${BR}${l#*:}"
61       elif [ $hits -gt $mhits ]; then
62         mhits="$hits" mcont="$cont"
63         hits=1 cont="${l#*:}"
64       else
65         hits=1 cont="${l#*:}"
66       fi
67     done
68
69     [ $hits -gt $mhits ] \
70     && STRING "$cont" \
71     || STRING "$mcont"
72   }
73 }
74
75 if [ ! "$action" -a "$LANGUAGE_DEFAULT" ]; then
76   action="./:${LANGUAGE}/[search]"
77 elif [ ! "$action" ]; then
78   action="./[search]"
79 fi
80
81 if [ "${show_form}" = true ]; then
82   printf '<form class="macro search" method="GET" action="%s">
83   <input type="search" placeholder="%s" name="q" value="%s"><button type="submit" class="search">%s</button>
84 </form>' "$action" "$(_ Search)" "$([ "$show_hits" = true ] && GET q |HTML)" "$(_ Search)"
85 fi
86
87 if [ "${show_hits}" = true ]; then
88   { read tags; read ntags; read words; } <<-EOF
89         $(GET q | awk '
90         BEGIN { # Field separator FS should include punctuation, including Unicode Block U+2000 - U+206F
91                 if ( length("¡") == 1 )  # Utf-8 aware AWK
92                 FS = "([] \\t\\n\\r\"'\''()*+,./:;<=>?\\\\^_`{|}~[-]|%[0-9A-Fa-f]{2}|'"$(printf '[\342\200\200-\342\201\257]')"')+";
93                 else                     # UTF-8 Hack
94                 FS = "([] \\t\\n\\r\"'\''()*+,./:;<=>?\\\\^_`{|}~[-]|%[0-9A-Fa-f]{2}|'"$(printf '\342\200[\200-\277]|\342\201[\201-\257]')"')+";
95                 fi
96               }
97         { t=0; for (n = 1; n <= NF; n++) if ($n  ~ /#[[:alnum:]_]+/)  tags[t++] = toupper($n);
98           t=0; for (n = 1; n <= NF; n++) if ($n  ~ /![[:alnum:]_]+/) ntags[t++] = toupper($n);
99           t=0; for (n = 1; n <= NF; n++) if ($n !~ /![[:alnum:]_]+/) words[t++] = tolower($n);
100           for (t in  tags) { sub(/^#/, "",  tags[t]); printf "%s        ",  tags[t]; } print "";
101           for (t in ntags) { sub(/^!/, "", ntags[t]); printf "%s        ", ntags[t]; } print "";
102           for (t in words) { sub(/^[!#]/, "", words[t]); printf "%s     ", words[t]; } print "";
103         }
104         ')
105         EOF
106
107   printf '<ul class="macro search hits">'
108   for w in ${words}; do
109     [ ! -f "$I/$w" ] && continue
110   
111     while read date doc freq num total; do
112       P="$_DATA/pages$(UNSTRING "$doc")"
113       d="$(stat -c %Y -- "$P/#index.flag" 2>&-)"
114       [ "$d" -le "$date" -a -f "$P/#page.md" ] 2>&- || continue
115   
116       printf '%s        %f\n' "$doc" "$freq"
117     done <"$I/$w"
118   done \
119   | awk '
120         { cnt[$1]++; weight[$1] = weight[$1] ? weight[$1] + $2 : $2; }
121     END { m = 0; for (d in cnt) m = ( m < cnt[d] ) ? cnt[d] : m;
122           for (d in cnt) if ( cnt[d] == m ) printf "%f  %s\n", weight[d], d;
123         }
124   ' \
125   | sort -nr \
126   | while read freq doc; do
127     page="$(UNSTRING "$doc")"
128     [ "${page%*/\[*\]/*}" != "$page" ] && continue
129     if [ "$LANGUAGE_DEFAULT" ]; then
130       [ -d "${_DATA}/pages/${page}/:${LANGUAGE}/" ] && continue
131       [ "${page%/:*/}" = "${page%/:${LANGUAGE}/}" ] || continue
132     fi
133     acl_read "$page" || continue
134     has_tags "$page" $tags || continue
135     has_tag "$page" $ntags && continue
136     printf '%s  %s\n' "$doc" "$(searchteaser "$(mdfile "$page")" $words)"
137   done \
138   | while read -r p t; do
139     path="$(UNSTRING "$p")" pfrag="${path%/}" title=''
140     while [ "$pfrag" ]; do
141       title="$(page_title "$pfrag")/$title"
142       pfrag="${pfrag%/*}"
143     done
144     printf '<li><a href="%s">%s</a><p>%s</p></li>' \
145       "$(URL "$path")" "$(HTML "/$title")" "$(UNSTRING "$t" |HTML)"
146   done
147   printf '</ul>'
148 fi