]> Sergey Matveev's repositories - zk.zsh.git/blobdiff - zk.zsh
Caching
[zk.zsh.git] / zk.zsh
diff --git a/zk.zsh b/zk.zsh
index 7d5f556236057922836232c478666b97ef469435..a4984cc0fa3731550e40388b537bc812fb762dc4 100755 (executable)
--- a/zk.zsh
+++ b/zk.zsh
@@ -3,6 +3,7 @@
 # Copyright (C) 2022 Sergey Matveev <stargrave@stargrave.org>
 
 set -e
+ZK_VERSION=ZKZSH1
 
 usage() {
     cat >&2 <<EOF
@@ -36,8 +37,22 @@ zmodload zsh/mapfile
 zmodload -F zsh/files b:zf_mkdir
 typeset -A links
 typeset -A backs
+typeset -A cached
 typeset -aU ws
 for p (${(k)pages}) {
+    [[ $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
@@ -48,9 +63,15 @@ for p (${(k)pages}) {
         }
         ws=($ws $w)
     }
+    [[ $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 ws
+unset cache ws
 for p ws (${(kv)links}) {
     for w (${=ws}) backs[$w]="$p ${backs[$w]}"
 }
@@ -79,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 -r "<\!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) {
@@ -132,7 +162,7 @@ genIndex() {
     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)