From: Paul Hänsch Date: Sun, 7 Apr 2024 13:12:55 +0000 (+0200) Subject: define search function as macro X-Git-Url: https://git.plutz.net/?p=shellwiki;a=commitdiff_plain;h=20c6ce6a3de621383d1b6d985757366590722005 define search function as macro --- diff --git a/handlers/40_search.sh b/handlers/40_search.sh index 57117e3..3661f95 100644 --- a/handlers/40_search.sh +++ b/handlers/40_search.sh @@ -17,114 +17,5 @@ [ "$SEARCH_INDEX" != true ] && return 1 [ "${PATH_INFO%\[search\]}" = "$PATH_INFO" ] && return 1 -. "$_EXEC/cgilite/storage.sh" -. "$_EXEC/cgilite/db23.sh" - -I="$_DATA/index" -tags="$( GET q | awk ' - BEGIN { # Field separator FS should include punctuation, including Unicode Block U+2000 - U+206F - if ( length("¡") == 1 ) # Utf-8 aware AWK - FS = "([] \\t\\n\\r!\"'\''()*+,./:;<=>?\\\\^_`{|}~[-]|%[0-9A-Fa-f]{2}|'"$(printf '[\342\200\200-\342\201\257]')"')+"; - else # UTF-8 Hack - FS = "([] \\t\\n\\r!\"'\''()*+,./:;<=>?\\\\^_`{|}~[-]|%[0-9A-Fa-f]{2}|'"$(printf '\342\200[\200-\277]|\342\201[\201-\257]')"')+"; - fi - } - { for (n = 1; n <= NF; n++) if ($n ~ /#[[:alnum:]_]+/) { - sub(/^#/,"",$n) - printf "%s ", toupper($n); - } - } -')" - -ntags="$( GET q | awk ' - BEGIN { # Field separator FS should include punctuation, including Unicode Block U+2000 - U+206F - if ( length("¡") == 1 ) # Utf-8 aware AWK - FS = "([] \\t\\n\\r\"#'\''()*+,./:;<=>?\\\\^_`{|}~[-]|%[0-9A-Fa-f]{2}|'"$(printf '[\342\200\200-\342\201\257]')"')+"; - else # UTF-8 Hack - FS = "([] \\t\\n\\r\"#'\''()*+,./:;<=>?\\\\^_`{|}~[-]|%[0-9A-Fa-f]{2}|'"$(printf '\342\200[\200-\277]|\342\201[\201-\257]')"')+"; - fi - } - { for (n = 1; n <= NF; n++) if ($n ~ /![[:alnum:]_]+/) { - sub(/^!/,"",$n) - printf "%s ", toupper($n); - } - } -')" - -words="$( GET q | awk ' - BEGIN { # Field separator FS should include punctuation, including Unicode Block U+2000 - U+206F - if ( length("¡") == 1 ) # Utf-8 aware AWK - FS = "([] \\t\\n\\r\"#'\''()*+,./:;<=>?\\\\^_`{|}~[-]|%[0-9A-Fa-f]{2}|'"$(printf '[\342\200\200-\342\201\257]')"')+"; - else # UTF-8 Hack - FS = "([] \\t\\n\\r\"#'\''()*+,./:;<=>?\\\\^_`{|}~[-]|%[0-9A-Fa-f]{2}|'"$(printf '\342\200[\200-\277]|\342\201[\201-\257]')"')+"; - fi - } - { for (n = 1; n <= NF; n++) if ($n !~ /![[:alnum:]_]+/) { - sub(/!/," ",$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 - - while read date doc freq num total; do - P="$_DATA/pages$(UNSTRING "$doc")" - d="$(stat -c %Y -- "$P/#index.flag" 2>&-)" - [ "$d" -le "$date" -a -f "$P/#page.md" ] 2>&- || 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")" - [ "${page%*/\[*\]/*}" != "$page" ] && continue - if [ "$LANGUAGE_DEFAULT" ]; then - [ -d "${_DATA}/pages/${page}/:${LANGUAGE}/" ] && continue - [ "${page%/:*/}" = "${page%/:${LANGUAGE}/}" ] || continue - fi - acl_read "$page" || continue - has_tags "$page" $tags || continue - has_tag "$page" $ntags && continue - printf '%s %s\n' "$doc" "$(searchteaser "$(mdfile "$page")" $words)" -done \ -| theme_search "${words% }" +theme_page "/[wiki]/search/" "$(page_title "/[wiki]/search/")" +return 0 diff --git a/macros/search b/macros/search new file mode 100755 index 0000000..b3d06e8 --- /dev/null +++ b/macros/search @@ -0,0 +1,148 @@ +#!/bin/sh + +# Copyright 2023, 2024 Paul Hänsch +# +# Permission to use, copy, modify, and/or distribute this software for any +# purpose with or without fee is hereby granted, provided that the above +# copyright notice and this permission notice appear in all copies. +# +# THE SOFTWARE IS PROVIDED “AS IS” AND THE AUTHOR DISCLAIMS ALL WARRANTIES +# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF +# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY +# SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES +# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN +# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR +# IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + +I="$_DATA/index" + +. "$_EXEC/cgilite/cgilite.sh" +. "$_EXEC/cgilite/storage.sh" +. "$_EXEC/cgilite/db23.sh" +. "$_EXEC/tools.sh" +. "$_EXEC/acl.sh" + +_(){ printf %s\\n "$*"; } +[ "${LANGUAGE}" -a -r "${_EXEC}/l10n/${LANGUAGE}.sh" ] && . "${_EXEC}/l10n/${LANGUAGE}.sh" + +show_form=true show_hits='' action='' + +set -- "$@" -- +while [ $# -gt 0 ]; do case $1 in + --no-form|--noform) + show_form=''; shift 1;; + --hits|--results) + show_hits='true'; shift 1;; + --action) + action="$(HTML "$2")"; shift 2;; + --action=*|action=*) + action="$(HTML "${2#*=}")"; shift 1;; + --) shift 1; break;; + *) set -- "$@" "$1"; shift 1;; +esac; done + +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" + } +} + +if [ ! "$action" -a "$LANGUAGE_DEFAULT" ]; then + action="./:${LANGUAGE}/[search]" +elif [ ! "$action" ]; then + action="./[search]" +fi + +if [ "${show_form}" = true ]; then + printf '' "$action" "$(_ Search)" "$([ "$show_hits" = true ] && GET q |HTML)" "$(_ Search)" +fi + +if [ "${show_hits}" = true ]; then + { read tags; read ntags; read words; } <<-EOF + $(GET q | awk ' + BEGIN { # Field separator FS should include punctuation, including Unicode Block U+2000 - U+206F + if ( length("¡") == 1 ) # Utf-8 aware AWK + FS = "([] \\t\\n\\r\"'\''()*+,./:;<=>?\\\\^_`{|}~[-]|%[0-9A-Fa-f]{2}|'"$(printf '[\342\200\200-\342\201\257]')"')+"; + else # UTF-8 Hack + FS = "([] \\t\\n\\r\"'\''()*+,./:;<=>?\\\\^_`{|}~[-]|%[0-9A-Fa-f]{2}|'"$(printf '\342\200[\200-\277]|\342\201[\201-\257]')"')+"; + fi + } + { t=0; for (n = 1; n <= NF; n++) if ($n ~ /#[[:alnum:]_]+/) tags[t++] = toupper($n); + t=0; for (n = 1; n <= NF; n++) if ($n ~ /![[:alnum:]_]+/) ntags[t++] = toupper($n); + t=0; for (n = 1; n <= NF; n++) if ($n !~ /![[:alnum:]_]+/) words[t++] = tolower($n); + for (t in tags) { sub(/^#/, "", tags[t]); printf "%s ", tags[t]; } print ""; + for (t in ntags) { sub(/^!/, "", ntags[t]); printf "%s ", ntags[t]; } print ""; + for (t in words) { sub(/^[!#]/, "", words[t]); printf "%s ", words[t]; } print ""; + } + ') + EOF + + printf '' +fi diff --git a/pages/[wiki]/search/#page.md b/pages/[wiki]/search/#page.md new file mode 100644 index 0000000..598bbb8 --- /dev/null +++ b/pages/[wiki]/search/#page.md @@ -0,0 +1,6 @@ +%nocache +%title Search + +Search Results +============== +<> diff --git a/pages/[wiki]/search/:de/#page.md b/pages/[wiki]/search/:de/#page.md new file mode 100644 index 0000000..0a3ae08 --- /dev/null +++ b/pages/[wiki]/search/:de/#page.md @@ -0,0 +1,6 @@ +%nocache +%title Suche + +Suchergebnisse +============== +<> diff --git a/themes/default.css b/themes/default.css index e0f7022..c7eb6dd 100644 --- a/themes/default.css +++ b/themes/default.css @@ -173,15 +173,16 @@ input.search, input[type="search"] { max-width: 80%; max-width: calc(100% - 2.5em); } -ul.searchresults, ol.searchresults { +ul.search.hits, ol.search.hits { margin-left: auto; margin-right: auto; width: 100%; max-width: 540pt; text-align: center; + list-style: none; } -.searchresults li a { +.search.hits li a { display: block; } -.searchresults li p { +.search.hits li p { display: inline-block; margin: 0 auto .5em auto; white-space: pre-line; @@ -240,7 +241,7 @@ table { width: 80%; width: calc(100% - 2.5em); } - ul.searchresults, ol.searchresults { + ul.search.hits, ol.search.hits { min-width: 50%; } } diff --git a/themes/default.sh b/themes/default.sh index d13e849..5ebbd0e 100755 --- a/themes/default.sh +++ b/themes/default.sh @@ -135,31 +135,6 @@ theme_editor(){ theme_revisions(){ theme_page "$@"; } -theme_search(){ - local words="$*" - # STDIN: [STRING page][TAB][STRING teaser] - - theme_page - "$(_ Search results): ${words}" <<-EOF -
-

$([ "$words" ] && _ "Search results" || _ "Search" )

- -
    - $(while read -r p t; do - path="$(UNSTRING "$p")" pfrag="${path%/}" title='' - while [ "$pfrag" ]; do - title="$(page_title "$pfrag")/$title" - pfrag="${pfrag%/*}" - done - printf '
  1. %s

    %s

  2. ' \ - "$(URL "$path")" "$(HTML "/$title")" "$(UNSTRING "$t" |HTML)" - done) -
-
- EOF -} - theme_attachments(){ local page="$1"