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