#!/usr/bin/env zsh
+# zk.zsh -- zettelkästen/wiki/static website helper/generator
+# Copyright (C) 2022 Sergey Matveev <stargrave@stargrave.org>
set -e
# Collect all pages
setopt GLOB_STAR_SHORT
+zmodload -F zsh/stat b:zstat
typeset -A pages
-for f (**(.)) pages[$f]=1
+for p (**(.)) {
+ zstat -A reply -F "%F %T" +mtime $p
+ pages[$p]=${reply[1]}
+}
+typeset -a cats
+for p (**(/)) cats=($p $cats)
# Determine the links between them
typeset -A links
typeset -A backs
+typeset -aU words
for p (${(k)pages}) {
- for w (`cat $p`) {
- [[ $w =~ "^\(.*\)$" ]] && w=${MATCH[2,-2]}
- [[ ${pages[$w]} ]] || continue
- links[$p]="$w ${links[$p]}"
- }
-}
-
-# Deduplicate all references
-typeset -A ws
-for p (${(k)links}) {
- ws=()
- for w (${=${links[$p]}}) {
- ws[$w]=1
- backs[$w]="$p ${backs[$w]}"
+ words=()
+ for w (`< $p`) {
+ [[ $w =~ "\[(.*)\]" ]] || continue
+ w=${match[1]}
+ [[ ${pages[$w]} ]] || {
+ [[ $ZK_SHOW_MISSING ]] && print "missing $w"
+ continue
+ }
+ words=($words $w)
}
- links[$p]=${(k)ws}
+ [[ $words ]] && links[$p]=${(j: :)words}
}
-for p (${(k)backs}) {
- ws=()
- for w (${=${backs[$p]}}) ws[$w]=1
- backs[$p]=${(k)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() {
- local tgt=$2:a
- cd $1:h
- relative $tgt
+ # 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() {
+genHTML() {
local page=$1
- local data
- [[ $# -eq 1 ]] && data="`cat $page`" || data=$2
- local tgt
- data="${data//&/&}"
- data="${data//</<}"
- data="${data//>/>}"
- for p (${(k)pages}) {
- [[ $p = ALL ]] && continue
- [[ $p = index ]] && continue
- tgt=`getrel $page $p`
- data="${data//${p}/<a href=\"${tgt}.html\">$p</a>}"
+ local data p
+ [[ $# -eq 1 ]] && data=`< $page` || data=$2
+ data=${data//&/&}
+ data=${data//</<}
+ data=${data//>/>}
+ for p (${=links[$page]}) {
+ getrel $page $p
+ data="${data//\[${p}\]/<a href=\"${REPLY}.html\">[$p]</a>}"
}
print "<\!DOCTYPE html>
-<html><head><title>$page</title></head><body><pre>
+<html><head><title>$page (${pages[$page]})</title></head><body><pre>
$data
-</pre><hr/><ul>"
- for p (${(oi)=${backs[$page]}}) {
- [[ $p = ALL ]] && continue
- tgt=`getrel $page $p`
- print "<li><a href=\"${tgt}.html\">$p</a></li>"
+</pre>"
+ local bs=(${(oi)=${backs[$page]}})
+ if [[ $bs ]]; then
+ print "<hr/><ul>"
+ for p (${(oi)=${backs[$page]}}) {
+ 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
+now=$(strftime "%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]})")
+ if [[ $cats ]]; then
+ 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}
}
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
+ 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