]> git.plutz.net Git - serve0/blobdiff - widgets.sh
generate advfilter section from cached tag order
[serve0] / widgets.sh
index 4d42c41d0bc73a0cd65f164033f5d295e10cddcd..84b656774c3ead4b1f2c2a5334e67f496e7b6eb4 100755 (executable)
@@ -11,27 +11,36 @@ w_refuri="$(URL "$PATH_INFO")?$(HTML "$QUERY_STRING")"
 w_str_s="$(STRING "$SEARCH")"
 w_str_f="$(STRING "$FILTER")"
 
-c_tags="$_DATA/.index/tags.cache"; c_tagcategories="$_DATA/.index/tagcategories.cache"
-if [ ! -s "$c_tags" -o ! -s "$c_tagcategories" ] \
-   || [ "$(find "$_DATA/" -path '*/.index/meta' -newer "$c_tags")" ]
-  then
-  w_tags="$(
-    list_meta |sed -E '
-      s;^.*\ttags=([^\t]*)\t.*$;\1;;
-      s;,;\n;g;
-    ' \
-    | sort |uniq -c |sort -rn |sed -E 's;^ *[0-9]+ ;;;' \
-    | UNSTRING | HTML \
-    | sed -E 's;&#x0A\;;\n;g; s;\n+;\n;g;'
-  )"
-  w_tagcategories="$(printf %s "$w_tags" | sed -rn '/:/s;^-?([^:]+):.*$;\1;p' |sort -u )"
-  printf %s "$w_tags" >"$c_tags"
-  printf %s "$w_tagcategories" >"$c_tagcategories"
-else
-  w_tags="$(cat "$c_tags")"
-  w_tagcategories="$(cat "$c_tagcategories")"
-fi
+taglist(){
+  list_meta |sed -E '
+    s;^.*\ttags=([^\t]*)\t.*$;\1;;
+    s;,;\n;g;
+  ' \
+  | sort |uniq -c |sort -rn |sed -E 's;^ *[0-9]+ ;;;' \
+  | UNSTRING | HTML \
+  | sed -E 's;&#x0A\;;\n;g; s;\n+;\n;g;'
+}
+tagorder(){
+  printf '*\n%s\n$\n' "$w_tagcategories" \
+  | while read -r category; do
+    printf '%s\n' "$w_tags" \
+    | { [ "$category" = '*' ] && grep -avF ':' || grep -awF "${category}"; } \
+    | { sed -u 10q; sort; }
+  done
+}
 
+c_tags="$_DATA/.index/tags.cache";
+if [ ! -s "$c_tags" ]; then
+  taglist >"$c_tags.$$"
+  mv "$c_tags.$$" "$c_tags"
+fi
+w_tags="$(cat "$c_tags")"
+w_tagcategories="$(
+  printf %s "$w_tags" \
+  | sed -rn '/:/s;^-?([^:]+):.*$;\1;p' \
+  |sort -u
+)"
+w_tags="$(tagorder)"
 
 [ "$ORDER" = Name   ] && w_coname=checked
 [ "$ORDER" = Date   ] && w_codate=checked
