]> git.plutz.net Git - shellwiki/blobdiff - macros/search
Merge commit '6bc502434737d7f08379e79b94fc6fda424ef779'
[shellwiki] / macros / search
index b3d06e85a86fbc33274b48b298b4af9b926bd48b..e8207c2eb4c46b2c41702fb905ab9aa5c07efa8b 100755 (executable)
@@ -25,22 +25,34 @@ I="$_DATA/index"
 _(){ printf %s\\n "$*"; }
 [ "${LANGUAGE}" -a -r "${_EXEC}/l10n/${LANGUAGE}.sh" ] && . "${_EXEC}/l10n/${LANGUAGE}.sh"
 
-show_form=true show_hits='' action=''
+show_form=true show_hits='' action='' query='' show_query='' hlevel='3' alt=''
 
 set -- "$@" --
 while [ $# -gt 0 ]; do case $1 in
   --no-form|--noform)
     show_form=''; shift 1;;
   --hits|--results)
+    [ ! "$show_query" ] && show_query=true
     show_hits='true'; shift 1;;
+  --query|--show-query)
+    show_query=true; shift 1;;
+  --no-query)
+    show_query=false; shift 1;;
   --action)
     action="$(HTML "$2")"; shift 2;;
   --action=*|action=*)
-    action="$(HTML "${2#*=}")"; shift 1;;
+    action="$(HTML "${1#*=}")"; shift 1;;
+  --h1|--h2|--h3|--h4|--h5|--h6)
+    hlevel="${1#--h}"; shift 1;;
+  --alt)
+    alt="$2"; shift 2;;
   --) shift 1; break;;
   *) set -- "$@" "$1"; shift 1;;
 esac; done
 
+[ $# -gt 0 ] && query="$*" || query="$(GET q)"
+[ ! "$show_query" ] && show_query=false
+
 searchteaser() {
   local file="$1" words db3_data
   local w l nc nl hits mhits cont mcont
@@ -72,26 +84,30 @@ searchteaser() {
   }
 }
 
-if [ ! "$action" -a "$LANGUAGE_DEFAULT" ]; then
+if [ ! "$action" -a "$LANGUAGE" != "$LANGUAGE_DEFAULT" ]; then
   action="./:${LANGUAGE}/[search]"
 elif [ ! "$action" ]; then
   action="./[search]"
 fi
 
 if [ "${show_form}" = true ]; then
-  printf '<form class="macro search" method="GET" action="%s">
-  <input type="search" placeholder="%s" name="q" value="%s"><button type="submit" class="search">%s</button>
-</form>' "$action" "$(_ Search)" "$([ "$show_hits" = true ] && GET q |HTML)" "$(_ Search)"
+  cat <<-EOF
+       <form class="macro search" method="GET" action="${action}">
+         <input type="search" placeholder="$(_ Search)" name="q"
+                value="$([ "$show_query" = true ] && printf %s\\n "$query" |HTML)"
+         ><button type="submit" class="search">$(_ Search)</button>
+       </form>
+       EOF
 fi
 
 if [ "${show_hits}" = true ]; then
   { read tags; read ntags; read words; } <<-EOF
-       $(GET q | awk '
+       $(printf %s\\n "${query}" | 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]')"')+";
+               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]')"')+";
+               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);
@@ -99,12 +115,11 @@ if [ "${show_hits}" = true ]; then
          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 "";
+         for (t in words) { gsub(/[!#_ ]+/, "  ", words[t]); printf "%s        ", words[t]; } print "";
        }
        ')
        EOF
 
-  printf '<ul class="macro search hits">'
   for w in ${words}; do
     [ ! -f "$I/$w" ] && continue
   
@@ -135,14 +150,16 @@ if [ "${show_hits}" = true ]; then
     has_tag "$page" $ntags && continue
     printf '%s %s\n' "$doc" "$(searchteaser "$(mdfile "$page")" $words)"
   done \
-  | while read -r p t; do
-    path="$(UNSTRING "$p")" pfrag="${path%/}" title=''
-    while [ "$pfrag" ]; do
-      title="$(page_title "$pfrag")/$title"
-      pfrag="${pfrag%/*}"
-    done
-    printf '<li><a href="%s">%s</a><p>%s</p></li>' \
-      "$(URL "$path")" "$(HTML "/$title")" "$(UNSTRING "$t" |HTML)"
-  done
-  printf '</ul>'
+  | { while read -r p t; do
+      [ ! "$path" ] && printf '<ul class="macro search hits">'
+      path="$(UNSTRING "$p")" title="$(page_title "$path")"
+      path="$(HTML "$path")"
+      printf '<li><h%i><a href="%s">%s</a></h%i><p class="path">%s</p><p class="teaser">%s</p></li>' \
+             "${hlevel}" "$path" "$(HTML "$title")" "${hlevel}" \
+             "$path" "$(UNSTRING "$t" |HTML)"
+      done
+    [ "$path" ] && printf '</ul>'
+    [ ! "$path" -a "$alt" -a "$query" ] \
+    && printf '<p class="macro search hits">%s</p>' "$(HTML "$alt")"
+  }
 fi