]> git.plutz.net Git - shellwiki/commitdiff
search handler
authorPaul Hänsch <paul@plutz.net>
Fri, 22 Sep 2023 08:07:31 +0000 (10:07 +0200)
committerPaul Hänsch <paul@plutz.net>
Fri, 22 Sep 2023 08:07:31 +0000 (10:07 +0200)
handlers/40_search.sh [new file with mode: 0644]

diff --git a/handlers/40_search.sh b/handlers/40_search.sh
new file mode 100644 (file)
index 0000000..3db8987
--- /dev/null
@@ -0,0 +1,40 @@
+#!/bin/sh
+
+[ "${PATH_INFO%\[search\]}" = "$PATH_INFO" ] && return 1
+
+. "$_EXEC/cgilite/storage.sh"
+
+I="$_DATA/index"
+words="$(GET q |tr '] \t\n\r!\"#'\''()*+,./:;<=>?\\^_`{|}~[-' ' ')"
+
+for w in ${words}; do
+  [ ! -f "$I/$w" ] && continue
+
+  while read date doc freq num total; do
+    P="$_DATA/pages$(UNSTRING "$doc")"
+    d="$(stat -c %Y -- "$P/#page.md")"
+    [ "$d" -gt "$date" ] && continue
+
+    printf '%s %f\n' "$doc" "$freq"
+  done <"$I/$w"
+done \
+| awk '
+      { cnt[$1]++; weight[$1] = weight[$1] ? weight[$1] * $2 : $2; }
+  END { m = 0; for (d in cnt) m = ( m < cnt[d] ) ? cnt[d] : m;
+        for (d in cnt) if ( cnt[d] == m ) printf "%f   %s\n", weight[d], d;
+      }
+' \
+| sort -nr \
+| while read freq doc; do
+  page="$(UNSTRING "$doc")"
+  acl_read "$page" || continue
+  printf '<li><a href="%s">%s</a></li>' "$(URL "$page")" "$(HTML "$page")"
+done \
+| theme_page - <<-EOF
+       <article>
+         <h1>$(_ "Search results:")</h1>
+         <ol class="searchresults">
+           $(cat)
+         </ol>
+       </article>
+       EOF