]> Sergey Matveev's repositories - zk.zsh.git/commitdiff
Separate Index-es for each category/subdirectory
authorSergey Matveev <stargrave@stargrave.org>
Sun, 13 Mar 2022 13:49:24 +0000 (16:49 +0300)
committerSergey Matveev <stargrave@stargrave.org>
Sun, 13 Mar 2022 13:49:24 +0000 (16:49 +0300)
zk.zsh

diff --git a/zk.zsh b/zk.zsh
index 81e032118715b6c02f0188f0e82948754c602504..348a5b521b2e066d581741ded0f5baf0fbf83dcf 100755 (executable)
--- a/zk.zsh
+++ b/zk.zsh
@@ -25,6 +25,8 @@ for p (**(.)) {
     zstat -A reply -F "%F %T" +mtime $p
     pages[$p]=${reply[1]}
 }
+typeset -a cats
+for p (**(/)) cats=($p $cats)
 
 # Determine the links between them
 typeset -A links
@@ -71,7 +73,7 @@ getrel() {
     REPLY=$src${dst:+/$dst}
 }
 
-genhtml() {
+genHTML() {
     local page=$1
     local data p
     [[ $# -eq 1 ]] && data=`< $page` || data=$2
@@ -86,29 +88,59 @@ genhtml() {
     print "<\!DOCTYPE html>
 <html><head><title>$page (${pages[$page]})</title></head><body><pre>
 $data
-</pre><hr/><ul>"
-    for p (${(oi)=${backs[$page]}}) {
-        getrel $page $p
-        print "<li><a href=\"${REPLY}.html\">$p</a> <sup>${pages[$p]}</sup></li>"
+</pre>"
+    local bs=(${(oi)=${backs[$page]}})
+    if [[ $bs ]]; then
+        print "<hr/><ul>"
+        for p (${(oi)=${backs[$page]}}) {
+            getrel $page $p
+            print "<li><a href=\"${REPLY}.html\">$p</a> <sup>${pages[$p]}</sup></li>"
+        }
+        print "</ul>"
+    fi
+    print "</body></html>"
+}
+
+zmodload -F zsh/datetime b:strftime
+now=$(strftime "%F %T")
+
+genIndex() {
+    local p
+    local entries=()
+    typeset -aU cats=()
+    local curdepth=${#${(s:/:)1}}
+    (( curdepth = curdepth + 1 ))
+    for p (${(oi)${(k)pages[(I)$1*]}}) {
+        [[ $p =~ "/Index$" ]] && continue
+        case ${#${(As:/:)p}} in
+        ($curdepth) entries=($entries "[$p] (${pages[$p]})") ;;
+        ( $(( $curdepth + 1 )) ) cats=(${1}${${p#$1}%%/*} $cats) ;;
+        (*) continue ;;
+        esac
     }
-    print "</ul></body></html>"
+    if [[ $cats ]]; then
+        entries=($entries "------------------------ >8 ------------------------")
+        for p (${(oi)cats}) entries=($entries "[$p/Index]")
+    fi
+    genHTML ${1}Index ${(F)entries}
 }
 
 case $1 in
 (links) for w (${(oi)=${links[$2]}}) print $w ;;
 (backs) for w (${(oi)=${backs[$2]}}) print $w ;;
-(html) genhtml $2 ;;
+(html) genHTML $2 ;;
+(html-index) genIndex $2 ;;
 (htmls)
     for p (${(k)pages}) {
-        local subdir=$p:h
-        mkdir -p $2/$subdir
-        genhtml $p > $2/$p.html
+        mkdir -p $2/$p:h
+        genHTML $p > $2/$p.html
+        touch -r $p $2/$p.html
     }
-    local all=()
-    for p (${(Oi)${(k)pages}}) all=("$p (${pages[$p]})" $all)
-    zmodload -F zsh/datetime b:strftime
-    pages[ALL]=$(strftime "%F %T")
-    genhtml ALL ${(j:\n:)all} > $2/ALL.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
     ;;
 (*) usage ;;
 esac