]> git.plutz.net Git - serve0/blobdiff - list.sh
altered groupmatch algorithm
[serve0] / list.sh
diff --git a/list.sh b/list.sh
index 18799f3ec3af2f77442224ca7191d62aeca1d88f..a4bdbc797490d31e68700e559476dcc8d02e4319 100644 (file)
--- a/list.sh
+++ b/list.sh
@@ -4,39 +4,47 @@
 . "$_EXEC/widgets.sh"
 
 list_item() {
-  local name link path length width height tags comment n
-  name="$(HTML "$1")"
-  link="$(URL "$ITEM/$1")"
-  path="$(HTML "$ITEM/$1")"
-  qry=$(HTML "$QUERY_STRING")
-
-  if [ -d "$_DATA/$ITEM/$1" ]; then
-    printf '[a .list .dir href="%s" %s]' "${path}?${qry}" "$name"
-  elif [ -f "$_DATA/$ITEM/$1" ]; then
-    read -r length width height tags comment n <<-EOF
-       $(meta_info "$_DATA/$ITEM/$1")
-       EOF
+  local meta type length width height tags comment name display link
+  meta="${1}"; type="${meta%%  *}"; meta="${meta#*     }"
+
+  if [ "$type" = dir ]; then
+    name="${meta%%     *}";
+    display="$(HTML "$name")"; link="$(URL "$ITEM/$name")"
+    printf '[a .list .dir href="%s" %s]' "${link}?${w_refuri#*\?}" "$name"
+    return 0
+  fi
+
+  length="${meta%%     *}"; meta="${meta#*     }"
+  width="${meta%%      *}"; meta="${meta#*     }"
+  height="${meta%%     *}"; meta="${meta#*     }"
+  tags="${meta%%       *}"; meta="${meta#*     }"
+  comment="${meta%%    *}"; meta="${meta#*     }"
+  name="${meta%%       *}"; meta="${meta#*     }"
+
+  if [ "$type" = metashort ]; then
+    name="$(list_fullname "$(UNSTRING "${name%${CR}}")")"
+  fi
+  if [ -f "$_DATA/$ITEM/$name" ]; then
+    link="$(URL "$ITEM/$name")"
+    name="$(HTML "$ITEM/$name")"
     printf '[div .list .file
               [a href="%s" [img src="%s?a=thumbnail"]][label %s]
-              [span .time %i:%imin] [span .dim %ix%i] %s
+              [span .time %i:%02imin] [span .dim %ix%i] %s
               [checkbox "select" "%s" id="select_%s"][label for="select_%s" +]
             ]' \
-      "$link" "$link" "$name" \
+      "$link" "$link" "${name##*&#47;}" \
       "$((length / 60))" "$((length % 60))" \
       "$width" "$height" \
-      "$(UNSTRING "${tags#tags=}" |tr , '\0' |xargs -r0 printf ' [span .tag %s]')" \
-      "$path" "$path" "$path"
+      "$(printf '%s\n' "${tags#tags=}" \
+         | sed -r "$UNSTRING"' s;^;,;; s;,+;,;g; s;,$;;;
+                   :X s;,-?([^,]+)(,|$); [span .tag\n \1]\2;; tX;'
+      )" "$name" "$link" "$link"
   else
-    printf 'Canning record for nonexist file: %s\n' "$1" >&2
-    meta_purge "$_DATA/$ITEM/$1"
+    printf 'Canning record for nonexist file: %s\n' "$name" >&2
+    meta_purge "$_DATA/$ITEM/$name"
   fi
 }
 
-list_directories(){
-  (cd "$_DATA/$ITEM";
-    find ./ -type d \! -name .index -mindepth 1 -maxdepth 1 \
-  ) | cut -d/ -f2- | sort
-}
 
 [ "$FILTER" ] && list_fex="$(
   fex='p'
@@ -61,88 +69,142 @@ list_fullname(){
   && printf '%s\n' "${base}/${file}"
 }
 
