#!/usr/bin/env zsh
+# zk.zsh -- zettelkästen/wiki/static website helper/generator
+# Copyright (C) 2022 Sergey Matveev <stargrave@stargrave.org>
set -e
+ZK_VERSION=ZKZSH1
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
[[ $# -eq 2 ]] || usage
-# Collect all pages
setopt GLOB_STAR_SHORT
+zmodload -F zsh/stat b:zstat
typeset -A pages
-for f (**(.)) pages[$f]=1
+typeset -A sizes
+for p (**(.)) {
+ 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 -A cached
+typeset -aU ws
for p (${(k)pages}) {
- for w (`< $p`) {
- [[ $w =~ "^[([{].*" ]] && w=${MATCH[2,-2]}
- [[ ${pages[$w]} ]] || continue
- links[$p]="$w ${links[$p]}"
+ [[ $ZK_CACHE ]] && {
+ zstat -A inode +inode $p
+ zstat -A ctime +ctime $p
+ cache=(${(f)mapfile[$ZK_CACHE/$p]})
+ if [[ ( ${cache[1]} = $ZK_VERSION ) &&
+ ( ${cache[2]} = ${inode[1]} ) &&
+ ( ${cache[3]} = ${ctime[1]} ) ]]; then
+ ws=(${cache[4,-1]})
+ [[ $ws ]] && links[$p]=${(j: :)ws}
+ cached[$p]=1
+ continue
+ fi
}
+ ws=()
+ for w (${=mapfile[$p]}) {
+ [[ $w =~ "\[([^] ]+)\]" ]] || continue
+ w=${match[1]}
+ [[ ${pages[$w]} ]] || {
+ [[ $ZK_SHOW_MISSING ]] && print "missing $w"
+ continue
+ }
+ ws=($ws $w)
+ }
+ [[ $ZK_CACHE ]] && {
+ zf_mkdir -p $ZK_CACHE/$p:h
+ ws=($ZK_VERSION ${inode[1]} ${ctime[1]} $ws)
+ print -l $ws > $ZK_CACHE/$p
+ ws=(${ws[3,-1]})
+ }
+ [[ $ws ]] && links[$p]=${(j: :)ws}
}
-
-# Deduplicate all references
-for p (${(k)links}) {
- local ws=(${(u)=links[$p]})
- links[$p]=${(j: :)ws}
- for w ($ws) backs[$w]="$p ${backs[$w]}"
-}
-for p (${(k)backs}) {
- local ws=(${(u)=backs[$p]})
- backs[$p]=${(j: :)ws}
+unset cache ws
+for p ws (${(kv)links}) {
+ for w (${=ws}) backs[$w]="$p ${backs[$w]}"
}
+for p ws (${(kv)backs}) backs[$p]=${(j: :)${(u)=ws}}
-autoload -U relative
getrel() {
# nearly the copy-paste of Functions/Misc/relative
local dst=$2:a
REPLY=$src${dst:+/$dst}
}
-genhtml() {
+genHTML() {
local page=$1
- local data
- [[ $# -eq 1 ]] && data=`< $page` || data=$2
- data="${data//&/&}"
- data="${data//</<}"
- data="${data//>/>}"
- for p (${(k)pages}) {
- [[ $p = index ]] && continue
- getrel $page $p
- data="${data//${p}/<a href=\"${REPLY}.html\">$p</a>}"
- }
- print "<\!DOCTYPE html>
-<html><head><title>$page</title></head><body><pre>
-$data
-</pre><hr/><ul>"
- for p (${(oi)=${backs[$page]}}) {
- getrel $page $p
- print "<li><a href=\"${REPLY}.html\">$p</a></li>"
+ local data p
+ [[ $# -eq 1 ]] && data=${mapfile[$page]} || data=$2
+ local _links=(${(oi)=links[$page]})
+ if [[ ( ${cached[$page]} ) && ( -s $ZK_CACHE/${page}.html ) ]]; then
+ cat $ZK_CACHE/${page}.html
+ else
+ data=${data//&/&}
+ data=${data//</<}
+ data=${data//>/>}
+ for p ($_links) {
+ getrel $page $p
+ data="${data//\[${p}\]/<a href=\"${REPLY}.html\">[$p]</a>}"
+ }
+ data="<!DOCTYPE html>
+<html><head><title>$page (${pages[$page]})</title></head><body><pre>
+$data</pre>"
+ if [[ $ZK_CACHE ]]; then
+ print -r "$data" > $ZK_CACHE/${page}.html
+ cat $ZK_CACHE/${page}.html
+ else
+ print -r "$data"
+ fi
+ fi
+ 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/>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>"
+}
+
+zmodload -F zsh/datetime b:strftime
+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) _links=($p $_links) ;;
+ ( $(( $curdepth + 1 )) ) cats=(${1}${${p#$1}%%/*} $cats) ;;
+ (*) continue ;;
+ esac
}
- print "</ul></body></html>"
+ for p (${(oi)_links}) \
+ entries=($entries "[$p] (${pages[$p]}) (${sizes[$p]} bytes)")
+ if [[ $cats ]]; then
+ entries=($entries " " "Subdirectories:" " ")
+ for p (${(oi)cats}) {
+ entries=($entries "[$p/Index]")
+ _links=($p/Index $_links)
+ }
+ fi
+ links[${1}Index]=${(j: :)_links}
+ genHTML ${1}Index ${(F)entries}
}
case $1 in
(links) for w (${(oi)=${links[$2]}}) print $w ;;
(backs) for w (${(oi)=${backs[$2]}}) print $w ;;
-(html) genhtml $2 ;;
+(html) genHTML $2 ;;
+(html-index) genIndex $2 ;;
(htmls)
for p (${(k)pages}) {
- local subdir=$p:h
- mkdir -p $2/$subdir
- genhtml $p > $2/$p.html
+ zf_mkdir -p $2/$p:h
+ genHTML $p > $2/$p.html
+ touch -r $p $2/$p.html
}
- local all=""
- for p (${(Oi)${(k)pages}}) all="$p\n$all"
- genhtml ALL $all > $2/ALL.html
+ for p ($cats) pages[${p}/Index]=$now
+ pages[Index]=$now
+ for p ($cats) genIndex $p/ > $2/$p/Index.html
+ genIndex "" > $2/Index.html
+ for p ("" $cats) touch -d ${now/ /T} $2/$p/Index.html
;;
(*) usage ;;
esac