From b45089e38287dd6da2cab48069cae40805f98da3 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Paul=20H=C3=A4nsch?= Date: Fri, 22 Sep 2023 10:07:31 +0200 Subject: [PATCH] search handler --- handlers/40_search.sh | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 handlers/40_search.sh diff --git a/handlers/40_search.sh b/handlers/40_search.sh new file mode 100644 index 0000000..3db8987 --- /dev/null +++ b/handlers/40_search.sh @@ -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 '
  • %s
  • ' "$(URL "$page")" "$(HTML "$page")" +done \ +| theme_page - <<-EOF +
    +

    $(_ "Search results:")

    +
      + $(cat) +
    +
    + EOF -- 2.39.2