]> git.plutz.net Git - serve0/blob - widgets.sh
speed up tag genration
[serve0] / widgets.sh
1 #!/bin/sh
2
3 [ -n "$include_widgets" ] && return 0
4 include_widgets="$0"
5
6 . "$_EXEC/cgilite/storage.sh"
7
8 w_refuri="$(URL "$PATH_INFO")?$(HTML "$QUERY_STRING")"
9
10 w_ascii='
11   s-0-0-g; s-1-1-g; s-2-2-g; s-3-3-g; s-4-4-g; s-5-5-g;
12   s-6-6-g; s-7-7-g; s-8-8-g; s-9-9-g;
13   s-A-A-g; s-B-B-g; s-C-C-g; s-D-D-g; s-E-E-g; s-F-F-g;
14   s-G-G-g; s-H-H-g; s-I-I-g; s-J-J-g; s-K-K-g; s-L-L-g;
15   s-M-M-g; s-N-N-g; s-O-O-g; s-P-P-g; s-Q-Q-g; s-R-R-g;
16   s-S-S-g; s-T-T-g; s-U-U-g; s-V-V-g; s-W-W-g; s-X-X-g;
17   s-Y-Y-g; s-Z-Z-g;
18   s-a-a-g; s-b-b-g; s-c-c-g; s-d-d-g; s-e-e-g; s-f-f-g;
19   s-g-g-g; s-h-h-g; s-i-i-g; s-j-j-g; s-k-k-g; s-l-l-g;
20   s-m-m-g; s-n-n-g; s-o-o-g; s-p-p-g; s-q-q-g; s-r-r-g;
21   s-s-s-g; s-t-t-g; s-u-u-g; s-v-v-g; s-w-w-g; s-x-x-g;
22   s-y-y-g; s-z-z-g;
23   s---\--g; s-/-/-g; s-/-/-g; s-:-:-g; s-=-=-g; s-@-@-g;
24   s-_-_-g; s-~-~-g;              s- - -g; s-^-^-g; s-|-|-g;
25 '
26
27 w_tags="$_DATA/.index/tags.cache"; w_tagcategories="$_DATA/.index/tagcategories.cache"
28 if [ ! -f "$w_tags" -o ! -f "$w_tagcategories" ] \
29    || [ "$(find "$_DATA/" -path '*/.index/meta' -newer "$w_tags" -quit)" ]
30   then
31   w_tags="$( { local cn=1
32     find "$_DATA/" -path '*/.index/meta' -print0 \
33     | xargs -r0 sed -r '
34       s;^.*\t.*\t.*\ttags=(.*)\tcomment=.*\t.*\r$;\1;;
35       s;,;\n;g;'"$UNSTRING" \
36     | { sort; printf '\n'; } \
37     | while read -r tag; do
38       [ "$tag" = "$otag" ] \
39       && cn=$((cn + 1)) \
40       || {
41         printf "%i      %s\n" "$cn" "$otag"
42         cn=1
43       }
44       otag="$tag"
45     done
46   } |sort -rn |cut -f2- |HTML |sed "$w_ascii s-
-\n-g; s;\n\n;\n;g;" |tee "$w_tags" )"
47   w_tagcategories="$(printf %s "$w_tags" \
48                      | cut -sd: -f1 |sort -u \
49                      | tee "$w_tagcategories" )"
50 else
51   w_tags="$(cat "$w_tags")"
52   w_tagcategories="$(cat "$w_tagcategories")"
53 fi
54
55
56 [ "$ORDER" = Name   ] && w_coname=checked
57 [ "$ORDER" = Date   ] && w_codate=checked
58 [ "$ORDER" = Length ] && w_colength=checked
59
60 w_bookmarks(){
61   local name='' cf='' cs='' bm="$_DATA/.index/bookmarks" proposed_name=''
62   [ ! -d "${bm%/*}" ] && return 0
63   [ ! -f "$bm" ] && touch "$bm"
64
65   [ "$SEARCH" ] && cs="$(STRING "$SEARCH")"
66   [ "$FILTER" ] && cf="$(STRING "$FILTER")"
67
68   name="$(grep -m1 -aF "        search=$cs      filter=$cf${CR}" "$bm" |cut -f1 |UNSTRING)"
69   [ ! "$name" ] && proposed_name="$(
70     printf '%s\t%s' "$SEARCH" "$FILTER" \
71     |sed -r '/^\t$/{  s;\t;All;; q;}
72              /.*\t$/{ s;\t$;;; q;}
73              /^\t.*/{ s;^\t;;;
74                       :x; s;(^|[~^|])([^|^~:]+):;\1;; tx;
75                       s;\^; and ;g; s;\|;,;g; s;~;not ;g; q;}'
76     )"
77
78   printf '[form #bookmarks action=?a=bookmark method=POST
79             [a href="#" x]
80             [hidden "ref" "%s"]
81             [hidden "search" "%s"][hidden "filter" "%s"]
82             [label Name for current page:]
83             [input name="name" value="%s" placeholder="Name" ]
84             [button type="submit" %s]' \
85             "$w_refuri" \
86             "$(HTML "$SEARCH")" "$(HTML "$FILTER")" \
87             "$(HTML "${name:-${proposed_name}}")" \
88             "${name:+Update}${proposed_name:+Add}"
89   [ "$name" ] && printf ' [submit "delete" "delete" Delete]'
90
91   sort "$bm" |while read -r name search filter; do
92     search="${search#search=}" filter="${filter#filter=}" filter="${filter%${CR}}"
93     [ "$search" = "${cs}" -a "$filter" = "${cf}" ] && continue
94
95     name="$(UNSTRING "$name")";
96     search="$(UNSTRING "${search}" |URL)";
97     filter="$(UNSTRING "${filter}" |URL)";
98     printf '[label .link %s]
99             [a .link target=blank href="?o=Name&s=%s&f=%s" by Name]
100             [a .link target=blank href="?o=Date&s=%s&f=%s" by Date]
101             [a .link target=blank href="?o=Length&s=%s&f=%s" by Length]
102             [br]' \
103             "$(HTML "$name" |sed 's;&#44\;;&[wbr];g;')" \
104             "$search" "$filter" \
105             "$search" "$filter" \
106             "$search" "$filter"
107   done
108   printf ']'
109 }
110
111 w_search(){
112   printf '
113   [form #search method=GET action=?
114     [select name=o size=1 
115       [option disabled=disabled Order By]
116       [option value=Name %s Name]
117       [option value=Date %s Date]
118       [option value=Length %s Length]
119     ]
120     [input name=s placeholder=Search value="%s"]
121   ]
122   ' \
123   "$w_coname" "$w_codate" "$w_colength" \
124   "$(HTML "$SEARCH")"
125 }
126
127 w_prefs(){
128   local tm tf
129
130   tm=''; [ "$(COOKIE mode)" = index ] && tm=' '
131   tf=''; [ "$(COOKIE fakemp4)" = yes ] && tf=checked
132
133   printf '
134   [form #prefs method="POST" action="?a=setprefs"
135     [a href="#" x]
136     [hidden "ref" "%s"]
137     [label for=prefs_ps Pagesize]
138     [input #prefs_ps type=number name=pagesize value="%s"][br]
139     [radio "mode" "browse" %s #prefs_modebrowse] [label for=prefs_modebrowse Browse Folders][br]
140     [radio "mode" "index"  %s #prefs_modeindex ] [label for=prefs_modeindex View Full Index][br]
141     [checkbox "fakemp4" "yes" %s #prefs_fmp4] [label for=prefs_fmp4 Fake .MP4 file ending][br]
142     [submit "store" "store" Set Cookie]
143   ]
144   ' \
145   "$w_refuri" "$LISTSIZE" \
146   "${tm:-checked}" "${tm:+checked}" "$tf"
147 }
148
149 w_index(){
150   [ -d "$_DATA/$ITEM/.index" ] || printf '
151   [form #index method="POST" action="?a=spawnindex"
152     [hidden "ref" "%s"]
153     [label Set up for Index view: ]
154     [checkbox "recursive" "yes" #spawn_recursive] [label for=spawn_recursive Include subdirectories]
155     [submit "spawn" "spawn" Set up]
156   ]
157   ' "$w_refuri"
158 }
159
160 w_advsearch(){
161   local n lbid tag category filter f t
162   filter="$(HTML "${FILTER}^" |sed "$w_ascii")"
163
164   printf '[form #advsearch action=?a=advsearch method=POST
165             [a href="#" Hide]
166             [p .help Select multiple tags from each category by holding down the [strong Ctrl] key on your keyboard.[br]
167             Refine the search further by setting additional search tags using the [strong "+and"] button.]'
168
169   for n in 1 2 3 4 5 6 7 8 9 10; do
170     f="${filter%%^*}"; filter="${filter#*^}"
171
172     t=''; [ "$f" -a ! "${f%%~*}" ] && t=" "
173
174     lbid="cat_${n}_(none)"
175     printf '[input .and type=checkbox name=and id="and_%i" %s][label for="and_%i" +and
176             ][fieldset .select
177             [radio "pol_%i" "pos" .pol %s #pol_pos_%i"][label for=pol_pos_%i Any]
178             [radio "pol_%i" "neg" .pol %s #pol_neg_%i"][label for=pol_neg_%i None]
179             [label .head Category:]' \
180             $n "${f:+checked}" $n \
181             $n "${t:-checked}" $n $n \
182             $n "${t:+checked}" $n $n
183
184     f="|${f#~}|"
185     printf '*\n%s\n' "$w_tagcategories" \
186     | while read -r category; do
187       lbid="cat_${n}_${category}"
188
189       t=''
190       [ "$category"  = '*' -a   "${f%%|${category}:*}" ] && t=checked
191       [ "$category" != '*' -a ! "${f%%|${category}:*}" ] && t=checked
192
193       printf '[radio "cat_%i" "%s" .cat %s id="%s"][label for="%s" %s]
194               [select name=tag_%s size=10 multiple' \
195               $n "$category" "$t" "$lbid" "$lbid" "$category" $n
196
197       printf '%s\n' "$w_tags" \
198       | { [ "$category" = '*' ] && grep -avF ':' || grep -awF "${category}"; } \
199       | { for n in 1 2 3 4 5 6 7 8 9 0; do
200           read -r line && printf '%s\n' "$line" || break;
201           done;  # path 10 lines through without modification
202           sort;  # and sort remaining lines
203       } | while read -r tag; do
204         [ "$tag" ] || continue
205         t=''; [ ! "${f%%*|${tag}|*}" ] && t=checked
206         printf '[option %s value="%s"\n%s]' "$t" "$tag" "${tag#*:}"
207       done
208       printf '\n]'
209     done
210     printf ']'
211   done
212
213   printf '[fieldset .submit [select name=order
214             [option disabled=disabled Order By]
215             [option value=Name %s Name]
216             [option value=Date %s Date]
217             [option value=Length %s Length]
218           ][button type=submit Apply Filter]]
219           ]' \
220           "$w_coname" \
221           "$w_codate" \
222           "$w_colength"
223 }
224
225 w_tagging(){
226   local tag category
227   printf '[div #multitag [input type="hidden" name="ref" value="%s"]' "$w_refuri"
228   printf '[a href="#" Hide][br]'
229
230   printf 'Tags\n%s\n' "$w_tagcategories" \
231   | while read -r category; do
232     printf '[fieldset [legend %s:][select name=tag size=4 multiple' "$category"
233     printf %s "$w_tags" \
234     | { [ "$category" = 'Tags' ] && grep -avF ':' || grep -awF "${category}"; } \
235     | { for n in 1 2 3 4 5 6 7 8 9 0; do
236         read -r line && printf '%s\n' "$line" || break
237         done;
238         sort;
239     } | while read -r tag; do
240       [ "$tag" ] || continue
241       printf '[option value="%s"\n%s]' "$tag" "${tag#*:}"
242     done
243     printf ']]'
244   done
245
246   printf '[fieldset [legend New:][textarea name=newtag\n][button type=Submit Add Tags]]]'
247 }