]> Sergey Matveev's repositories - zk.zsh.git/blobdiff - zk.zsh
Simpler deduplication
[zk.zsh.git] / zk.zsh
diff --git a/zk.zsh b/zk.zsh
index 37e7b63c92949c574c9ee3f921a2bca139addf4a..5326cb08b0f3e5d18cc6adf7807f24a14395d99a 100755 (executable)
--- a/zk.zsh
+++ b/zk.zsh
@@ -27,31 +27,41 @@ typeset -A links
 typeset -A backs
 for p (${(k)pages}) {
     for w (`cat $p`) {
-        [[ $w =~ "^\(.*\)$" ]] && w=${MATCH[2,-2]}
+        [[ $w =~ "^[([{].*" ]] && w=${MATCH[2,-2]}
         [[ ${pages[$w]} ]] || continue
         links[$p]="$w ${links[$p]}"
     }
 }
 
 # Deduplicate all references
-typeset -A ws
 for p (${(k)links}) {
-    ws=()
-    for w (${=${links[$p]}}) {
-        ws[$w]=1
-        backs[$w]="$p ${backs[$w]}"
-    }
-    links[$p]=${(k)ws}
+    local ws=(${(u)=links[$p]})
+    links[$p]=${(j: :)ws}
+    for w ($ws) backs[$w]="$p ${backs[$w]}"
 }
 for p (${(k)backs}) {
-    ws=()
-    for w (${=${backs[$p]}}) ws[$w]=1
-    backs[$p]=${(k)ws}
+    local ws=(${(u)=backs[$p]})
+    backs[$p]=${(j: :)ws}
 }
 
 autoload -U relative
 getrel() {
-    relative $2:a $1:h
+    # nearly the copy-paste of Functions/Misc/relative
+    local dst=$2:a
+    local src=$1:h:a
+    local -a cur abs
+    cur=(${(s:/:)src})
+    abs=(${(s:/:)dst:h} $dst:t)
+    integer i=1
+    while [[ i -le $#abs && $abs[i] == $cur[i] ]] ; do
+        ((++i > $#cur)) && {
+            REPLY=${(j:/:)abs[i,-1]}
+            return
+        }
+    done
+    src=${(j:/:)cur[i,-1]/*/..}
+    dst=${(j:/:)abs[i,-1]}
+    REPLY=$src${dst:+/$dst}
 }
 
 genhtml() {