2 # zk.zsh -- zettelkästen/wiki/static website helper/generator
3 # Copyright (C) 2022 Sergey Matveev <stargrave@stargrave.org>
14 Print PAGE's backlinks
21 [[ $# -eq 2 ]] || usage
23 separator="------------------------ >8 ------------------------"
24 setopt GLOB_STAR_SHORT
25 zmodload -F zsh/stat b:zstat
29 [[ $p:t == "index" ]] && {
30 echo unacceptable filename: $p >&2
33 zstat -A mtime -F "%F %T" +mtime $p
34 zstat -A size +size $p
39 for p (**(/)) cats[$p]=1
42 zmodload -F zsh/files b:zf_mkdir
49 zstat -A inode +inode $p
50 zstat -A ctime +ctime $p
51 cache=(${(f)mapfile[$ZK_CACHE/$p]})
52 if [[ ( ${cache[1]} = $ZK_VERSION ) &&
53 ( ${cache[2]} = ${inode[1]} ) &&
54 ( ${cache[3]} = ${ctime[1]} ) ]]; then
56 [[ $ws ]] && links[$p]=${(j: :)ws}
62 for w (${=mapfile[$p]}) {
63 [[ $w =~ "\[([^] ]+)\]" ]] || continue
65 [[ ( $w =~ "/$" ) && ( ${cats[$w[1,-2]]} ) ]] && {
69 [[ ${pages[$w]} ]] || {
70 [[ $ZK_SHOW_MISSING ]] && print "missing $w"
76 zf_mkdir -p $ZK_CACHE/$p:h
77 ws=($ZK_VERSION ${inode[1]} ${ctime[1]} $ws)
78 print -l $ws > $ZK_CACHE/$p
81 [[ $ws ]] && links[$p]=${(j: :)ws}
84 for p ws (${(kv)links}) {
85 for w (${=ws}) backs[$w]="$p ${backs[$w]}"
87 for p ws (${(kv)backs}) backs[$p]=${(j: :)${(u)=ws}}
90 # nearly the copy-paste of Functions/Misc/relative
95 abs=(${(s:/:)dst:h} $dst:t)
97 while [[ i -le $#abs && $abs[i] == $cur[i] ]] ; do
99 REPLY=${(j:/:)abs[i,-1]}
103 src=${(j:/:)cur[i,-1]/*/..}
104 dst=${(j:/:)abs[i,-1]}
105 REPLY=$src${dst:+/$dst}
111 [[ $# -eq 1 ]] && data=${mapfile[$page]} || data=$2
112 local _links=(${(oi)=links[$page]})
113 if [[ ( ${cached[$page]} ) && ( -s $ZK_CACHE/${page}.html ) ]]; then
114 < $ZK_CACHE/${page}.html
116 data=${data//&/&}
121 [[ -d $p ]] && REPLY=$REPLY/index
122 data="${data//\[${p}\]/<a href=\"${REPLY}.html\">[$p]</a>}"
124 data="<!DOCTYPE html>
125 <html><head><title>$page (${pages[$page]})</title></head><body><pre>
127 if [[ $ZK_CACHE ]]; then
128 print -r "$data" > $ZK_CACHE/${page}.html
129 < $ZK_CACHE/${page}.html
134 if [[ $_links ]]; then
135 print "<hr/>Links:<ul>"
138 print "<li><a href=\"${REPLY}.html\">$p</a> <sup>${pages[$p]}</sup></li>"
142 local bs=(${(oi)=${backs[$page]}})
144 print "<hr/>Backlinks:<ul>"
147 print "<li><a href=\"${REPLY}.html\">$p</a> <sup>${pages[$p]}</sup></li>"
151 print "</body></html>"
154 zmodload -F zsh/datetime b:strftime
155 strftime -s now "%F %T"
162 local curdepth=${#${(s:/:)1}}
163 (( curdepth = curdepth + 1 ))
164 for p (${(k)pages[(I)$1*]}) {
165 case ${#${(As:/:)p}} in
166 ($curdepth) _links=($p $_links) ;;
167 ( $(( $curdepth + 1 )) ) _cats=(${1}${${p#$1}%%/*} $_cats) ;;
172 print "<!DOCTYPE html>
173 <html><head><title>$page ($now)</title></head><body><ul>"
174 for p (${(oi)_links}) {
176 print "<li><a href=\"${REPLY}.html\">$p</a> <sup>${pages[$p]}</sup> (${sizes[$p]} bytes)</li>"
179 if [[ $_cats ]]; then
180 print "<hr/>Subdirectories:<ul>"
181 for p (${(oi)_cats}) {
182 getrel $page $p/index
183 print "<li><a href=\"${REPLY}.html\">$p</a></li>"
187 local bs=(${(oi)=${backs[$1]}})
189 print "<hr/>Backlinks:<ul>"
192 print "<li><a href=\"${REPLY}.html\">$p</a> <sup>${pages[$p]}</sup></li>"
196 print "</body></html>"
200 (links) for w (${(oi)=${links[$2]}}) print $w ;;
201 (backs) for w (${(oi)=${backs[$2]}}) print $w ;;
203 (html-index) genIndex $2 ;;
205 for p (${(k)pages}) {
207 genHTML $p > $2/$p.html
208 touch -r $p $2/$p.html
210 for p (${(k)cats}) genIndex $p/ > $2/$p/index.html
211 genIndex "" > $2/index.html
212 for p ("" ${(k)cats}) touch -d ${now/ /T} $2/$p/index.html