2 # zk.zsh -- zettelkästen-related helper
3 # Copyright (C) 2022 Sergey Matveev <stargrave@stargrave.org>
11 Print the PAGE's links
13 Print who backlinks to the PAGE
20 [[ $# -eq 2 ]] || usage
23 setopt GLOB_STAR_SHORT
24 zmodload -F zsh/stat b:zstat
27 zstat -A reply -F "%F %T" +mtime $p
31 for p (**(/)) cats=($p $cats)
33 # Determine the links between them
40 [[ $w =~ "\[(.*)\]" ]] || continue
42 [[ ${pages[$w]} ]] || {
43 [[ $ZK_SHOW_MISSING ]] && print "missing $w"
48 [[ $words ]] && links[$p]=${(j: :)words}
50 for p ws (${(kv)links}) {
51 for w (${=ws}) backs[$w]="$p ${backs[$w]}"
53 for p ws (${(kv)backs}) backs[$p]=${(j: :)${(u)=ws}}
56 # nearly the copy-paste of Functions/Misc/relative
61 abs=(${(s:/:)dst:h} $dst:t)
63 while [[ i -le $#abs && $abs[i] == $cur[i] ]] ; do
65 REPLY=${(j:/:)abs[i,-1]}
69 src=${(j:/:)cur[i,-1]/*/..}
70 dst=${(j:/:)abs[i,-1]}
71 REPLY=$src${dst:+/$dst}
78 [[ $# -eq 1 ]] && data=`< $page` || data=$2
82 [[ ( $page = "Index" ) || ( $page =~ "/Index" ) ]] \
83 && _links=(${(k)pages}) || _links=(${=links[$page]})
86 data="${data//\[${p}\]/<a href=\"${REPLY}.html\">[$p]</a>}"
88 print "<\!DOCTYPE html>
89 <html><head><title>$page (${pages[$page]})</title></head><body><pre>
92 local bs=(${(oi)=${backs[$page]}})
95 for p (${(oi)=${backs[$page]}}) {
97 print "<li><a href=\"${REPLY}.html\">$p</a> <sup>${pages[$p]}</sup></li>"
101 print "</body></html>"
104 zmodload -F zsh/datetime b:strftime
105 now=$(strftime "%F %T")
111 local curdepth=${#${(s:/:)1}}
112 (( curdepth = curdepth + 1 ))
113 for p (${(oi)${(k)pages[(I)$1*]}}) {
114 [[ $p =~ "/Index$" ]] && continue
115 case ${#${(As:/:)p}} in
116 ($curdepth) entries=($entries "[$p] (${pages[$p]})") ;;
117 ( $(( $curdepth + 1 )) ) cats=(${1}${${p#$1}%%/*} $cats) ;;
122 entries=($entries "------------------------ >8 ------------------------")
123 for p (${(oi)cats}) entries=($entries "[$p/Index]")
125 genHTML ${1}Index ${(F)entries}
129 (links) for w (${(oi)=${links[$2]}}) print $w ;;
130 (backs) for w (${(oi)=${backs[$2]}}) print $w ;;
132 (html-index) genIndex $2 ;;
134 for p (${(k)pages}) {
136 genHTML $p > $2/$p.html
137 touch -r $p $2/$p.html
139 for p ($cats) pages[${p}/Index]=$now
141 for p ($cats) genIndex $p/ > $2/$p/Index.html
142 genIndex "" > $2/Index.html
143 for p ("" $cats) touch -d ${now/ /T} $2/$p/Index.html