3 # Copyright 2023, 2024 Paul Hänsch
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.
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.
17 [ "$SEARCH_INDEX" != true ] && return 1
18 [ "${PATH_INFO%\[search\]}" = "$PATH_INFO" ] && return 1
20 . "$_EXEC/cgilite/storage.sh"
21 . "$_EXEC/cgilite/db23.sh"
24 words="$( 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]')"')+";
29 FS = "([] \\t\\n\\r!\"#'\''()*+,./:;<=>?\\\\^_`{|}~[-]|%[0-9A-Fa-f]{2}|'"$(printf '\342\200[\200-\277]|\342\201[\201-\257]')"')+";
32 { for (n = 1; n <= NF; n++) printf "%s ", tolower($n); }
36 local file="$1" words db3_data
37 local w l nc nl hits mhits cont mcont
41 grep -hiwnF "$w" "$file"
44 | { nc=-1 hits=0 mhits=0
46 nl="$nc" nc="${l%%:*}"
47 if [ $nc -eq $nl ]; then
49 elif [ $nc -eq $((nl + 1 )) ]; then
51 cont="${cont}${BR}${l#*:}"
52 elif [ $hits -gt $mhits ]; then
53 mhits="$hits" mcont="$cont"
60 [ $hits -gt $mhits ] \
67 [ ! -f "$I/$w" ] && continue
69 while read date doc freq num total; do
70 P="$_DATA/pages$(UNSTRING "$doc")"
71 d="$(stat -c %Y -- "$P/#index.flag" 2>&-)"
72 [ "$d" -le "$date" -a -f "$P/#page.md" ] 2>&- || continue
74 printf '%s %f\n' "$doc" "$freq"
78 { cnt[$1]++; weight[$1] = weight[$1] ? weight[$1] + $2 : $2; }
79 END { m = 0; for (d in cnt) m = ( m < cnt[d] ) ? cnt[d] : m;
80 for (d in cnt) if ( cnt[d] == m ) printf "%f %s\n", weight[d], d;
84 | while read freq doc; do
85 page="$(UNSTRING "$doc")"
86 [ "${page%*/\[*\]/*}" != "$page" ] && continue
87 if [ "$LANGUAGE_DEFAULT" ]; then
88 [ -d "${_DATA}/pages/${page}/:${LANGUAGE}/" ] && continue
89 [ "${page%/:*/}" = "${page%/:${LANGUAGE}/}" ] || continue
91 acl_read "$page" || continue
92 printf '%s %s\n' "$doc" "$(searchteaser "$(mdfile "$page")" $words)"
94 | theme_search "${words% }"