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