]> Sergey Matveev's repositories - zk.zsh.git/blobdiff - zk.zsh
Typo
[zk.zsh.git] / zk.zsh
diff --git a/zk.zsh b/zk.zsh
index 348a5b521b2e066d581741ded0f5baf0fbf83dcf..94653b1965db5edfa3a39cc8060f7fc6e7a84085 100755 (executable)
--- a/zk.zsh
+++ b/zk.zsh
@@ -1,4 +1,6 @@
 #!/usr/bin/env zsh
+# zk.zsh -- zettelkästen/wiki/static website helper/generator
+# Copyright (C) 2022 Sergey Matveev <stargrave@stargrave.org>
 
 set -e
 
@@ -31,28 +33,24 @@ for p (**(/)) cats=($p $cats)
 # Determine the links between them
 typeset -A links
 typeset -A backs
+typeset -aU words
 for p (${(k)pages}) {
+    words=()
     for w (`< $p`) {
         [[ $w =~ "\[(.*)\]" ]] || continue
         w=${match[1]}
         [[ ${pages[$w]} ]] || {
-            [[ $ZK_SHOW_MISSING ]] && print "Missing $w"
+            [[ $ZK_SHOW_MISSING ]] && print "missing $w"
             continue
         }
-        links[$p]="$w ${links[$p]}"
+        words=($words $w)
     }
+    [[ $words ]] && links[$p]=${(j: :)words}
 }
-
-# Deduplicate all references
-for p w (${(kv)links}) {
-    local ws=(${(u)=w})
-    links[$p]=${(j: :)ws}
-    for w ($ws) backs[$w]="$p ${backs[$w]}"
-}
-for p w (${(kv)backs}) {
-    local ws=(${(u)=w})
-    backs[$p]=${(j: :)ws}
+for p ws (${(kv)links}) {
+    for w (${=ws}) backs[$w]="$p ${backs[$w]}"
 }
+for p ws (${(kv)backs}) backs[$p]=${(j: :)${(u)=ws}}
 
 getrel() {
     # nearly the copy-paste of Functions/Misc/relative
@@ -77,11 +75,10 @@ genHTML() {
     local page=$1
     local data p
     [[ $# -eq 1 ]] && data=`< $page` || data=$2
-    data="${data//&/&amp;}"
-    data="${data//</&lt;}"
-    data="${data//>/&gt;}"
-    for p (${(k)pages}) {
-        [[ $p = index ]] && continue
+    data=${data//&/&amp;}
+    data=${data//</&lt;}
+    data=${data//>/&gt;}
+    for p (${=links[$page]}) {
         getrel $page $p
         data="${data//\[${p}\]/<a href=\"${REPLY}.html\">[$p]</a>}"
     }
@@ -107,21 +104,27 @@ now=$(strftime "%F %T")
 genIndex() {
     local p
     local entries=()
+    local _links=()
     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) _links=($p $_links) ;;
         ( $(( $curdepth + 1 )) ) cats=(${1}${${p#$1}%%/*} $cats) ;;
         (*) continue ;;
         esac
     }
+    for p (${(oi)_links}) entries=($entries "[$p] (${pages[$p]})")
     if [[ $cats ]]; then
-        entries=($entries "------------------------ >8 ------------------------")
-        for p (${(oi)cats}) entries=($entries "[$p/Index]")
+        entries=($entries "\nSubdirectories:\n")
+        for p (${(oi)cats}) {
+            entries=($entries "[$p/Index]")
+            _links=($p/Index $_links)
+        }
     fi
+    links[${1}Index]=${(j: :)_links}
     genHTML ${1}Index ${(F)entries}
 }