]> git.plutz.net Git - serve0/blob - list.sh
various style improvements
[serve0] / list.sh
1 #!/bin/sh
2
3 . "$_EXEC/indexmeta.sh"
4 . "$_EXEC/widgets.sh"
5
6 list_item() {
7   local name link path length width height tags comment n
8   name="$(HTML "$1")"
9   link="$(URL "$ITEM/$1")"
10   path="$(HTML "$ITEM/$1")"
11   qry=$(HTML "$QUERY_STRING")
12
13   if [ -d "$_DATA/$ITEM/$1" ]; then
14     printf '[a .list .dir href="%s" %s]' "${path}?${qry}" "$name"
15   elif [ -f "$_DATA/$ITEM/$1" ]; then
16     read -r length width height tags comment n <<-EOF
17         $(meta_info "$_DATA/$ITEM/$1")
18         EOF
19     printf '[div .list .file
20               [a href="%s" [img src="%s?a=thumbnail"]][label %s]
21               [span .time %i:%imin] [span .dim %ix%i] %s
22               [checkbox "select" "%s" id="select_%s"][label for="select_%s" +]
23             ]' \
24       "$link" "$link" "$name" \
25       "$((length / 60))" "$((length % 60))" \
26       "$width" "$height" \
27       "$(UNSTRING "${tags#tags=}" |tr , '\0' |xargs -r0 printf ' [span .tag %s]')" \
28       "$path" "$path" "$path"
29   else
30     printf 'Canning record for nonexist file: %s\n' "$1" >&2
31     meta_purge "$_DATA/$ITEM/$1"
32   fi
33 }
34
35 list_directories(){
36   (cd "$_DATA/$ITEM";
37     find ./ -type d \! -name .index -mindepth 1 -maxdepth 1 \
38   ) | cut -d/ -f2- | sort
39 }
40
41 [ "$FILTER" ] && list_fex="$(
42   fex='p'
43   STRING "$FILTER^" \
44   | sed -r 's;\^;\n;g; s;[]\/\(\)\\\^\$\?\.\+\*\;\[\{\}];\\&;g' \
45   | while read -r f; do
46     [ ! "${f#~}" ] && continue
47     [ "${f#~}" = "$f" ] \
48     && fex="/(\ttags=([^\t]*,)?)(${f})((,[^\t]*)?\t)/{${fex}}" \
49     || fex="/(\ttags=([^\t]*,)?)(${f#~})((,[^\t]*)?\t)/d; ${fex}"
50     printf '%s\n' "${fex}"
51   done \
52   | tail -n1
53 )"
54
55 list_fullname(){
56   sn="$1"
57   [ ! "${sn%%*/*}" ] && base="${sn%/*}" || base=.
58   file="$(printf '%s' "$_DATA/$ITEM/$sn".*)"
59   file="${file##*/}"
60   [ -e "$_DATA/$ITEM/$base/${file}" ] \
61   && printf '%s\n' "${base}/${file}"
62 }
63
64 list_filemeta(){
65   local meta base f fn file
66   base="$1"
67   meta="$_DATA/$ITEM/$base/.index/meta"
68   meta_dir "$_DATA/$ITEM/$base"
69
70   if [ $ORDER = Name ]; then
71     sort -k6 "$meta"
72   elif [ $ORDER = Length ]; then
73     sort -n -k1 "$meta"
74   else
75     cat "$meta"
76   fi \
77   | if [ "$FILTER" ]; then
78     sed -nr "$list_fex"
79   elif [ "${SEARCH#!}" != "${SEARCH}" ]; then
80     grep -aviE "$(STRING "${SEARCH#!}" |sed -r ':x s;((^|[^\\])(\\\\)*)\+;\1\\+;g; tx;')"
81   else
82     grep -aiE "$(STRING "${SEARCH}" \
83                  | sed -r ':x s;((^|[^\\])(\\\\)*)\+;\1 ;g; tx;
84                             s;((^|[^\\])(\\\\)*)\\\+;\1+;g;
85                             s; ;\\+;g;')"
86   fi |cut -f6 | UNSTRING \
87   | while read -r fn; do
88     printf '%s\n' "${base}/${fn%${CR}}"
89   done | tee .index/debug
90 }
91
92 list_index(){
93   local meta
94   (cd "$_DATA/$ITEM";
95     find ./ -path '*/.index/meta'
96   ) | while read -r meta; do
97     list_filemeta "${meta%/.index/meta}"
98   done
99 }
100
101 list_dateorder(){
102   while read -r sn; do 
103     list_fullname "$sn"
104   done \
105   | xargs -rd'\n' stat -c '%Y   %n' \
106   | sort -rn |cut -d/ -f2- \
107   | sed -r 's;\.[^\.]*$;;;'
108 }
109
110 list_items() {
111   local mode
112   mode="$(COOKIE mode |grep -m1 -axE 'index|browse' || printf browse )"
113
114   [ "$mode" = browse -a "$ITEM" ] && printf '..\n'
115
116   if   [ "$mode" = browse -a "$ORDER" = Date ]; then
117     list_directories
118     list_filemeta . |list_dateorder
119   elif [ "$mode" = index  -a "$ORDER" = Date ]; then
120     list_index |list_dateorder
121   elif [ "$mode" = browse ]; then
122     list_directories
123     list_filemeta .
124   elif [ "$mode" = index ]; then
125     list_index
126   fi
127 }
128
129 list_paginate() {
130   local page i c n end
131   page="$(GET p |grep -axE '[0-9]+' || printf 1)"; c=1
132   end=$((page + LISTSIZE))
133
134   printf '[div .itemlist '
135   while read -r i; do
136     [ $c -ge $page -a $c -lt $end  ] \
137     && list_item "$(list_fullname "$i")"
138     c=$((c + 1))
139   done
140   printf ']'
141
142   printf '[div .pagination'
143   for n in $( seq 1 $((c / LISTSIZE + 1)) ); do
144     printf '[a .page href="%s" %s]' \
145       "?p=$(( (n - 1) * LISTSIZE + 1))&$qry" "$n"
146   done
147   printf ']'
148 }
149
150 printf 'Content-Type: text/html;charset=utf-8\r\n\r\n'
151
152 { printf '
153 [!DOCTYPE HTML]
154 [html [head [title Listing]
155   [meta name="viewport" content="width=device-width"]
156   [link rel=stylesheet href="/style.css" ]
157 ] [body
158   [div #navigation
159     [a #t_bookmarks href="#bookmarks" &#x2605;]'
160     w_search
161     printf '
162     [a #t_avsearch href="#advsearch" Advanced]
163     [a #t_prefs href="#prefs" &#x2699;]
164   ]'
165   w_bookmarks
166   w_advsearch
167   w_prefs
168   printf '
169   [form method=POST action="?a=multitag"'
170     list_items \
171     | list_paginate
172     printf '
173     [div #editing
174       [a href="#multitag" Add Tags]'
175       w_tagging
176       w_index
177       printf '
178     ]
179   ]
180 ] ]
181 '; } | "$_EXEC/cgilite/html-sh.sed"