# 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]}
+}
# Determine the links between them
typeset -A links
typeset -A backs
for p (${(k)pages}) {
- for w (`cat $p`) {
- [[ $w =~ "^\(.*\)$" ]] && w=${MATCH[2,-2]}
+ for w (`< $p`) {
+ [[ $w =~ "\[(.*)\]" ]] || continue
+ w=${match[1]}
[[ ${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]}"
- }
- links[$p]=${(k)ws}
+for p w (${(kv)links}) {
+ local ws=(${(u)=w})
+ links[$p]=${(j: :)ws}
+ for w ($ws) backs[$w]="$p ${backs[$w]}"
}
-for p (${(k)backs}) {
- ws=()
- for w (${=${backs[$p]}}) ws[$w]=1
- backs[$p]=${(k)ws}
+for p w (${(kv)backs}) {
+ local ws=(${(u)=w})
+ backs[$p]=${(j: :)ws}
}
autoload -U relative
getrel() {
- relative $2:a $1:h
+ # 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
- [[ $# -eq 1 ]] && data="`cat $page`" || data=$2
- local tgt
+ [[ $# -eq 1 ]] && data=`< $page` || data=$2
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>}"
+ 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>"
+ getrel $page $p
+ print "<li><a href=\"${REPLY}.html\">$p</a> <sup>${pages[$p]}</sup></li>"
}
print "</ul></body></html>"
}
mkdir -p $2/$subdir
genhtml $p > $2/$p.html
}
- local all=""
- for p (${(Oi)${(k)pages}}) all="$p\n$all"
- genhtml ALL $all > $2/ALL.html
+ local all=()
+ for p (${(Oi)${(k)pages}}) all=("$p (${pages[$p]})" $all)
+ zmodload -F zsh/datetime b:strftime
+ pages[ALL]=$(strftime "%F %T")
+ genhtml ALL ${(j:\n:)all} > $2/ALL.html
;;
(*) usage ;;
esac