]> git.plutz.net Git - serve0/blob - list.sh
list styling
[serve0] / list.sh
1 #!/bin/sh
2
3 list_item() {
4   name="$(HTML "$1")"
5   path="$(HTML "$ITEM/$1")"
6   meta="$_DATA/$ITEM/.index/meta"
7
8   if [ -d "$_DATA/$ITEM/$1" ]; then
9     printf '[a .list .dir href="%s" %s]' \
10       "$path" "$name"
11   elif [ -f "$meta" ]; then
12     read -r length width height tags comment n <<-EOF
13         $(grep -m1 -F " $1" "$meta")
14         EOF
15     printf '[div .list .file
16               [a href="%s" [img src="%s?a=thumbnail"][label %s]]
17               [span .time %i:%imin] [span .dim %ix%i] %s
18             ]' \
19       "$path" "$path" "$name" \
20       "$((length / 60))" "$((length % 60))" \
21       "$width" "$height" \
22       "$(printf %s\\n "${tags#tags=}" |tr , ' ' |xargs printf '[span .tag %s]')"
23   else
24     printf '[div .list .file [a href="%s" [img src="%s?a=thumbnail"][label %s]]]' \
25       "$path" "$path" "$name"
26   fi
27 }
28
29 list_items() {
30   mode="$(COOKIE mode |grep -m1 -xE 'index|browse' || printf browse )"
31
32   [ "$mode" = browse -a "$ITEM" ] && printf '..\n'
33   if [ "$mode" = browse ]; then
34     [ -d "$_DATA/$ITEM/.index" -a \! "$_DATA/$ITEM" -ot "$_DATA/$ITEM/.index/meta" ] \
35     && dir="$_DATA/$ITEM" . "$_EXEC/update_meta.sh"
36     (cd "$_DATA/$ITEM";
37      find ./ -type d \! -name .index -mindepth 1 -maxdepth 1 \
38        -exec stat -c '%Y        %n' '{}' +
39      find ./ -type f -mindepth 1 -maxdepth 1 \
40        -exec stat -c '%Y        %n' '{}' +
41     )
42   elif [ "$mode" = index ]; then
43     find "$_DATA/$ITEM" -type d -name .index \
44     | while d="$(line)"; do
45       [ ! "${d%/.index}" -ot "$d/meta" ] \
46       && dir="${d%/.index}" . "$_EXEC/update_meta.sh"
47     done
48     (cd "$_DATA/$ITEM";
49      find ./ -type f \
50        -exec stat -c '%Y        %n' '{}' +
51     )
52   fi \
53   | case $ORDER in
54     Date) sort -rn;;
55     Name) sort -k 2;;
56   esac \
57   | cut -d/ -f2-
58 }
59
60 list_paginate() {
61   page="$(GET p |grep -xE '[0-9]+' || printf 1)"
62
63   printf '[div .itemlist '
64   while i="$(line)"; do
65     c=$((${c-0} + 1))
66     if [ $c -lt $page ]; then
67       true
68     elif [ $c -lt $((LISTSIZE + page)) ]; then
69       list_item "$i"
70     fi
71   done
72   printf ']'
73
74   printf '[div .pagination'
75   for n in $(seq 1 $((c / LISTSIZE + 1)) ); do
76     printf '[a .page href="%s" %s]' \
77       "?p=$(( (n - 1) * LISTSIZE + 1))" "$n"
78   done
79   printf ']'
80 }
81
82 printf 'Content-Type: text/html;charset=utf-8\r\n\r\n'
83
84 "$_EXEC/cgilite/html-sh.sed" <<-EOF
85 [!DOCTYPE HTML]
86 [html [head [title Listing]
87   [link rel=stylesheet href="/style.css" ]
88 ] [body
89   [div #navigation
90     [a #t_bookmarks href="#bookmarks" &#x2605;]
91     $(w_search)
92     [a #t_avsearch href="#advsearch" Advanced]
93     [a #t_prefs href="#prefs" &#x2699;]
94   ]
95   $(w_prefs)
96
97   $(list_items \
98     | list_paginate
99   )
100   [div #editing
101     $(w_index)
102   ]
103 ] ]
104 EOF
105