]> git.plutz.net Git - shellwiki/blobdiff - handlers/40_search.sh
fancier search results with teasers and page titles
[shellwiki] / handlers / 40_search.sh
index 90840603cd6ec7e1881b197f92994d2bbd4e3882..a4d91a82a8fa5e0f3861bf48f93ce6ac6fb1060b 100644 (file)
@@ -18,6 +18,7 @@
 [ "${PATH_INFO%\[search\]}" = "$PATH_INFO" ] && return 1
 
 . "$_EXEC/cgilite/storage.sh"
+. "$_EXEC/db23.sh"
 
 I="$_DATA/index"
 words="$( GET q | awk '
@@ -31,6 +32,37 @@ words="$( GET q | awk '
        { for (n = 1; n <= NF; n++) printf "%s  ", tolower($n); }
 ')"
 
+searchteaser() {
+  local file="$1" words db3_data
+  local w l nc nl hits mhits cont mcont
+  shift 1; words="$*"
+
+  for w in ${words}; do
+    grep -hiwnF "$w" "$file"
+  done \
+  | sort -t: -k1 -n \
+  | { nc=-1 hits=0 mhits=0
+    while read -r l; do
+      nl="$nc" nc="${l%%:*}"
+      if [ $nc -eq $nl ]; then
+        hits=$((hits + 1))
+      elif [ $nc -eq $((nl + 1 )) ]; then
+        hits=$((hits + 1))
+        cont="${cont}${BR}${l#*:}"
+      elif [ $hits -gt $mhits ]; then
+        mhits="$hits" mcont="$cont"
+        hits=1 cont="${l#*:}"
+      else
+        hits=1 cont="${l#*:}"
+      fi
+    done
+
+    [ $hits -gt $mhits ] \
+    && STRING "$cont" \
+    || STRING "$mcont"
+  }
+}
+
 for w in ${words}; do
   [ ! -f "$I/$w" ] && continue
 
@@ -57,6 +89,6 @@ done \
     [ "${page%/:*/}" = "${page%/:${LANGUAGE}/}" ] || continue
   fi
   acl_read "$page" || continue
-  printf '%s\n' "$page"
+  printf '%s   %s\n' "$doc" "$(searchteaser "$(mdfile "$page")" $words)"
 done \
 | theme_search "${words%       }"