@@ -114,7 +123,7 @@ w_search(){
 w_prefs(){
   local tm tf td
 
-  tm=''; [ "$(COOKIE mode)" = index ] && tm=' '
+  tm=''; [ "$(COOKIE mode)" = browse ] && tm=' '
   tf=''; [ "$(COOKIE fakemp4)" = yes ] && tf=checked
   td=''; [ "$(COOKIE downscale)" = yes ] && td=checked
 
@@ -124,8 +133,8 @@ w_prefs(){
          [hidden "ref" "${w_refuri}"]
          [label for=prefs_ps Pagesize]
          [input #prefs_ps type=number name=pagesize value="${LISTSIZE}"][br]
-         [radio "mode" "browse" ${tm:-checked=checked} #prefs_modebrowse] [label for=prefs_modebrowse Browse Folders][br]
-         [radio "mode" "index" ${tm:+checked=checked} #prefs_modeindex ] [label for=prefs_modeindex View Full Index][br]
+         [radio "mode" "browse" ${tm:+checked=checked} #prefs_modebrowse] [label for=prefs_modebrowse Browse Folders][br]
+         [radio "mode" "index"  ${tm:-checked=checked} #prefs_modeindex ] [label for=prefs_modeindex View Full Index][br]
          [checkbox "fakemp4" "yes" ${tf} #prefs_fmp4] [label for=prefs_fmp4 Fake .MP4 file type][br]
          [checkbox "downscale" "yes" ${td} #prefs_downscale] [label for=prefs_downscale Prefer downscale to 480p][br]
          [submit "index" "update" Force Index Update][br]
@@ -171,32 +180,39 @@ w_advsearch(){
        EOF
 
     f="|${f#\~}|"
-    printf '*\n%s\n$\n' "$w_tagcategories" \
-    | while read -r category; do
-      lbid="cat_${n}_${category}"
-
-      t=''
-      [ "$category"  = '*' -a   "${f%%|${category}:*}" ] && t=checked
-      [ "$category" != '*' -a ! "${f%%|${category}:*}" ] && t=checked
-      [ "$category" != '*' -a ! "${f%%|-${category}:*}" ] && t=checked
-
-      printf '[radio "cat_%i" "%s" .cat %s id="%s"][label for="%s" %s]
-              [div .catselect\n' \
-              $n "$category" "$t" "$lbid" "$lbid" "$category"
-
-      printf '%s\n' "$w_tags" \
-      | { [ "$category" = '*' ] && grep -avF ':' || grep -awF "${category}"; } \
-      | { sed -u 10q; sort; } \
-      | while read -r tag; do
-        [ "$tag" ] || continue
-        t=''; [ ! "${f%%*|${tag}|*}" ] && t=checked
-        d="${tag#-}"; d="${d#*:}"
-       printf '[label [checkbox "tag_%s" "%s" %s] %s]' "$n" "$tag" "$t" "$d"
-      done
-      d="${f##*\$:}" d="${d%%\|*}"
-      [ "$category" = \$ ] && printf '[input name="tag_%i" value="%s"]' "$n" "$(HTML "$d")"
-      printf '\n]'
+    printf '%s\n$:\n' "$w_tags" \
+    | while read tctag; do
+      [ "$tctag" ] || continue
+      category="${tctag#-}"; category="${category%%:*}"
+      tag="${tctag#-}"; tag="${tag#*:}"
+      [ "${tag}" = "${tctag#-}" ] \
+      && category="*"
+  
+      if [ "$category" != "$oldcat" ]; then
+        [ "$oldcat" ] && printf '\n]'
+        lbid="cat_${n}_${category}"
+
+        t=''
+        [ "$category"  = '*' -a   "${f%%|${category}:*}" ] && t=checked
+        [ "$category" != '*' -a ! "${f%%|${category}:*}" ] && t=checked
+        [ "$category" != '*' -a ! "${f%%|-${category}:*}" ] && t=checked
+
+        printf '[radio "cat_%i" "%s" .cat %s id="%s"][label for="%s" %s]
+                [div .catselect\n' \
+                $n "$category" "$t" "$lbid" "$lbid" "$category"
+      fi
+
+      if [ "$category" = '$' ]; then
+        tag="${f##*\$:}" tag="${tag%%\|*}"
+        printf '[input name="tag_%i" value="%s"]' "$n" "$(HTML "$tag")"
+      else
+        t=''; [ ! "${f%%*|${tctag}|*}" ] && t=checked
+        printf '[label [checkbox "tag_%s" "%s" %s] %s]' "$n" "$tctag" "$t" "$tag"
+      fi
+
+      oldcat="$category"
     done
+    [ "$w_tags" ] && printf '\n]'
     printf ']'
   done
 
@@ -222,7 +238,7 @@ w_delete(){
 }
 
 w_tagging(){
-  local tag category d
+  local tctag oldcat="" category tag
 
   cat <<-EOF
        [a href="#multitag" Add Tags / Remove Tags]
@@ -230,20 +246,24 @@ w_tagging(){
          [a href="#" X]
        EOF
 
-  printf 'Tags\n%s\n' "$w_tagcategories" \
-  | while read -r category; do
-    [ "$category" ] || continue
-    printf '[fieldset [legend %s:][div .tagselect\n' "$category"
-    printf %s "$w_tags" \
-    | { [ "$category" = 'Tags' ] && grep -avF ':' || grep -awF "${category}"; } \
-    | { sed -u 10q; sort; } \
-    | while read -r tag; do
-      [ "$tag" ] || continue
-      d="${tag#-}"; d="${d#*:}"
-      printf '[label [checkbox "tag" "%s"] %s]\n' "$tag" "$d"
-    done
-    printf ']]'
+  printf '%s\n' "$w_tags" \
+  | while read tctag; do
+    [ "$tctag" ] || continue
+    category="${tctag#-}"; category="${category%%:*}"
+    tag="${tctag#-}"; tag="${tag#*:}"
+
+    [ "${tag}" = "${tctag#-}" ] \
+    && category="Tags"
+
+    if [ "$category" != "$oldcat" ]; then
+      [ "$oldcat" ] && printf ']]'
+      printf '[fieldset [legend %s:][div .tagselect\n' "$category"
+    fi
+    printf '[label [checkbox "tag" "%s"] %s]\n' "$tctag" "$tag"
+
+    oldcat="$category"
   done
+  [ "$w_tags" ] && printf ']]'
 
   cat <<-EOF
          [fieldset [legend New:][textarea name=newtag\n]