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