]> git.plutz.net Git - serve0/blob - list.sh
styling for pagination links
[serve0] / list.sh
1 #!/bin/sh
2
3 list_item(){
4   name="$(HTML "$1")"
5   path="$(HTML "$ITEM/$1")"
6
7   if [ -d "$_DATA/$ITEM/$1" ]; then
8     printf '[a .list .dir href="%s" %s]' \
9       "$path" "$name"
10   else
11     printf '[div .list .file [a href="%s" [img src="%s?a=thumbnail"] %s]]' \
12       "$path" "$path" "$name"
13   fi
14 }
15
16 list_dirs(){
17   [ "$ITEM" ] && printf '..\n'
18   (cd "$_DATA/$ITEM";
19    find ./ -type d -mindepth 1 -maxdepth 1 \
20      -exec stat -c '%Y  %n' '{}' +
21    ) \
22   | { [ "$(GET o)" = Date ] && sort -rn || sort -k 2; } \
23   | cut -d/ -f2-
24 }
25
26 list_files(){
27   (cd "$_DATA/$ITEM";
28    find ./ -type f -mindepth 1 -maxdepth 1 \
29      -exec stat -c '%Y  %n' "${f#./}" '{}' + 2>&-
30    ) \
31   | { [ "$(GET o)" = Date ] && sort -rn || sort -k 2; } \
32   | cut -d/ -f2-
33 }
34
35 list_tree(){
36   (cd "$_DATA/$ITEM";
37    find ./ -type f \
38      -exec stat -c '%Y  %n' '{}' +
39    ) \
40   | { [ "$(GET o)" = Date ] && sort -rn || sort -k 2; } \
41   | cut -d/ -f2-
42 }
43
44 list_paginate(){
45   page="$(GET p |grep -xE '[0-9]+' || printf 1)"
46
47   printf '[div .itemlist '
48   while i="$(line)"; do
49     c=$((${c-0} + 1))
50     if [ $c -lt $page ]; then
51       true
52     elif [ $c -lt $((LISTSIZE + page)) ]; then
53       list_item "$i"
54     fi
55   done
56   printf ']'
57
58   printf '[div .pagination'
59   for n in $(seq 1 $((c / LISTSIZE + 1)) ); do
60     printf '[a .page href="%s" %s]' \
61       "?p=$(( (n - 1) * LISTSIZE + 1))" "$n"
62   done
63   printf ']'
64 }
65
66 printf 'Content-Type: text/html;charset=utf-8\r\n\r\n'
67
68 "$_EXEC/cgilite/html-sh.sed" <<-EOF
69 [!DOCTYPE HTML]
70 [html [head [title Listing]
71   [link rel=stylesheet href="/style.css" ]
72 ] [body
73   $(w_search)
74   [input type=checkbox #t_prefs .toggle ][label for=t_prefs &#x2699;]
75   $(w_prefs)
76   $(if [ "$(COOKIE mode)" = index ]; then
77     list_tree
78   else
79     list_dirs
80     list_files
81   fi \
82   | list_paginate
83   )
84 ] ]
85 EOF
86