]> git.plutz.net Git - shellwiki/blob - macros/search
bugfix: find tags containing "_"
[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='' query=''
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 [ $@ -gt 0 ] && query="$*" || query="$(GET q)"
45
46 searchteaser() {
47   local file="$1" words db3_data
48   local w l nc nl hits mhits cont mcont
49   shift 1; words="$*"
50
51   for w in ${words}; do
52     grep -hiwnF "$w" "$file"
53   done \
54   | sort -t: -k1 -n \
55   | { nc=-1 hits=0 mhits=0
56     while read -r l; do
57       nl="$nc" nc="${l%%:*}"
58       if [ $nc -eq $nl ]; then
59         hits=$((hits + 1))
60       elif [ $nc -eq $((nl + 1 )) ]; then
61         hits=$((hits + 1))
62         cont="${cont}${BR}${l#*:}"
63       elif [ $hits -gt $mhits ]; then
64         mhits="$hits" mcont="$cont"
65         hits=1 cont="${l#*:}"
66       else
67         hits=1 cont="${l#*:}"
68       fi
69     done
70
71     [ $hits -gt $mhits ] \
72     && STRING "$cont" \
73     || STRING "$mcont"
74   }
75 }
76
77 if [ ! "$action" -a "$LANGUAGE_DEFAULT" ]; then
78   action="./:${LANGUAGE}/[search]"
79 elif [ ! "$action" ]; then
80   action="./[search]"
81 fi
82
83 if [ "${show_form}" = true ]; then
84   cat <<-EOF
85         <form class="macro search" method="GET" action="${action}">
86           <input type="search" placeholder="$(_ Search)" name="q"
87                  value="$([ "$show_hits" = true ] && printf %s\\n "$query" |HTML)"
88           ><button type="submit" class="search">$(_ Search)</button>
89         </form>
90         EOF
91 fi
92
93 if [ "${show_hits}" = true ]; then
94   { read tags; read ntags; read words; } <<-EOF
95         $(printf %s\\n "${query}" | awk '
96         BEGIN { # Field separator FS should include punctuation, including Unicode Block U+2000 - U+206F
97                 if ( length("¡") == 1 )  # Utf-8 aware AWK
98                 FS = "([] \\t\\n\\r\"'\''()*+,./:;<=>?\\\\^`{|}~[-]|%[0-9A-Fa-f]{2}|'"$(printf '[\342\200\200-\342\201\257]')"')+";
99                 else                     # UTF-8 Hack
100                 FS = "([] \\t\\n\\r\"'\''()*+,./:;<=>?\\\\^`{|}~[-]|%[0-9A-Fa-f]{2}|'"$(printf '\342\200[\200-\277]|\342\201[\201-\257]')"')+";
101                 fi
102               }
103         { t=0; for (n = 1; n <= NF; n++) if ($n  ~ /#[[:alnum:]_]+/)  tags[t++] = toupper($n);
104           t=0; for (n = 1; n <= NF; n++) if ($n  ~ /![[:alnum:]_]+/) ntags[t++] = toupper($n);
105           t=0; for (n = 1; n <= NF; n++) if ($n !~ /![[:alnum:]_]+/) words[t++] = tolower($n);
106           for (t in  tags) { sub(/^#/, "",  tags[t]); printf "%s        ",  tags[t]; } print "";
107           for (t in ntags) { sub(/^!/, "", ntags[t]); printf "%s        ", ntags[t]; } print "";
108           for (t in words) { gsub(/[!#_ ]+/, "  ", words[t]); printf "%s        ", words[t]; } print "";
109         }
110         ')
111         EOF
112
113   printf '<ul class="macro search hits">'
114   for w in ${words}; do
115     [ ! -f "$I/$w" ] && continue
116   
117     while read date doc freq num total; do
118       P="$_DATA/pages$(UNSTRING "$doc")"
119       d="$(stat -c %Y -- "$P/#index.flag" 2>&-)"
120       [ "$d" -le "$date" -a -f "$P/#page.md" ] 2>&- || continue
121   
122       printf '%s        %f\n' "$doc" "$freq"
123     done <"$I/$w"
124   done \
125   | awk '
126         { cnt[$1]++; weight[$1] = weight[$1] ? weight[$1] + $2 : $2; }
127     END { m = 0; for (d in cnt) m = ( m < cnt[d] ) ? cnt[d] : m;
128           for (d in cnt) if ( cnt[d] == m ) printf "%f  %s\n", weight[d], d;
129         }
130   ' \
131   | sort -nr \
132   | while read freq doc; do
133     page="$(UNSTRING "$doc")"
134     [ "${page%*/\[*\]/*}" != "$page" ] && continue
135     if [ "$LANGUAGE_DEFAULT" ]; then
136       [ -d "${_DATA}/pages/${page}/:${LANGUAGE}/" ] && continue
137       [ "${page%/:*/}" = "${page%/:${LANGUAGE}/}" ] || continue
138     fi
139     acl_read "$page" || continue
140     has_tags "$page" $tags || continue
141     has_tag "$page" $ntags && continue
142     printf '%s  %s\n' "$doc" "$(searchteaser "$(mdfile "$page")" $words)"
143   done \
144   | while read -r p t; do
145     path="$(UNSTRING "$p")" pfrag="${path%/}" title=''
146     while [ "$pfrag" ]; do
147       title="$(page_title "$pfrag")/$title"
148       pfrag="${pfrag%/*}"
149     done
150     printf '<li><a href="%s">%s</a><p>%s</p></li>' \
151       "$(URL "$path")" "$(HTML "/$title")" "$(UNSTRING "$t" |HTML)"
152   done
153   printf '</ul>'
154 fi