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