]> git.plutz.net Git - serve0/blob - widgets.sh
call sed via xargs, which seems to be quicker than find -exec
[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_str_s="$(STRING "$SEARCH")"
11 w_str_f="$(STRING "$FILTER")"
12
13 c_tags="$_DATA/.index/tags.cache"; c_tagcategories="$_DATA/.index/tagcategories.cache"
14 if [ ! -s "$c_tags" -o ! -s "$c_tagcategories" ] \
15    || [ "$(find "$_DATA/" -path '*/.index/meta' -newer "$c_tags")" ]
16   then
17   w_tags="$( cn=1
18     find "$_DATA/" -path '*/.index/meta' -print0 \
19     | xargs -0 -r sed -E '
20       s;^.*\t.*\t.*\ttags=(.*)\tcomment=.*\t.*\r$;\1;;
21       s;,;\n;g;' \
22     | UNSTRING \
23     | { sort; printf '\n'; } \
24     | while read -r tag; do
25       [ "$tag" = "$otag" ] \
26       && cn=$((cn + 1)) \
27       || {
28         printf "%i      %s\n" "$cn" "$otag"
29         cn=1
30       }
31       otag="$tag"
32     done \
33     |sort -rn |cut -f2- |HTML \
34     |sed "s-
-\n-g; s;\n\n;\n;g;"
35   )"
36   w_tagcategories="$(printf %s "$w_tags" | sed -rn '/:/s;^-?([^:]+):.*$;\1;p' |sort -u )"
37   printf %s "$w_tags" >"$c_tags"
38   printf %s "$w_tagcategories" >"$c_tagcategories"
39 else
40   w_tags="$(cat "$c_tags")"
41   w_tagcategories="$(cat "$c_tagcategories")"
42 fi
43
44
45 [ "$ORDER" = Name   ] && w_coname=checked
46 [ "$ORDER" = Date   ] && w_codate=checked
47 [ "$ORDER" = Length ] && w_colength=checked
48 [ "$ORDER" = Group  ] && w_cogroup=checked
49
50 w_bmname=
51 w_bmname(){
52   [ "$w_bmname" ] || w_bmname="$(
53     bm="$_DATA/.index/bookmarks"
54     name="$(grep -m1 -aF "      search=${w_str_s}       filter=${w_str_f}${CR}" "$bm" 2>&-)"
55
56     if [ "$name" ]; then
57       printf '%s' "$name" |cut -f1 |UNSTRING |HTML
58     else
59       printf '%s\t%s' "$SEARCH" "$FILTER" \
60       | sed -r '/^\t$/{  s;\t;All;; q;}
61                 /.*\t$/{ s;\t$;;; q;}
62                 /^\t.*/{ s;^\t;;;
63                          :x; s;(^|[~^|])([^|^~:]+):;\1;; tx;
64                          s;\^; and ;g; s;\|;,;g; s;~;not ;g; q;}' \
65       | HTML
66     fi
67   )"
68   printf '%s' "$w_bmname"
69 }
70
71 w_bookmarks(){
72   local bm="$_DATA/.index/bookmarks" name='' search='' filter=''
73   [ ! -d "${bm%/*}" ] && return 0
74   [ ! -f "$bm" ] && touch "$bm"
75
76   grep -qaF "   search=$w_str_s filter=${w_str_f}${CR}" "$bm" && name=Update || name=Add
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             "$(w_bmname)" "${name}"
88   [ "$name" ] && printf ' [submit "delete" "delete" Delete]'
89
90   sort "$bm" |while read -r name search filter; do
91     search="${search#search=}" filter="${filter#filter=}" filter="${filter%${CR}}"
92     [ "$search" = "${w_str_s}" -a "$filter" = "${w_str_f}" ] && continue
93
94     name="$(UNSTRING "$name")";
95     search="$(UNSTRING "${search}" |URL)";
96     filter="$(UNSTRING "${filter}" |URL)";
97     printf '[label .link %s]
98             [a .link target=blank href="/?o=Name&s=%s&f=%s" by Name]
99             [a .link target=blank href="/?o=Date&s=%s&f=%s" by Date]
100             [a .link target=blank href="/?o=Length&s=%s&f=%s" by Length]
101             [a .link target=blank href="/?o=Group&s=%s&f=%s" by Group]
102             ' \
103             "$(HTML "$name" |sed 's;&#44\;;&[wbr];g;')" \
104             "$search" "$filter" \
105             "$search" "$filter" \
106             "$search" "$filter" \
107             "$search" "$filter"
108   done
109   printf ']'
110 }
111
112 w_search(){
113   printf '
114   [form #search method=GET action=./?
115     [select name=o size=1 
116       [option disabled=disabled Order By]
117       [option value=Name %s Name]
118       [option value=Date %s Date]
119       [option value=Length %s Length]
120       [option value=Group %s Group]
121     ]
122     [input name=s placeholder=Search value="%s"]
123     [a #t_avsearch href="#advsearch" Advanced]
124   ]
125   ' \
126   "$w_coname" "$w_codate" "$w_colength" "$w_cogroup" \
127   "$(HTML "$SEARCH")"
128 }
129
130 w_prefs(){
131   local tm tf td
132
133   tm=''; [ "$(COOKIE mode)" = index ] && tm=' '
134   tf=''; [ "$(COOKIE fakemp4)" = yes ] && tf=checked
135   td=''; [ "$(COOKIE downscale)" = yes ] && td=checked
136
137   printf '
138   [form #prefs method="POST" action="?a=setprefs"
139     [a href="#" x]
140     [hidden "ref" "%s"]
141     [label for=prefs_ps Pagesize]
142     [input #prefs_ps type=number name=pagesize value="%s"][br]
143     [radio "mode" "browse" %s #prefs_modebrowse] [label for=prefs_modebrowse Browse Folders][br]
144     [radio "mode" "index"  %s #prefs_modeindex ] [label for=prefs_modeindex View Full Index][br]
145     [checkbox "fakemp4" "yes" %s #prefs_fmp4] [label for=prefs_fmp4 Fake .MP4 file type][br]
146     [checkbox "downscale" "yes" %s #prefs_downscale] [label for=prefs_downscale Prefer downscale to 480p][br]
147     [submit "index" "update" Force Index Update][br]
148     [submit "store" "store" Set Cookie]
149   ]
150   ' \
151   "$w_refuri" "$LISTSIZE" \
152   "${tm:-checked}" "${tm:+checked}" "$tf" "$td"
153 }
154
155 w_index(){
156   printf '
157   [form #index method="POST" action="?a=spawnindex"
158     [hidden "ref" "%s"]
159     [label Set up for Index view: ]
160     [checkbox "recursive" "yes" #spawn_recursive] [label for=spawn_recursive Include subdirectories]
161     [submit "spawn" "spawn" Set up]
162   ]
163   ' "$w_refuri"
164   return 0
165 }
166
167 w_advsearch(){
168   local n lbid tag category filter f t d
169   filter="$(HTML "${FILTER}^")"
170
171   printf '[form #advsearch action=./?a=advsearch method=POST
172             [a href="#" X]
173             [p .help Select multiple tags from each category by holding down the [strong Ctrl] key on your keyboard.
174             Refine the search further by setting additional search tags using the [strong "+and"] button.]'
175
176   for n in 1 2 3 4 5 6 7 8 9 10; do
177     f="${filter%%^*}"; filter="${filter#*^}"
178
179     t=''; [ "$f" -a ! "${f%%~*}" ] && t=" "
180
181     lbid="cat_${n}_(none)"
182     printf '[input .and type=checkbox name=and id="and_%i" %s][label for="and_%i" +and
183             ][fieldset .select
184             [radio "pol_%i" "pos" .pol %s #pol_pos_%i"][label for=pol_pos_%i Any]
185             [radio "pol_%i" "neg" .pol %s #pol_neg_%i"][label for=pol_neg_%i None]
186             [label .head Category:]' \
187             $n "${f:+checked}" $n \
188             $n "${t:-checked}" $n $n \
189             $n "${t:+checked}" $n $n
190
191     f="|${f#\~}|"
192     printf '*\n%s\n$\n' "$w_tagcategories" \
193     | while read -r category; do
194       lbid="cat_${n}_${category}"
195
196       t=''
197       [ "$category"  = '*' -a   "${f%%|${category}:*}" ] && t=checked
198       [ "$category" != '*' -a ! "${f%%|${category}:*}" ] && t=checked
199       [ "$category" != '*' -a ! "${f%%|-${category}:*}" ] && t=checked
200
201       # printf '[radio "cat_%i" "%s" .cat %s id="%s"][label for="%s" %s]
202       #         [select name=tag_%s size=10 multiple' \
203       #         $n "$category" "$t" "$lbid" "$lbid" "$category" $n
204       printf '[radio "cat_%i" "%s" .cat %s id="%s"][label for="%s" %s]
205               [div .catselect\n' \
206               $n "$category" "$t" "$lbid" "$lbid" "$category"
207
208       printf '%s\n' "$w_tags" \
209       | { [ "$category" = '*' ] && grep -avF ':' || grep -awF "${category}"; } \
210       | { for n in 1 2 3 4 5 6 7 8 9 0; do
211           read -r line && printf '%s\n' "$line" || break;
212           done;  # pass 10 lines through without modification
213           sort;  # and sort remaining lines
214       } | while read -r tag; do
215         [ "$tag" ] || continue
216         t=''; [ ! "${f%%*|${tag}|*}" ] && t=checked
217         d="${tag#-}"; d="${d#*:}"
218         # printf '[option %s value="%s"\n%s]' "$t" "$tag" "$d"
219         printf '[label [checkbox "tag_%s" "%s" %s] %s]' "$n" "$tag" "$t" "$d"
220       done
221       d="${f##*\$:}" d="${d%%\|*}"
222       [ "$category" = \$ ] && printf '[input name="tag_%i" value="%s"]' "$n" "$(HTML "$d")"
223       printf '\n]'
224     done
225     printf ']'
226   done
227
228   printf '[fieldset .submit [select name=order
229             [option disabled=disabled Order By]
230             [option value=Name %s Name]
231             [option value=Date %s Date]
232             [option value=Length %s Length]
233             [option value=Group %s Group]
234           ][button type=submit Apply Filter]]
235           ]' \
236           "$w_coname" "$w_codate" \
237           "$w_colength" "$w_cogroup"
238 }
239
240 w_delete(){
241   printf '[a href="#multitag" Add Tags / Remove Tags]
242           [div #multitag [input type="hidden" name="ref" value="%s"]
243           [a href="#" X]
244           [fieldset [legend New:]
245           [submit "op" "filedelete" Delete Files]
246           ]]' "$w_refuri"
247 }
248
249 w_tagging(){
250   local tag category d
251   printf '[a href="#multitag" Add Tags / Remove Tags]
252           [div #multitag [input type="hidden" name="ref" value="%s"]' "$w_refuri"
253   printf '[a href="#" X]'
254
255   printf 'Tags\n%s\n' "$w_tagcategories" \
256   | while read -r category; do
257     [ "$category" ] || continue
258     # printf '[fieldset [legend %s:][select name=tag size=4 multiple\n' "$category"
259     printf '[fieldset [legend %s:][div .tagselect\n' "$category"
260     printf %s "$w_tags" \
261     | { [ "$category" = 'Tags' ] && grep -avF ':' || grep -awF "${category}"; } \
262     | { for n in 1 2 3 4 5 6 7 8 9 0; do
263         read -r line && printf '%s\n' "$line" || break
264         done;
265         sort;
266     } | while read -r tag; do
267       [ "$tag" ] || continue
268       d="${tag#-}"; d="${d#*:}"
269       # printf '[option value="%s"\n%s]' "$tag" "$d"
270       printf '[label [checkbox "tag" "%s"] %s]\n' "$tag" "$d"
271     done
272     printf ']]'
273   done
274
275   printf '[fieldset [legend New:][textarea name=newtag\n]
276           [submit "op" "del" Remove Tags][submit "op" "add" Add Tags]
277           ]]'
278 }