]> git.plutz.net Git - serve0/blobdiff - widgets.sh
generate tagging section from cached order
[serve0] / widgets.sh
index 26d755fb4301e3676b973cad186edfc41f13af8f..23afd9d440ba6ffbf411faaffcd628f58634403d 100755 (executable)
@@ -20,11 +20,13 @@ taglist(){
   | UNSTRING | HTML \
   | sed -E 's;&#x0A\;;\n;g; s;\n+;\n;g;'
 }
-
-tclist(){
-  printf %s "$*" \
-  | sed -rn '/:/s;^-?([^:]+):.*$;\1;p' \
-  |sort -u
+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";
@@ -33,13 +35,12 @@ if [ ! -s "$c_tags" ]; then
   mv "$c_tags.$$" "$c_tags"
 fi
 w_tags="$(cat "$c_tags")"
-
-c_tagcategories="$_DATA/.index/tagcategories.cache"
-if [ ! -s "$c_tagcategories" ]; then
-  tclist "$w_tags" >"$c_tagcategories.$$"
-  mv "$c_tagcategories.$$" "$c_tagcategories"
-fi
-w_tagcategories="$(cat "$c_tagcategories")"
+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
@@ -194,7 +195,6 @@ w_advsearch(){
 
       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
@@ -230,7 +230,7 @@ w_delete(){
 }
 
 w_tagging(){
-  local tag category d
+  local tctag oldcat category tag
 
   cat <<-EOF
        [a href="#multitag" Add Tags / Remove Tags]
@@ -238,20 +238,26 @@ 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 ']]'
+  oldcat="Tags"
+  printf '[fieldset [legend %s:][div .tagselect\n' "Tags"
+  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
+      printf ']]'
+      printf '[fieldset [legend %s:][div .tagselect\n' "$category"
+    fi
+    printf '[label [checkbox "tag" "%s"] %s]\n' "$tctag" "$tag"
+
+    oldcat="$category"
   done
+  printf ']]'
 
   cat <<-EOF
          [fieldset [legend New:][textarea name=newtag\n]