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}
77 [[ $# -eq 1 ]] && data=`< $page` || data=$2
81 for p (${=links[$page]}) {
83 data="${data//\[${p}\]/<a href=\"${REPLY}.html\">[$p]</a>}"
85 print "<\!DOCTYPE html>
86 <html><head><title>$page (${pages[$page]})</title></head><body><pre>
89 local bs=(${(oi)=${backs[$page]}})
92 for p (${(oi)=${backs[$page]}}) {
94 print "<li><a href=\"${REPLY}.html\">$p</a> <sup>${pages[$p]}</sup></li>"
98 print "</body></html>"
101 zmodload -F zsh/datetime b:strftime
102 now=$(strftime "%F %T")
109 local curdepth=${#${(s:/:)1}}
110 (( curdepth = curdepth + 1 ))
111 for p (${(oi)${(k)pages[(I)$1*]}}) {
112 [[ $p =~ "/Index$" ]] && continue
113 case ${#${(As:/:)p}} in
114 ($curdepth) _links=($p $_links) ;;
115 ( $(( $curdepth + 1 )) ) cats=(${1}${${p#$1}%%/*} $cats) ;;
119 for p (${(oi)_links}) entries=($entries "[$p] (${pages[$p]})")
121 entries=($entries "\nBacklinks:\n")
122 for p (${(oi)cats}) {
123 entries=($entries "[$p/Index]")
124 _links=($p/Index $_links)
127 links[${1}Index]=${(j: :)_links}
128 genHTML ${1}Index ${(F)entries}
132 (links) for w (${(oi)=${links[$2]}}) print $w ;;
133 (backs) for w (${(oi)=${backs[$2]}}) print $w ;;
135 (html-index) genIndex $2 ;;
137 for p (${(k)pages}) {
139 genHTML $p > $2/$p.html
140 touch -r $p $2/$p.html
142 for p ($cats) pages[${p}/Index]=$now
144 for p ($cats) genIndex $p/ > $2/$p/Index.html
145 genIndex "" > $2/Index.html
146 for p ("" $cats) touch -d ${now/ /T} $2/$p/Index.html