]> git.plutz.net Git - shellwiki/blob - handlers/40_search.sh
use tags (#positive and !negative) in search
[shellwiki] / handlers / 40_search.sh
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 [ "$SEARCH_INDEX" != true ] && return 1
18 [ "${PATH_INFO%\[search\]}" = "$PATH_INFO" ] && return 1
19
20 . "$_EXEC/cgilite/storage.sh"
21 . "$_EXEC/cgilite/db23.sh"
22
23 I="$_DATA/index"
24 tags="$( GET q | awk '
25   BEGIN { # Field separator FS should include punctuation, including Unicode Block U+2000 - U+206F
26           if ( length("¡") == 1 )  # Utf-8 aware AWK
27           FS = "([] \\t\\n\\r!\"'\''()*+,./:;<=>?\\\\^_`{|}~[-]|%[0-9A-Fa-f]{2}|'"$(printf '[\342\200\200-\342\201\257]')"')+";
28           else                     # UTF-8 Hack
29           FS = "([] \\t\\n\\r!\"'\''()*+,./:;<=>?\\\\^_`{|}~[-]|%[0-9A-Fa-f]{2}|'"$(printf '\342\200[\200-\277]|\342\201[\201-\257]')"')+";
30           fi
31         }
32   { for (n = 1; n <= NF; n++) if ($n ~ /#[[:alnum:]_]+/) {
33       sub(/^#/,"",$n)
34       printf "%s        ", toupper($n);
35     }
36   }
37 ')"
38
39 ntags="$( GET q | awk '
40   BEGIN { # Field separator FS should include punctuation, including Unicode Block U+2000 - U+206F
41           if ( length("¡") == 1 )  # Utf-8 aware AWK
42           FS = "([] \\t\\n\\r\"#'\''()*+,./:;<=>?\\\\^_`{|}~[-]|%[0-9A-Fa-f]{2}|'"$(printf '[\342\200\200-\342\201\257]')"')+";
43           else                     # UTF-8 Hack
44           FS = "([] \\t\\n\\r\"#'\''()*+,./:;<=>?\\\\^_`{|}~[-]|%[0-9A-Fa-f]{2}|'"$(printf '\342\200[\200-\277]|\342\201[\201-\257]')"')+";
45           fi
46         }
47   { for (n = 1; n <= NF; n++) if ($n ~ /![[:alnum:]_]+/) {
48       sub(/^!/,"",$n)
49       printf "%s        ", toupper($n);
50     }
51   }
52 ')"
53
54 words="$( GET q | awk '
55   BEGIN { # Field separator FS should include punctuation, including Unicode Block U+2000 - U+206F
56           if ( length("¡") == 1 )  # Utf-8 aware AWK
57           FS = "([] \\t\\n\\r\"#'\''()*+,./:;<=>?\\\\^_`{|}~[-]|%[0-9A-Fa-f]{2}|'"$(printf '[\342\200\200-\342\201\257]')"')+";
58           else                     # UTF-8 Hack
59           FS = "([] \\t\\n\\r\"#'\''()*+,./:;<=>?\\\\^_`{|}~[-]|%[0-9A-Fa-f]{2}|'"$(printf '\342\200[\200-\277]|\342\201[\201-\257]')"')+";
60           fi
61         }
62   { for (n = 1; n <= NF; n++) if ($n !~ /![[:alnum:]_]+/) {
63       sub(/!/," ",$n)
64       printf "%s        ", tolower($n);
65     }
66   }
67 ')"
68
69 searchteaser() {
70   local file="$1" words db3_data
71   local w l nc nl hits mhits cont mcont
72   shift 1; words="$*"
73
74   for w in ${words}; do
75     grep -hiwnF "$w" "$file"
76   done \
77   | sort -t: -k1 -n \
78   | { nc=-1 hits=0 mhits=0
79     while read -r l; do
80       nl="$nc" nc="${l%%:*}"
81       if [ $nc -eq $nl ]; then
82         hits=$((hits + 1))
83       elif [ $nc -eq $((nl + 1 )) ]; then
84         hits=$((hits + 1))
85         cont="${cont}${BR}${l#*:}"
86       elif [ $hits -gt $mhits ]; then
87         mhits="$hits" mcont="$cont"
88         hits=1 cont="${l#*:}"
89       else
90         hits=1 cont="${l#*:}"
91       fi
92     done
93
94     [ $hits -gt $mhits ] \
95     && STRING "$cont" \
96     || STRING "$mcont"
97   }
98 }
99
100 for w in ${words}; do
101   [ ! -f "$I/$w" ] && continue
102
103   while read date doc freq num total; do
104     P="$_DATA/pages$(UNSTRING "$doc")"
105     d="$(stat -c %Y -- "$P/#index.flag" 2>&-)"
106     [ "$d" -le "$date" -a -f "$P/#page.md" ] 2>&- || continue
107
108     printf '%s  %f\n' "$doc" "$freq"
109   done <"$I/$w"
110 done \
111 | awk '
112       { cnt[$1]++; weight[$1] = weight[$1] ? weight[$1] + $2 : $2; }
113   END { m = 0; for (d in cnt) m = ( m < cnt[d] ) ? cnt[d] : m;
114         for (d in cnt) if ( cnt[d] == m ) printf "%f    %s\n", weight[d], d;
115       }
116 ' \
117 | sort -nr \
118 | while read freq doc; do
119   page="$(UNSTRING "$doc")"
120   [ "${page%*/\[*\]/*}" != "$page" ] && continue
121   if [ "$LANGUAGE_DEFAULT" ]; then
122     [ -d "${_DATA}/pages/${page}/:${LANGUAGE}/" ] && continue
123     [ "${page%/:*/}" = "${page%/:${LANGUAGE}/}" ] || continue
124   fi
125   acl_read "$page" || continue
126   has_tags "$page" $tags || continue
127   has_tag "$page" $ntags && continue
128   printf '%s    %s\n' "$doc" "$(searchteaser "$(mdfile "$page")" $words)"
129 done \
130 | theme_search "${words%        }"