]> Sergey Matveev's repositories - zk.zsh.git/blobdiff - zk.zsh
More strict regexp
[zk.zsh.git] / zk.zsh
diff --git a/zk.zsh b/zk.zsh
index eb05aff979a2a5a7796cb6a2698a62c787645e35..25a3036ec1fbfb53ba4964a81726b8d8ccc15cd9 100755 (executable)
--- a/zk.zsh
+++ b/zk.zsh
@@ -1,5 +1,5 @@
 #!/usr/bin/env zsh
-# zk.zsh -- zettelkästen-related helper
+# zk.zsh -- zettelkästen/wiki/static website helper/generator
 # Copyright (C) 2022 Sergey Matveev <stargrave@stargrave.org>
 
 set -e
@@ -19,7 +19,6 @@ EOF
 
 [[ $# -eq 2 ]] || usage
 
-# Collect all pages
 setopt GLOB_STAR_SHORT
 zmodload -F zsh/stat b:zstat
 typeset -A pages
@@ -30,14 +29,14 @@ for p (**(.)) {
 typeset -a cats
 for p (**(/)) cats=($p $cats)
 
-# Determine the links between them
+zmodload zsh/mapfile
 typeset -A links
 typeset -A backs
 typeset -aU words
 for p (${(k)pages}) {
     words=()
-    for w (`< $p`) {
-        [[ $w =~ "\[(.*)\]" ]] || continue
+    for w (${=mapfile[$p]}) {
+        [[ $w =~ "\[([^] ]+)\]" ]] || continue
         w=${match[1]}
         [[ ${pages[$w]} ]] || {
             [[ $ZK_SHOW_MISSING ]] && print "missing $w"
@@ -74,13 +73,11 @@ getrel() {
 genHTML() {
     local page=$1
     local data p
-    typeset -a _links
-    [[ $# -eq 1 ]] && data=`< $page` || data=$2
+    [[ $# -eq 1 ]] && data=${mapfile[$page]} || data=$2
     data=${data//&/&amp;}
     data=${data//</&lt;}
     data=${data//>/&gt;}
-    [[ ( $page = "Index" ) || ( $page =~ "/Index" ) ]] \
-        && _links=(${(k)pages}) || _links=(${=links[$page]})
+    local _links=(${(oi)=links[$page]})
     for p ($_links) {
         getrel $page $p
         data="${data//\[${p}\]/<a href=\"${REPLY}.html\">[$p]</a>}"
@@ -89,10 +86,18 @@ genHTML() {
 <html><head><title>$page (${pages[$page]})</title></head><body><pre>
 $data
 </pre>"
+    if [[ $_links ]]; then
+        print "<hr/>Links:<ul>"
+        for p ($_links) {
+            getrel $page $p
+            print "<li><a href=\"${REPLY}.html\">$p</a> <sup>${pages[$p]}</sup></li>"
+        }
+        print "</ul>"
+    fi
     local bs=(${(oi)=${backs[$page]}})
     if [[ $bs ]]; then
-        print "<hr/><ul>"
-        for p (${(oi)=${backs[$page]}}) {
+        print "<hr/>Backlinks:<ul>"
+        for p ($bs) {
             getrel $page $p
             print "<li><a href=\"${REPLY}.html\">$p</a> <sup>${pages[$p]}</sup></li>"
         }
@@ -102,26 +107,32 @@ $data
 }
 
 zmodload -F zsh/datetime b:strftime
-now=$(strftime "%F %T")
+strftime -s now "%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}
 }