]> git.plutz.net Git - serve0/blob - list.sh
pagination function
[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   while i="$(line)"; do
47     c=$((${c-0} + 1))
48     if [ $c -lt $page ]; then
49       true
50     elif [ $c -lt $((LISTSIZE + page)) ]; then
51       list_item "$i"
52     fi
53   done
54
55   for n in $(seq 1 $((c / LISTSIZE + 1)) ); do
56     printf '[a .page href="%s" %s]' \
57       "?p=$(( (n - 1) * LISTSIZE + 1))" "$n" |tee /dev/stderr
58   done
59 }
60
61 printf 'Content-Type: text/html;charset=utf-8\r\n\r\n'
62
63 "$_EXEC/cgilite/html-sh.sed" <<-EOF
64 [!DOCTYPE HTML]
65 [html [head [title Listing]
66   [link rel=stylesheet href="/style.css" ]
67 ] [body
68   $(w_search)
69   [input type=checkbox #t_prefs .toggle ][label for=t_prefs &#x2699;]
70   $(w_prefs)
71   $(if [ "$(COOKIE mode)" = index ]; then
72     list_tree
73   else
74     list_dirs
75     list_files
76   fi \
77   | list_paginate
78   )
79 ] ]
80 EOF
81