]> Sergey Matveev's repositories - zk.zsh.git/blobdiff - zk.zsh
Nothing prevents using "index" for convenience
[zk.zsh.git] / zk.zsh
diff --git a/zk.zsh b/zk.zsh
index 25a3036ec1fbfb53ba4964a81726b8d8ccc15cd9..9b2d0a5426a106855aed0bf6eb8b75ca43292d99 100755 (executable)
--- a/zk.zsh
+++ b/zk.zsh
@@ -3,14 +3,15 @@
 # Copyright (C) 2022 Sergey Matveev <stargrave@stargrave.org>
 
 set -e
+ZK_VERSION=ZKZSH1
 
 usage() {
     cat >&2 <<EOF
 Usage:
   \$ $0:t links PAGE
-    Print the PAGE's links
+    Print PAGE's links
   \$ $0:t backs PAGE
-    Print who backlinks to the PAGE
+    Print PAGE's backlinks
   \$ $0:t htmls DIR
     Generate HTMLs in DIR
 EOF
@@ -22,19 +23,37 @@ EOF
 setopt GLOB_STAR_SHORT
 zmodload -F zsh/stat b:zstat
 typeset -A pages
+typeset -A sizes
 for p (**(.)) {
-    zstat -A reply -F "%F %T" +mtime $p
-    pages[$p]=${reply[1]}
+    zstat -A mtime -F "%F %T" +mtime $p
+    zstat -A size +size $p
+    pages[$p]=${mtime[1]}
+    sizes[$p]=${size[1]}
 }
 typeset -a cats
 for p (**(/)) cats=($p $cats)
 
 zmodload zsh/mapfile
+zmodload -F zsh/files b:zf_mkdir
 typeset -A links
 typeset -A backs
-typeset -aU words
+typeset -A cached
+typeset -aU ws
 for p (${(k)pages}) {
-    words=()
+    [[ $ZK_CACHE ]] && {
+        zstat -A inode +inode $p
+        zstat -A ctime +ctime $p
+        cache=(${(f)mapfile[$ZK_CACHE/$p]})
+        if [[ ( ${cache[1]} = $ZK_VERSION ) &&
+              ( ${cache[2]} = ${inode[1]} ) &&
+              ( ${cache[3]} = ${ctime[1]} ) ]]; then
+            ws=(${cache[4,-1]})
+            [[ $ws ]] && links[$p]=${(j: :)ws}
+            cached[$p]=1
+            continue
+        fi
+    }
+    ws=()
     for w (${=mapfile[$p]}) {
         [[ $w =~ "\[([^] ]+)\]" ]] || continue
         w=${match[1]}
@@ -42,10 +61,17 @@ for p (${(k)pages}) {
             [[ $ZK_SHOW_MISSING ]] && print "missing $w"
             continue
         }
-        words=($words $w)
+        ws=($ws $w)
     }
-    [[ $words ]] && links[$p]=${(j: :)words}
+    [[ $ZK_CACHE ]] && {
+        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 ]] && links[$p]=${(j: :)ws}
 }
+unset cache ws
 for p ws (${(kv)links}) {
     for w (${=ws}) backs[$w]="$p ${backs[$w]}"
 }
@@ -74,18 +100,27 @@ genHTML() {
     local page=$1
     local data p
     [[ $# -eq 1 ]] && data=${mapfile[$page]} || data=$2
-    data=${data//&/&amp;}
-    data=${data//</&lt;}
-    data=${data//>/&gt;}
     local _links=(${(oi)=links[$page]})
-    for p ($_links) {
-        getrel $page $p
-        data="${data//\[${p}\]/<a href=\"${REPLY}.html\">[$p]</a>}"
-    }
-    print "<\!DOCTYPE html>
+    if [[ ( ${cached[$page]} ) && ( -s $ZK_CACHE/${page}.html ) ]]; then
+        cat $ZK_CACHE/${page}.html
+    else
+        data=${data//&/&amp;}
+        data=${data//</&lt;}
+        data=${data//>/&gt;}
+        for p ($_links) {
+            getrel $page $p
+            data="${data//\[${p}\]/<a href=\"${REPLY}.html\">[$p]</a>}"
+        }
+        data="<!DOCTYPE html>
 <html><head><title>$page (${pages[$page]})</title></head><body><pre>
-$data
-</pre>"
+$data</pre>"
+        if [[ $ZK_CACHE ]]; then
+            print -r "$data" > $ZK_CACHE/${page}.html
+            cat $ZK_CACHE/${page}.html
+        else
+            print -r "$data"
+        fi
+    fi
     if [[ $_links ]]; then
         print "<hr/>Links:<ul>"
         for p ($_links) {
@@ -117,23 +152,24 @@ genIndex() {
     local curdepth=${#${(s:/:)1}}
     (( curdepth = curdepth + 1 ))
     for p (${(oi)${(k)pages[(I)$1*]}}) {
-        [[ $p =~ "/Index$" ]] && continue
+        [[ ( $p =~ "/index$" ) || ( $p = "index" ) ]] && continue
         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]})")
+    for p (${(oi)_links}) \
+        entries=($entries "[$p] (${pages[$p]}) (${sizes[$p]} bytes)")
     if [[ $cats ]]; then
-        entries=($entries "\nSubdirectories:\n")
+        entries=($entries " " "Subdirectories:" " ")
         for p (${(oi)cats}) {
-            entries=($entries "[$p/Index]")
-            _links=($p/Index $_links)
+            entries=($entries "[$p/index]")
+            _links=($p/index $_links)
         }
     fi
-    links[${1}Index]=${(j: :)_links}
-    genHTML ${1}Index ${(F)entries}
+    links[${1}index]=${(j: :)_links}
+    genHTML ${1}index ${(F)entries}
 }
 
 case $1 in
@@ -143,15 +179,15 @@ case $1 in
 (html-index) genIndex $2 ;;
 (htmls)
     for p (${(k)pages}) {
-        mkdir -p $2/$p:h
+        zf_mkdir -p $2/$p:h
         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
+    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