]> Sergey Matveev's repositories - zk.zsh.git/blobdiff - zk.zsh
Sizes in indices
[zk.zsh.git] / zk.zsh
diff --git a/zk.zsh b/zk.zsh
index b6b640700c4a2ba95c9d86c5b28eefca731a72c3..7d5f556236057922836232c478666b97ef469435 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
@@ -8,9 +8,9 @@ usage() {
     cat >&2 <<EOF
 Usage:
   \$ $0:t links PAGE
-    Print the PAGE's links
+    Print PAGE's links
   \$ $0:t backs PAGE
-    Print who backlinks to the PAGE
+    Print PAGE's backlinks
   \$ $0:t htmls DIR
     Generate HTMLs in DIR
 EOF
@@ -19,34 +19,38 @@ EOF
 
 [[ $# -eq 2 ]] || usage
 
-# Collect all pages
 setopt GLOB_STAR_SHORT
 zmodload -F zsh/stat b:zstat
 typeset -A pages
+typeset -A sizes
 for p (**(.)) {
-    zstat -A reply -F "%F %T" +mtime $p
-    pages[$p]=${reply[1]}
+    zstat -A mtime -F "%F %T" +mtime $p
+    zstat -A size +size $p
+    pages[$p]=${mtime[1]}
+    sizes[$p]=${size[1]}
 }
 typeset -a cats
 for p (**(/)) cats=($p $cats)
 
-# Determine the links between them
+zmodload zsh/mapfile
+zmodload -F zsh/files b:zf_mkdir
 typeset -A links
 typeset -A backs
-typeset -aU words
+typeset -aU ws
 for p (${(k)pages}) {
-    words=()
-    for w (`< $p`) {
-        [[ $w =~ "\[(.*)\]" ]] || continue
+    ws=()
+    for w (${=mapfile[$p]}) {
+        [[ $w =~ "\[([^] ]+)\]" ]] || continue
         w=${match[1]}
         [[ ${pages[$w]} ]] || {
             [[ $ZK_SHOW_MISSING ]] && print "missing $w"
             continue
         }
-        words=($words $w)
+        ws=($ws $w)
     }
-    [[ $words ]] && links[$p]=${(j: :)words}
+    [[ $ws ]] && links[$p]=${(j: :)ws}
 }
+unset ws
 for p ws (${(kv)links}) {
     for w (${=ws}) backs[$w]="$p ${backs[$w]}"
 }
@@ -74,22 +78,31 @@ getrel() {
 genHTML() {
     local page=$1
     local data p
-    [[ $# -eq 1 ]] && data=`< $page` || data=$2
+    [[ $# -eq 1 ]] && data=${mapfile[$page]} || data=$2
     data=${data//&/&amp;}
     data=${data//</&lt;}
     data=${data//>/&gt;}
-    for p (${=links[$page]}) {
+    local _links=(${(oi)=links[$page]})
+    for p ($_links) {
         getrel $page $p
         data="${data//\[${p}\]/<a href=\"${REPLY}.html\">[$p]</a>}"
     }
-    print "<\!DOCTYPE html>
+    print -r "<\!DOCTYPE html>
 <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>"
         }
@@ -99,7 +112,7 @@ $data
 }
 
 zmodload -F zsh/datetime b:strftime
-now=$(strftime "%F %T")
+strftime -s now "%F %T"
 
 genIndex() {
     local p
@@ -116,9 +129,10 @@ genIndex() {
         (*) continue ;;
         esac
     }
-    for p (${(oi)_links}) entries=($entries "[$p] (${pages[$p]})")
+    for p (${(oi)_links}) \
+        entries=($entries "[$p] (${pages[$p]}) (${sizes[$p]} bytes)")
     if [[ $cats ]]; then
-        entries=($entries "\nBacklinks:\n")
+        entries=($entries "\nSubdirectories:\n")
         for p (${(oi)cats}) {
             entries=($entries "[$p/Index]")
             _links=($p/Index $_links)
@@ -135,7 +149,7 @@ case $1 in
 (html-index) genIndex $2 ;;
 (htmls)
     for p (${(k)pages}) {
-        mkdir -p $2/$p:h
+        zf_mkdir -p $2/$p:h
         genHTML $p > $2/$p.html
         touch -r $p $2/$p.html
     }