-list_filemeta(){
-  local meta base f fn file
-  base="$1"
-  meta="$_DATA/$ITEM/$base/.index/meta"
-  meta_dir "$_DATA/$ITEM/$base"
-
-  if [ $ORDER = Name ]; then
-    sort -k6 "$meta"
-  elif [ $ORDER = Length ]; then
-    sort -n -k1 "$meta"
-  else
-    cat "$meta"
-  fi \
-  | if [ "$FILTER" ]; then
+list_filter(){
+  if [ "$FILTER" ]; then
     sed -nr "$list_fex"
   elif [ "${SEARCH#!}" != "${SEARCH}" ]; then
-    grep -aviE "$(STRING "${SEARCH#!}" |sed -r ':x s;((^|[^\\])(\\\\)*)\+;\1\\+;g; tx;')"
-  else
-    grep -aiE "$(STRING "${SEARCH}" \
+    grep -aviEe "$(STRING "${SEARCH}" \
+                 | sed -r ':x s;((^|[^\\])(\\\\)*)\+;\1 ;g; tx;
+                            s;((^|[^\\])(\\\\)*)\\\+;\1+;g;
+                            s; ;\\+;g;')"
+  elif [ "${SEARCH}" ]; then
+    grep -aiEe "$(STRING "${SEARCH}" \
                  | sed -r ':x s;((^|[^\\])(\\\\)*)\+;\1 ;g; tx;
                             s;((^|[^\\])(\\\\)*)\\\+;\1+;g;
                             s; ;\\+;g;')"
-  fi |cut -f6 | UNSTRING \
-  | while read -r fn; do
-    printf '%s\n' "${base}/${fn%${CR}}"
-  done | tee .index/debug
+  else
+    cat
+  fi
 }
 
-list_index(){
-  local meta
-  (cd "$_DATA/$ITEM";
-    find ./ -path '*/.index/meta'
-  ) | while read -r meta; do
-    list_filemeta "${meta%/.index/meta}"
-  done
+groupmatch(){
+  a="${1%$CR}" b="${2%$CR}"
+#  if [ ${#a} -gt ${#b} ]; then
+#    long="$a" short="$b"
+#  else
+#    long="$b" short="$a"
+#  fi
+  # com="$(expr substr "$long" 1 $((${#long} * 3 / 4)))"
+  # com="$(expr substr "$long" 1 $((${#long} - 3)))"
+  ## Cut off youtube- and other site suffixes. Cut of last three characters
+  com="$(printf %s "$a" |sed -E 's;-([0-9a-zA-Z_-]{11}|ph[0-9a-f]{13}|[0-9]{8})$;;; s;...$;;;')"
+  cut="${b#$com}"
+  if [ "${cut}" != "${b}" ]; then
+    return 0
+  else
+    return 1
+  fi
 }
 
-list_dateorder(){
-  while read -r sn; do 
-    list_fullname "$sn"
-  done \
-  | xargs -rd'\n' stat -c '%Y  %n' \
-  | sort -rn |cut -d/ -f2- \
-  | sed -r 's;\.[^\.]*$;;;'
+list_order(){
+  local fm fn fn al length ln h w t c name buffer l
+
+  if [ $ORDER = Name ]; then
+    sort -k6 |sed 's;^;metashort\t;;'
+  elif [ $ORDER = Group ]; then
+    { sort -k6; echo '0 0 0 tags= comment= _'; } \
+    | while read -r length h w t c name; do
+      if groupmatch "$ln" "$name"; then
+        al=$((al + length))
+        buffer="${buffer}${BR}$length  $h      $w      $t      $c      $name"
+      else
+       printf %s\\n "$buffer" |while read -r l; do
+          [ "$l" ] && printf '%s       %s\n' "$al" "$l"
+        done
+        al="$length"
+        buffer="$length        $h      $w      $t      $c      $name"
+      fi
+      ln="$name"
+    done \
+    | sort -sn -k1 |sed -r 's;^[0-9]+\t;metashort\t;;'
+  elif [ $ORDER = Length ]; then
+    sort -sn -k1 |sed 's;^;metashort\t;;'
+  elif [ $ORDER = Date ]; then
+    while read -r fm; do
+      sn="${fm##*      }"
+      fn="$(list_fullname "$(UNSTRING "${sn%${CR}}")")"
+      printf '%i       %s      %s\n' \
+             "$(stat -c %Y "$fn")" "${fm%      *}" "$fn"
+    done \
+    | sort -srn -k1 |sed -r 's;^[0-9]+\t;metalong\t;;'
+  fi
 }
 
-list_items() {
-  local mode
-  mode="$(COOKIE mode |grep -m1 -axE 'index|browse' || printf browse )"
+list_filemeta(){
+  local meta base cbase fm cachename
+  base="$1"
+  meta="$_DATA/$ITEM/$base/.index/meta"
+  meta_dir "$_DATA/$ITEM/$base"
 
-  [ "$mode" = browse -a "$ITEM" ] && printf '..\n'
+  cachename="$(printf '%s\n' "$mode" "$FILTER" "$SEARCH" "$ORDER" |sha1sum)"
+  cachename="$_DATA/$ITEM/.index/${cachename%  -}.cache"
 
-  if   [ "$mode" = browse -a "$ORDER" = Date ]; then
-    list_directories
-    list_filemeta . |list_dateorder
-  elif [ "$mode" = index  -a "$ORDER" = Date ]; then
-    list_index |list_dateorder
-  elif [ "$mode" = browse ]; then
-    list_directories
+  if [ "$cachename" -nt "$meta" ] 2>&-; then
+    cat "$cachename"
+  else
+    cbase="$(STRING "$base")"
+    grep -axE '[0-9]+  [0-9]+  [0-9]+  tags=[^ ]*      comment=[^      ]*      .+' "$meta" \
+    | while read -r fm; do
+      printf '%s       %s/%s\n' "${fm% *}" "$cbase" "${fm##*   }"
+    done \
+    | list_filter \
+    | list_order \
+    | { [ -d "${meta%meta}" ] && tee "$cachename" || cat; }
+  fi
+}
+
+list_items() {
+  local mode meta
+  mode="$(COOKIE mode |grep -m1 -axE 'index|browse' || printf browse )"
+  
+  if [ "$mode" = browse ]; then
+    [ "$ITEM" ] && printf 'dir\t..\n'
+    (cd "$_DATA/$ITEM";
+      find ./ -type d \! -name .index -mindepth 1 -maxdepth 1 \
+    ) | cut -d/ -f2- | sort |sed 's;^;dir\t;;'
     list_filemeta .
   elif [ "$mode" = index ]; then
-    list_index
+    (cd "$_DATA/$ITEM";
+      find ./ -path '*/.index/meta'
+    ) | while read -r meta; do
+      list_filemeta "${meta%/.index/meta}"
+    done
   fi
 }
 
 list_paginate() {
-  local page i c n end
+  local page i c n end qry
   page="$(GET p |grep -axE '[0-9]+' || printf 1)"; c=1
   end=$((page + LISTSIZE))
+  qry="${w_refuri#*\?}"; qry="${qry#&#112;&#61;*&#38;}"
 
   printf '[div .itemlist '
   while read -r i; do
-    [ $c -ge $page -a $c -lt $end  ] \
-    && list_item "$(list_fullname "$i")"
     c=$((c + 1))
+    [ $c -gt $page -a $c -le $end  ] && list_item "$i"
   done
   printf ']'
 
+  [ $(( c % LISTSIZE )) -gt 0 ] \
+  && end=$((c / LISTSIZE + 1)) \
+  || end=$((c / LISTSIZE))
+
   printf '[div .pagination'
-  for n in $( seq 1 $((c / LISTSIZE + 1)) ); do
-    printf '[a .page href="%s" %s]' \
-      "?p=$(( (n - 1) * LISTSIZE + 1))&$qry" "$n"
+  for n in $( seq 1 $end ); do
+    c=$(( (n - 1) * LISTSIZE + 1 ))
+    [ $c = $page ] \
+    && printf '[a .page .current href="%s" %s]' "?p=${c}&${qry}" "$n" \
+    || printf '[a .page href="%s" %s]' "?p=${c}&${qry}" "$n"
   done
   printf ']'
 }
@@ -151,7 +213,10 @@ printf 'Content-Type: text/html;charset=utf-8\r\n\r\n'
 
 { printf '
 [!DOCTYPE HTML]
-[html [head [title Listing]
+[html [head [title '
+  w_bmname
+  printf ' by %s]' "$ORDER"
+  printf '
   [meta name="viewport" content="width=device-width"]
   [link rel=stylesheet href="/style.css" ]
 ] [body
@@ -169,13 +234,18 @@ printf 'Content-Type: text/html;charset=utf-8\r\n\r\n'
   [form method=POST action="?a=multitag"'
     list_items \
     | list_paginate
-    printf '
-    [div #editing
-      [a href="#multitag" Add Tags]'
+    [ -d "$_DATA/$ITEM/.index" ] && { printf '
+    [div #editing'
       w_tagging
-      w_index
-      printf '
-    ]
-  ]
+    printf '
+    ]'; }
+  printf '
+  ]'
+  [ ! -d "$_DATA/$ITEM/.index" ] && { printf '
+  [div #editing'
+    w_index
+  printf '
+  ]'; }
+  printf '
 ] ]
 '; } | "$_EXEC/cgilite/html-sh.sed"