]> git.plutz.net Git - serve0/blob - list.sh
allow manual grouping
[serve0] / list.sh
1 #!/bin/sh
2
3 . "$_EXEC/indexmeta.sh"
4 . "$_EXEC/widgets.sh"
5 . "$_EXEC/db_meta.sh"
6
7 list_item() {
8   local meta file link name
9   eval "$LOCAL_META"
10   meta="${1}";
11
12   read_meta <<-EOF
13         $meta
14         EOF
15
16   if [ "${META_NAME%/}" != "${META_NAME}" ]; then
17     printf '[a .list .dir href="%s?%s" . %s]' \
18            "$(URL "${PATH_INFO%/}/${META_NAME}")" "${w_refuri#*\?}" \
19            "$(HTML "${META_NAME%/}")"
20     return 0
21   fi
22
23   file="$_DATA/${PATH_INFO%/}/$(list_fullname "${META_NAME}")"
24   if [ -f "$file" ]; then
25     link="$(URL "${PATH_INFO%/}/${file#${_DATA}/${PATH_INFO}}")"
26     name="$(HTML "${PATH_INFO%/}/${file#${_DATA}/${PATH_INFO}}")"
27     printf '[div .list .file
28               [a href="%s" [img src="%s?a=thumbnail"]][label . %s]
29               [span .time %i:%02imin] [span .dim %ix%i] %s
30               [checkbox "select" "%s" id="select_%s"][label for="select_%s" +]
31             ]' \
32       "$link" "$link" "${name##/}" \
33       "$((META_LENGTH / 60))" "$((META_LENGTH % 60))" \
34       "$META_WIDTH" "$META_HEIGHT" \
35       "$(printf %s\\n "${META_TAGS}" \
36          | sed -r 's;^;,;; s;,+;,;g; s;,$;;;
37                    :X s;,-?([^,]+)(,|$); [span .tag\n \1]\2;; tX;'
38       )" "$name" "$link" "$link"
39   else
40     debug "Canning record for nonexist file: $META_NAME"
41     meta_purge "$_DATA/$ITEM/$META_NAME"
42   fi
43 }
44
45 [ "$FILTER" ] && list_fex="$(
46   fex='p'
47   STRING "$FILTER^" \
48   | sed -E 's;\^;\n;g; s;[]\/\(\)\\\^\$\?\.\+\*\;\[\{\}];\\&;g' \
49   | while read -r f; do
50     [ "${f##*[A-Z]*}" ] && tl="y;ABCDEFGHIJKLMNOPQRSTUVWXYZ;abcdefghijklmnopqrstuvwxyz;;"
51     case $f in
52       ''|~) continue;;
53       ~\\\$:*) fex="h; ${tl} /${f#~\\\$:}/d; g;${fex}";;
54       \\\$:*) fex="h; ${tl} /${f#\\\$:}/{g;${fex}}";;
55       ~*) fex="/(\ttags=([^\t]*,)?)(${f#\~})((,[^\t]*)?\t)/d; ${fex}";;
56        *) fex="/(\ttags=([^\t]*,)?)(${f})((,[^\t]*)?\t)/{${fex}}";;
57     esac
58     printf '%s\n' "${fex}"
59   done \
60   | tail -n1
61 )"
62
63 list_fullname(){
64   local short="$(UNSTRING "${1%${CR}}")" file
65   file="$(printf %s\\n "$_DATA/$ITEM/$short".*)"
66   file="${file%%${BR}*}"
67   [ -e "$file" ] && printf %s\\n "${file#${_DATA}/${ITEM}/}"
68 }
69
70 list_filter(){
71   if [ "$FILTER" ]; then
72     debug "FEX:" "$list_fex"
73     sed -nE "$list_fex"
74   elif [ "${SEARCH#!}" != "${SEARCH}" ]; then
75     grep -aviEe "$(STRING "${SEARCH}" \
76                  | sed -E ':x s;((^|[^\\])(\\\\)*)\+;\1 ;g; tx;
77                             s;((^|[^\\])(\\\\)*)\\\+;\1+;g;
78                             s; ;\\+;g;')"
79   elif [ "${SEARCH}" ]; then
80     grep -aiEe "$(STRING "${SEARCH}" \
81                  | sed -E ':x s;((^|[^\\])(\\\\)*)\+;\1 ;g; tx;
82                             s;((^|[^\\])(\\\\)*)\\\+;\1+;g;
83                             s; ;\\+;g;')"
84   else
85     cat
86   fi
87 }
88
89 list_order(){
90   local fm fn fn al length ln h w t c name buffer l
91
92   if [ $ORDER = Name ]; then
93     sort -k6
94   elif [ $ORDER = Group ]; then
95     sed -E '
96       /^[^\t]+\t[^\t]+\t[^\t]+\t[^\t]+\t[^\t]+\t[^\r]*\r$/bX;
97       /^[^\t]+\t[^\t]+\t[^\t]+\t[^\t]+\t[^\t]+\t[^\r]*\r\t\\$/bX;
98       s;^([^\t]+\t[^\t]+\t[^\t]+\t[^\t]+\t[^\t]+\t)([^\r]*)\r\t(.*)$;\1\r\3 \2\r\t;;
99       t;
100       :X
101       s;^([^\t]+\t[^\t]+\t[^\t]+\t[^\t]+\t[^\t]+\t[^ 0-9]*)-([0-9a-zA-Z_-]{11}\r|ph[0-9a-f]{13}\r|[0-9]{8}\r)(.*)$;\1-\r\3 \2;;
102       t;
103       s;^([^\t]+\t[^\t]+\t[^\t]+\t[^\t]+\t[^\t]+\t[^ 0-9]*)([0-9]+)(.*)$;\1\r\3 \2;;
104       tX;' \
105     | { sort -n -k7 -k8 -k9 -k10 -k11 |sort -s -k6,6 ; echo '0 0 0 tags= comment= _'; } \
106     | while read -r length w h t c name; do
107       if [ "${ln%% *}" = "${name%% *}" ]; then
108         al=$((al + length))
109         buffer="${buffer}${BR}$length   $w      $h      $t      $c      $name"
110       else
111         printf '%s\n' "$buffer" |while read -r l; do
112           [ "$l" ] && printf '%i        %s\n' "$al" "$l"
113         done
114         al="$length"
115         buffer="$length $w      $h      $t      $c      $name"
116       fi
117       ln="$name"
118     done \
119     | sort -s -n -k1,1 |sed -E 's;^[0-9]+\t;;;' \
120     | sed -E '
121       s;^([^\t]+\t[^\t]+\t[^\t]+\t[^\t]+\t[^\t]+\t)\r([0-9A-Za-z:=]{16}) ([^ ]+)$;\1\3\r\t\2;; t;
122       :X s;^([^\t]+\t[^\t]+\t[^\t]+\t[^\t]+\t[^\t]+\t[^\r]*)\r([^ ]*) ([^ ]+)( .*)?$;\1\3\2\4;; tX
123     '
124   elif [ $ORDER = Length ]; then
125     sort -sn -k1
126   elif [ $ORDER = Date ]; then
127     while read -r fm; do
128       fn="${fm%${CR}    *}"
129       fn="$(list_fullname "${fn##*      }")"
130       printf '%i        %s\n' \
131              "$(stat -c %Y "$fn")" "${fm}"
132     done \
133     | sort -srn -k1 |sed -E 's;^[0-9]+\t;;;'
134   fi
135 }
136
137 list_items() {
138   local mode meta cachename
139   mode="$(COOKIE mode |grep -m1 -axE 'index|browse' || printf index )"
140   
141   cachename="$(printf '%s\n' "$mode" "$FILTER" "$SEARCH" "$ORDER" |sha1sum)"
142   cachename="$_DATA/$ITEM/.index/${cachename%  -}.cache"
143   meta="$_DATA/$ITEM/.index/meta"
144   meta_dir "$_DATA/$ITEM/"
145
146   if [ "$mode" = browse ]; then
147     [ "$ITEM" ] && printf '0    0       0       \       \       ../\n'
148     (cd "$_DATA/$ITEM";
149       find ./ -type d \! -name .index -mindepth 1 -maxdepth 1 \
150     ) | sort |while read dir; do
151       printf '0 0       0       \\      \\      %s\n' "$(STRING "${dir#./}")"
152     done
153
154     if [ "$cachename" -nt "$meta" ]; then
155       cat "$cachename"
156     else
157       list_meta "$meta" \
158       | list_filter \
159       | list_order \
160       | { [ -d "${cachename%/*}" ] && tee "$cachename" || cat; }
161     fi
162
163   elif [ "$mode" = index ]; then
164     if [ -f "$cachename" -a ! "$(find "$_DATA" -path '*/.index/meta' -newer "$cachename")" ]; then
165       cat "$cachename"
166     else
167       list_meta \
168       | list_filter \
169       | list_order \
170       | { [ -d "${cachename%/*}" ] && tee "$cachename" || cat; }
171     fi
172   fi
173 }
174
175 list_paginate() {
176   local page i c n end qry
177   page="$(GET p |grep -axE '[0-9]+' || printf 1)"; c=1
178   end=$((page + LISTSIZE))
179   qry="${w_refuri#*\?}"; qry="${qry#&#112;&#61;*&#38;}"
180
181   printf '[div .itemlist '
182   while read -r i; do
183     c=$((c + 1))
184     [ $c -gt $page -a $c -le $end  ] && list_item "$i"
185   done
186   printf ']'
187
188   [ $(( c % LISTSIZE )) -gt 0 ] \
189   && end=$((c / LISTSIZE + 1)) \
190   || end=$((c / LISTSIZE))
191
192   printf '[div .pagination'
193   for n in $( seq 1 $end ); do
194     c=$(( (n - 1) * LISTSIZE + 1 ))
195     [ $c = $page ] \
196     && printf '[a .page .current href="%s" %s]' "?p=${c}&${qry}" "$n" \
197     || printf '[a .page href="%s" %s]' "?p=${c}&${qry}" "$n"
198   done
199   printf ']'
200 }
201
202 printf 'Content-Type: text/html;charset=utf-8\r\n\r\n'
203
204 { printf '
205 [!DOCTYPE HTML]
206 [html [head [title '
207   w_bmname
208   printf ' by %s]' "$ORDER"
209   printf '
210   [meta name="viewport" content="width=device-width"]
211   [link rel=stylesheet href="/cgilite/common.css" ]
212   [link rel=stylesheet href="/style.css" ]
213 ] [body
214   [div #navigation
215     [a #t_bookmarks href="#bookmarks" &#x2605;]'
216     w_search
217     printf '
218     [a #t_prefs href="#prefs" &#x2699;]
219   ]'
220   w_bookmarks
221   w_advsearch
222   w_prefs
223   printf '
224   [form method=POST action="?a=multitag"'
225     list_items \
226     | list_paginate
227     [ -d "$_DATA/$ITEM/.index" ] && { printf '
228     [div #editing'
229       w_tagging
230     printf '
231     ]'; }
232   printf '
233   ]'
234   [ ! -d "$_DATA/$ITEM/.index" ] && { printf '
235   [div #editing'
236     w_index
237   printf '
238   ]'; }
239   printf '
240 ] ]
241 '; } | "$_EXEC/cgilite/html-sh.sed"