#!/usr/bin/env zsh # zk.zsh -- zettelkästen/wiki/static website helper/generator # Copyright (C) 2022-2024 Sergey Matveev set -e ZK_VERSION=ZKZSH1 usage() { >&2 <&2 exit 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]=1 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}) { [[ $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]} [[ ( $w =~ "/$" ) && ( ${cats[$w[1,-2]]} ) ]] && { ws=($ws $w) continue } [[ ${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[4,-1]}) } [[ $ws ]] && links[$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}} getrel() { # nearly the copy-paste of Functions/Misc/relative local dst=$2:a local src=$1:h:a local -a cur abs cur=(${(s:/:)src}) abs=(${(s:/:)dst:h} $dst:t) integer i=1 while [[ i -le $#abs && $abs[i] == $cur[i] ]] ; do ((++i > $#cur)) && { REPLY=${(j:/:)abs[i,-1]} return } done src=${(j:/:)cur[i,-1]/*/..} dst=${(j:/:)abs[i,-1]} REPLY=$src${dst:+/$dst} } genHTML() { local page=$1 local data p [[ $# -eq 1 ]] && data=${mapfile[$page]} || data=$2 local _links=(${(oi)=links[$page]}) if [[ ( ${cached[$page]} ) && ( -s $ZK_CACHE/${page}.html ) ]]; then < $ZK_CACHE/${page}.html else data=${data//&/&} data=${data///>} for p ($_links) { getrel $page $p [[ -d $p ]] && REPLY=$REPLY/index data="${data//\[${p}\]/[$p]}" } data=" $page (${pages[$page]})
$data
" if [[ $ZK_CACHE ]]; then print -r "$data" > $ZK_CACHE/${page}.html < $ZK_CACHE/${page}.html else print -r "$data" fi fi if [[ $_links ]]; then print "
Links:" fi local bs=(${(oi)=${backs[$page]}}) if [[ $bs ]]; then print "
Backlinks:" fi print "" } 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 (${(k)pages[(I)$1*]}) { case ${#${(As:/:)p}} in ($curdepth) _links=($p $_links) ;; ( $(( $curdepth + 1 )) ) _cats=(${1}${${p#$1}%%/*} $_cats) ;; (*) continue ;; esac } local page=${1}index print " $page ($now) " if [[ $_cats ]]; then print "
Subdirectories:" fi local bs=(${(oi)=${backs[$1]}}) if [[ $bs ]]; then print "
Backlinks:" fi print "" } case $1 in (links) for w (${(oi)=${links[$2]}}) print $w ;; (backs) for w (${(oi)=${backs[$2]}}) print $w ;; (html) genHTML $2 ;; (html-index) genIndex $2 ;; (htmls) for p (${(k)pages}) { zf_mkdir -p $2/$p:h genHTML $p > $2/$p.html touch -r $p $2/$p.html } for p (${(k)cats}) genIndex $p/ > $2/$p/index.html genIndex "" > $2/index.html for p ("" ${(k)cats}) touch -d ${now/ /T} $2/$p/index.html ;; (*) usage ;; esac