]> Sergey Matveev's repositories - zk.zsh.git/blobdiff - zk.zsh
Backlinks require square brackets now
[zk.zsh.git] / zk.zsh
diff --git a/zk.zsh b/zk.zsh
index 08a2f082654ca4a26336fa5666dd7bc3335079ba..81e032118715b6c02f0188f0e82948754c602504 100755 (executable)
--- a/zk.zsh
+++ b/zk.zsh
@@ -19,32 +19,39 @@ EOF
 
 # Collect all pages
 setopt GLOB_STAR_SHORT
+zmodload -F zsh/stat b:zstat
 typeset -A pages
-for f (**(.)) pages[$f]=1
+for p (**(.)) {
+    zstat -A reply -F "%F %T" +mtime $p
+    pages[$p]=${reply[1]}
+}
 
 # Determine the links between them
 typeset -A links
 typeset -A backs
 for p (${(k)pages}) {
     for w (`< $p`) {
-        [[ $w =~ "^[([{].*" ]] && w=${MATCH[2,-2]}
-        [[ ${pages[$w]} ]] || continue
+        [[ $w =~ "\[(.*)\]" ]] || continue
+        w=${match[1]}
+        [[ ${pages[$w]} ]] || {
+            [[ $ZK_SHOW_MISSING ]] && print "Missing $w"
+            continue
+        }
         links[$p]="$w ${links[$p]}"
     }
 }
 
 # Deduplicate all references
-for p (${(k)links}) {
-    local ws=(${(u)=links[$p]})
+for p w (${(kv)links}) {
+    local ws=(${(u)=w})
     links[$p]=${(j: :)ws}
     for w ($ws) backs[$w]="$p ${backs[$w]}"
 }
-for p (${(k)backs}) {
-    local ws=(${(u)=backs[$p]})
+for p w (${(kv)backs}) {
+    local ws=(${(u)=w})
     backs[$p]=${(j: :)ws}
 }
 
-autoload -U relative
 getrel() {
     # nearly the copy-paste of Functions/Misc/relative
     local dst=$2:a
@@ -66,7 +73,7 @@ getrel() {
 
 genhtml() {
     local page=$1
-    local data
+    local data p
     [[ $# -eq 1 ]] && data=`< $page` || data=$2
     data="${data//&/&amp;}"
     data="${data//</&lt;}"
@@ -74,15 +81,15 @@ genhtml() {
     for p (${(k)pages}) {
         [[ $p = index ]] && continue
         getrel $page $p
-        data="${data//${p}/<a href=\"${REPLY}.html\">$p</a>}"
+        data="${data//\[${p}\]/<a href=\"${REPLY}.html\">[$p]</a>}"
     }
     print "<\!DOCTYPE html>
-<html><head><title>$page</title></head><body><pre>
+<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></li>"
+        print "<li><a href=\"${REPLY}.html\">$p</a> <sup>${pages[$p]}</sup></li>"
     }
     print "</ul></body></html>"
 }
@@ -97,9 +104,11 @@ case $1 in
         mkdir -p $2/$subdir
         genhtml $p > $2/$p.html
     }
-    local all=""
-    for p (${(Oi)${(k)pages}}) all="$p\n$all"
-    genhtml ALL $all > $2/ALL.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
     ;;
 (*) usage ;;
 esac