]> Sergey Matveev's repositories - zk.zsh.git/blobdiff - zk.zsh
Lighter, simpler and cleaner indices HTML generation
[zk.zsh.git] / zk.zsh
diff --git a/zk.zsh b/zk.zsh
index 9b2d0a5426a106855aed0bf6eb8b75ca43292d99..b6ac72d05e46e06d663e437ca9a5a41ddf2459e5 100755 (executable)
--- a/zk.zsh
+++ b/zk.zsh
@@ -6,7 +6,7 @@ set -e
 ZK_VERSION=ZKZSH1
 
 usage() {
-    cat >&2 <<EOF
+    >&2 <<EOF
 Usage:
   \$ $0:t links PAGE
     Print PAGE's links
@@ -20,11 +20,16 @@ EOF
 
 [[ $# -eq 2 ]] || usage
 
+separator="------------------------ >8 ------------------------"
 setopt GLOB_STAR_SHORT
 zmodload -F zsh/stat b:zstat
 typeset -A pages
 typeset -A sizes
 for p (**(.)) {
+    [[ $p:t == "index" ]] && {
+        echo unacceptable filename: $p >&2
+        exit 1
+    }
     zstat -A mtime -F "%F %T" +mtime $p
     zstat -A size +size $p
     pages[$p]=${mtime[1]}
@@ -67,7 +72,7 @@ for p (${(k)pages}) {
         zf_mkdir -p $ZK_CACHE/$p:h
         ws=($ZK_VERSION ${inode[1]} ${ctime[1]} $ws)
         print -l $ws > $ZK_CACHE/$p
-        ws=(${ws[3,-1]})
+        ws=(${ws[4,-1]})
     }
     [[ $ws ]] && links[$p]=${(j: :)ws}
 }
@@ -102,7 +107,7 @@ genHTML() {
     [[ $# -eq 1 ]] && data=${mapfile[$page]} || data=$2
     local _links=(${(oi)=links[$page]})
     if [[ ( ${cached[$page]} ) && ( -s $ZK_CACHE/${page}.html ) ]]; then
-        cat $ZK_CACHE/${page}.html
+        < $ZK_CACHE/${page}.html
     else
         data=${data//&/&amp;}
         data=${data//</&lt;}
@@ -116,7 +121,7 @@ genHTML() {
 $data</pre>"
         if [[ $ZK_CACHE ]]; then
             print -r "$data" > $ZK_CACHE/${page}.html
-            cat $ZK_CACHE/${page}.html
+            < $ZK_CACHE/${page}.html
         else
             print -r "$data"
         fi
@@ -151,25 +156,30 @@ genIndex() {
     typeset -aU cats=()
     local curdepth=${#${(s:/:)1}}
     (( curdepth = curdepth + 1 ))
-    for p (${(oi)${(k)pages[(I)$1*]}}) {
-        [[ ( $p =~ "/index$" ) || ( $p = "index" ) ]] && continue
+    for p (${(k)pages[(I)$1*]}) {
         case ${#${(As:/:)p}} in
         ($curdepth) _links=($p $_links) ;;
         ( $(( $curdepth + 1 )) ) cats=(${1}${${p#$1}%%/*} $cats) ;;
         (*) continue ;;
         esac
     }
-    for p (${(oi)_links}) \
-        entries=($entries "[$p] (${pages[$p]}) (${sizes[$p]} bytes)")
+    local page=${1}index
+    print "<!DOCTYPE html>
+<html><head><title>$page ($now)</title></head><body><ul>"
+    for p (${(oi)_links}) {
+        getrel $page $p
+        print "<li><a href=\"${REPLY}.html\">$p</a> <sup>${pages[$p]}</sup> (${sizes[$p]} bytes)</li>"
+    }
+    print "</ul>"
     if [[ $cats ]]; then
-        entries=($entries " " "Subdirectories:" " ")
+        print "<hr/>Subdirectories:<ul>"
         for p (${(oi)cats}) {
-            entries=($entries "[$p/index]")
-            _links=($p/index $_links)
+            getrel $page $p/index
+            print "<li><a href=\"${REPLY}.html\">$p</a></li>"
         }
+        print "</ul>"
     fi
-    links[${1}index]=${(j: :)_links}
-    genHTML ${1}index ${(F)entries}
+    print "</body></html>"
 }
 
 case $1 in
@@ -183,8 +193,6 @@ case $1 in
         genHTML $p > $2/$p.html
         touch -r $p $2/$p.html
     }
-    for p ($cats) pages[${p}/index]=$now
-    pages[index]=$now
     for p ($cats) genIndex $p/ > $2/$p/index.html
     genIndex "" > $2/index.html
     for p ("" $cats) touch -d ${now/ /T} $2/$p/index.html