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