]> Sergey Matveev's repositories - zk.zsh.git/commitdiff
Do not iterate over all pages during HTML rendering
authorSergey Matveev <stargrave@stargrave.org>
Sun, 13 Mar 2022 18:36:08 +0000 (21:36 +0300)
committerSergey Matveev <stargrave@stargrave.org>
Sun, 13 Mar 2022 18:36:08 +0000 (21:36 +0300)
zk.zsh

diff --git a/zk.zsh b/zk.zsh
index 348a5b521b2e066d581741ded0f5baf0fbf83dcf..eb05aff979a2a5a7796cb6a2698a62c787645e35 100755 (executable)
--- a/zk.zsh
+++ b/zk.zsh
@@ -1,4 +1,6 @@
 #!/usr/bin/env zsh
+# zk.zsh -- zettelkästen-related helper
+# 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
@@ -76,12 +74,14 @@ getrel() {
 genHTML() {
     local page=$1
     local data p
+    typeset -a _links
     [[ $# -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;}
+    [[ ( $page = "Index" ) || ( $page =~ "/Index" ) ]] \
+        && _links=(${(k)pages}) || _links=(${=links[$page]})
+    for p ($_links) {
         getrel $page $p
         data="${data//\[${p}\]/<a href=\"${REPLY}.html\">[$p]</a>}"
     }