]> Sergey Matveev's repositories - zk.zsh.git/blobdiff - zk.zsh
Raise copyright years
[zk.zsh.git] / zk.zsh
diff --git a/zk.zsh b/zk.zsh
index d583ae3d5d3dc5a990578bad5599916e9605a347..9b65aecc25db5e0c1709f2717b8651bd94a46d5c 100755 (executable)
--- a/zk.zsh
+++ b/zk.zsh
@@ -1,6 +1,6 @@
 #!/usr/bin/env zsh
 # zk.zsh -- zettelkästen/wiki/static website helper/generator
-# Copyright (C) 2022 Sergey Matveev <stargrave@stargrave.org>
+# Copyright (C) 2022-2024 Sergey Matveev <stargrave@stargrave.org>
 
 set -e
 ZK_VERSION=ZKZSH1
@@ -35,8 +35,8 @@ for p (**(.)) {
     pages[$p]=${mtime[1]}
     sizes[$p]=${size[1]}
 }
-typeset -a cats
-for p (**(/)) cats=($p $cats)
+typeset -A cats
+for p (**(/)) cats[$p]=1
 
 zmodload zsh/mapfile
 zmodload -F zsh/files b:zf_mkdir
@@ -62,6 +62,10 @@ for p (${(k)pages}) {
     for w (${=mapfile[$p]}) {
         [[ $w =~ "\[([^] ]+)\]" ]] || continue
         w=${match[1]}
+        [[ ( $w =~ "/$" ) && ( ${cats[$w[1,-2]]} ) ]] && {
+            ws=($ws $w)
+            continue
+        }
         [[ ${pages[$w]} ]] || {
             [[ $ZK_SHOW_MISSING ]] && print "missing $w"
             continue
@@ -114,10 +118,14 @@ genHTML() {
         data=${data//>/&gt;}
         for p ($_links) {
             getrel $page $p
+            [[ -d $p ]] && REPLY=$REPLY/index
             data="${data//\[${p}\]/<a href=\"${REPLY}.html\">[$p]</a>}"
         }
         data="<!DOCTYPE html>
-<html><head><title>$page (${pages[$page]})</title></head><body><pre>
+<html><head>
+<title>$page (${pages[$page]})</title>
+<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\">
+</head><body><pre>
 $data</pre>"
         if [[ $ZK_CACHE ]]; then
             print -r "$data" > $ZK_CACHE/${page}.html
@@ -153,28 +161,45 @@ genIndex() {
     local p
     local entries=()
     local _links=()
-    typeset -aU cats=()
+    typeset -aU _cats=()
     local curdepth=${#${(s:/:)1}}
     (( curdepth = curdepth + 1 ))
-    for p (${(oi)${(k)pages[(I)$1*]}}) {
-        [[ ( $p =~ "/index$" ) || ( $p = "index" ) ]] && continue
+    for p (${(k)pages[(I)$1*]}) {
         case ${#${(As:/:)p}} in
         ($curdepth) _links=($p $_links) ;;
-        ( $(( $curdepth + 1 )) ) cats=(${1}${${p#$1}%%/*} $cats) ;;
+        ( $(( $curdepth + 1 )) ) _cats=(${1}${${p#$1}%%/*} $_cats) ;;
         (*) continue ;;
         esac
     }
-    for p (${(oi)_links}) \
-        entries=($entries "[$p] (${pages[$p]}) (${sizes[$p]} bytes)")
-    if [[ $cats ]]; then
-        entries=($entries $separator "Subdirectories:" $separator)
-        for p (${(oi)cats}) {
-            entries=($entries "[$p/index]")
-            _links=($p/index $_links)
+    local page=${1}index
+    print "<!DOCTYPE html>
+<html><head>
+<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\">
+<title>$page ($now)</title>
+</head><body><ul>"
+    for p (${(oi)_links}) {
+        getrel $page $p
+        print "<li><a href=\"${REPLY}.html\">$p</a> <sup>${pages[$p]}</sup> (${sizes[$p]} bytes)</li>"
+    }
+    print "</ul>"
+    if [[ $_cats ]]; then
+        print "<hr/>Subdirectories:<ul>"
+        for p (${(oi)_cats}) {
+            getrel $page $p/index
+            print "<li><a href=\"${REPLY}.html\">$p</a></li>"
         }
+        print "</ul>"
     fi
-    links[${1}index]=${(j: :)_links}
-    genHTML ${1}index ${(F)entries}
+    local bs=(${(oi)=${backs[$1]}})
+    if [[ $bs ]]; then
+        print "<hr/>Backlinks:<ul>"
+        for p ($bs) {
+            getrel $page $p
+            print "<li><a href=\"${REPLY}.html\">$p</a> <sup>${pages[$p]}</sup></li>"
+        }
+        print "</ul>"
+    fi
+    print "</body></html>"
 }
 
 case $1 in
@@ -188,11 +213,9 @@ case $1 in
         genHTML $p > $2/$p.html
         touch -r $p $2/$p.html
     }
-    for p ($cats) pages[${p}/index]=$now
-    pages[index]=$now
-    for p ($cats) genIndex $p/ > $2/$p/index.html
+    for p (${(k)cats}) genIndex $p/ > $2/$p/index.html
     genIndex "" > $2/index.html
-    for p ("" $cats) touch -d ${now/ /T} $2/$p/index.html
+    for p ("" ${(k)cats}) touch -d ${now/ /T} $2/$p/index.html
     ;;
 (*) usage ;;
 esac