From 81b5d834ef1762196d80ff0a8b1af1ef3f89889c Mon Sep 17 00:00:00 2001 From: =?utf8?q?Paul=20H=C3=A4nsch?= Date: Wed, 7 Feb 2024 03:32:52 +0100 Subject: [PATCH] fancier search results with teasers and page titles --- handlers/40_search.sh | 34 +++++++++++++++++++++++++++++++++- themes/default.css | 8 ++++++++ themes/default.sh | 12 +++++++++--- 3 files changed, 50 insertions(+), 4 deletions(-) diff --git a/handlers/40_search.sh b/handlers/40_search.sh index 9084060..a4d91a8 100644 --- a/handlers/40_search.sh +++ b/handlers/40_search.sh @@ -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% }" diff --git a/themes/default.css b/themes/default.css index d8a5b31..323e7b4 100644 --- a/themes/default.css +++ b/themes/default.css @@ -178,6 +178,14 @@ ul.searchresults, ol.searchresults { width: 100%; max-width: 540pt; text-align: center; } +.searchresults li a { + display: block; +} +.searchresults li p { + display: inline-block; + margin: 0 auto .5em auto; + white-space: pre-line; +} table { min-width: 50%; diff --git a/themes/default.sh b/themes/default.sh index 5b2f1ed..fefde75 100755 --- a/themes/default.sh +++ b/themes/default.sh @@ -136,7 +136,7 @@ theme_revisions(){ theme_page "$@"; } theme_search(){ local words="$*" - # STDIN: read result pages line by line + # STDIN: [STRING page][TAB][STRING teaser] theme_page - "$(_ Search results): ${words}" <<-EOF
@@ -145,8 +145,14 @@ theme_search(){
    - $( while read p; do - printf '
  1. %s
  2. ' "$(URL "$p")" "$(HTML "$p")" + $(while read -r p t; do + path="$(UNSTRING "$p")" pfrag="${path%/}" title='' + while [ "$pfrag" ]; do + title="$(page_title "$pfrag")/$title" + pfrag="${pfrag%/*}" + done + printf '
  3. %s

    %s

  4. ' \ + "$(URL "$path")" "$(HTML "/$title")" "$(UNSTRING "$t" |HTML)" done)
-- 2.39.2