2 # zk.zsh -- zettelkästen/wiki/static website helper/generator
3 # Copyright (C) 2022-2023 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>
126 <title>$page (${pages[$page]})</title>
127 <meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\">
130 if [[ $ZK_CACHE ]]; then
131 print -r "$data" > $ZK_CACHE/${page}.html
132 < $ZK_CACHE/${page}.html
137 if [[ $_links ]]; then
138 print "<hr/>Links:<ul>"
141 print "<li><a href=\"${REPLY}.html\">$p</a> <sup>${pages[$p]}</sup></li>"
145 local bs=(${(oi)=${backs[$page]}})
147 print "<hr/>Backlinks:<ul>"
150 print "<li><a href=\"${REPLY}.html\">$p</a> <sup>${pages[$p]}</sup></li>"
154 print "</body></html>"
157 zmodload -F zsh/datetime b:strftime
158 strftime -s now "%F %T"
165 local curdepth=${#${(s:/:)1}}
166 (( curdepth = curdepth + 1 ))
167 for p (${(k)pages[(I)$1*]}) {
168 case ${#${(As:/:)p}} in
169 ($curdepth) _links=($p $_links) ;;
170 ( $(( $curdepth + 1 )) ) _cats=(${1}${${p#$1}%%/*} $_cats) ;;
175 print "<!DOCTYPE html>
177 <meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\">
178 <title>$page ($now)</title>
180 for p (${(oi)_links}) {
182 print "<li><a href=\"${REPLY}.html\">$p</a> <sup>${pages[$p]}</sup> (${sizes[$p]} bytes)</li>"
185 if [[ $_cats ]]; then
186 print "<hr/>Subdirectories:<ul>"
187 for p (${(oi)_cats}) {
188 getrel $page $p/index
189 print "<li><a href=\"${REPLY}.html\">$p</a></li>"
193 local bs=(${(oi)=${backs[$1]}})
195 print "<hr/>Backlinks:<ul>"
198 print "<li><a href=\"${REPLY}.html\">$p</a> <sup>${pages[$p]}</sup></li>"
202 print "</body></html>"
206 (links) for w (${(oi)=${links[$2]}}) print $w ;;
207 (backs) for w (${(oi)=${backs[$2]}}) print $w ;;
209 (html-index) genIndex $2 ;;
211 for p (${(k)pages}) {
213 genHTML $p > $2/$p.html
214 touch -r $p $2/$p.html
216 for p (${(k)cats}) genIndex $p/ > $2/$p/index.html
217 genIndex "" > $2/index.html
218 for p ("" ${(k)cats}) touch -d ${now/ /T} $2/$p/index.html