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