]> git.plutz.net Git - shellwiki/blob - parsers/40_indexer.sh
c26584aeb101a34f008b83434446c4a9692273f1
[shellwiki] / parsers / 40_indexer.sh
1 #!/bin/sh
2
3 # Copyright 2023 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 DOC="${PATH_INFO%/}/" P="$_DATA/pages${DOC}" I="$_DATA/index/"
18
19 if [ "$SEARCH_INDEX" != true ] || [ ! -d "$P" ] || \
20    [ -f "$P/#index.flag" -a ! "$P/#page.md" -nt "$P/#index.flag" ]
21 then
22   cat
23   exit 0
24 fi
25
26 . "$_EXEC/cgilite/storage.sh"
27
28 exec 3>&1
29
30 touch -d "@$_DATE" "$P/#index.flag"
31 mkdir -p "$I"
32
33 { cat; printf \\n; } \
34 | while IFS='' read -r line; do
35   printf '%s\n' "$line" >&3
36   printf '%s\n' "$line"
37 done \
38 | awk '
39   BEGIN { # Field separator FS should include punctuation, including Unicode Block U+2000 - U+206F
40           if ( length("¡") == 1 )  # Utf-8 aware AWK
41           FS = "([] \\t\\n\\r!\"#'\''()*+,./:;<=>?\\\\^_`{|}~[-]|%[0-9A-Fa-f]{2}|'"$(printf '[\342\200\200-\342\201\257]')"')+";
42           else                     # UTF-8 Hack
43           FS = "([] \\t\\n\\r!\"#'\''()*+,./:;<=>?\\\\^_`{|}~[-]|%[0-9A-Fa-f]{2}|'"$(printf '\342\200[\200-\277]|\342\201[\201-\257]')"')+";
44           fi
45         }
46         { for (n = 1; n <= NF; n++) {
47             if ( $n != "" && length($n) <= 128 ) {
48               words[tolower($n)]++; total++;
49         } } }
50     END { for (w in words) printf "%i %i %f %s\n", words[w], total, words[w] / total, w; }
51 ' \
52 | while read -r num total freq word; do
53   [ "$word" ] || continue
54   printf "%i    %s      %f      %i      %i\n" \
55          "$_DATE" "$(STRING "$DOC")" \
56          "$freq" "$num" "$total" \
57   >>"$I/$word"
58 done