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 $cats)
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 [[ ${pages[$w]} ]] || {
66 [[ $ZK_SHOW_MISSING ]] && print "missing $w"
72 zf_mkdir -p $ZK_CACHE/$p:h
73 ws=($ZK_VERSION ${inode[1]} ${ctime[1]} $ws)
74 print -l $ws > $ZK_CACHE/$p
77 [[ $ws ]] && links[$p]=${(j: :)ws}
80 for p ws (${(kv)links}) {
81 for w (${=ws}) backs[$w]="$p ${backs[$w]}"
83 for p ws (${(kv)backs}) backs[$p]=${(j: :)${(u)=ws}}
86 # nearly the copy-paste of Functions/Misc/relative
91 abs=(${(s:/:)dst:h} $dst:t)
93 while [[ i -le $#abs && $abs[i] == $cur[i] ]] ; do
95 REPLY=${(j:/:)abs[i,-1]}
99 src=${(j:/:)cur[i,-1]/*/..}
100 dst=${(j:/:)abs[i,-1]}
101 REPLY=$src${dst:+/$dst}
107 [[ $# -eq 1 ]] && data=${mapfile[$page]} || data=$2
108 local _links=(${(oi)=links[$page]})
109 if [[ ( ${cached[$page]} ) && ( -s $ZK_CACHE/${page}.html ) ]]; then
110 < $ZK_CACHE/${page}.html
112 data=${data//&/&}
117 data="${data//\[${p}\]/<a href=\"${REPLY}.html\">[$p]</a>}"
119 data="<!DOCTYPE html>
120 <html><head><title>$page (${pages[$page]})</title></head><body><pre>
122 if [[ $ZK_CACHE ]]; then
123 print -r "$data" > $ZK_CACHE/${page}.html
124 < $ZK_CACHE/${page}.html
129 if [[ $_links ]]; then
130 print "<hr/>Links:<ul>"
133 print "<li><a href=\"${REPLY}.html\">$p</a> <sup>${pages[$p]}</sup></li>"
137 local bs=(${(oi)=${backs[$page]}})
139 print "<hr/>Backlinks:<ul>"
142 print "<li><a href=\"${REPLY}.html\">$p</a> <sup>${pages[$p]}</sup></li>"
146 print "</body></html>"
149 zmodload -F zsh/datetime b:strftime
150 strftime -s now "%F %T"
157 local curdepth=${#${(s:/:)1}}
158 (( curdepth = curdepth + 1 ))
159 for p (${(k)pages[(I)$1*]}) {
160 case ${#${(As:/:)p}} in
161 ($curdepth) _links=($p $_links) ;;
162 ( $(( $curdepth + 1 )) ) cats=(${1}${${p#$1}%%/*} $cats) ;;
167 print "<!DOCTYPE html>
168 <html><head><title>$page ($now)</title></head><body><ul>"
169 for p (${(oi)_links}) {
171 print "<li><a href=\"${REPLY}.html\">$p</a> <sup>${pages[$p]}</sup> (${sizes[$p]} bytes)</li>"
175 print "<hr/>Subdirectories:<ul>"
176 for p (${(oi)cats}) {
177 getrel $page $p/index
178 print "<li><a href=\"${REPLY}.html\">$p</a></li>"
182 print "</body></html>"
186 (links) for w (${(oi)=${links[$2]}}) print $w ;;
187 (backs) for w (${(oi)=${backs[$2]}}) print $w ;;
189 (html-index) genIndex $2 ;;
191 for p (${(k)pages}) {
193 genHTML $p > $2/$p.html
194 touch -r $p $2/$p.html
196 for p ($cats) genIndex $p/ > $2/$p/index.html
197 genIndex "" > $2/index.html
198 for p ("" $cats) touch -d ${now/ /T} $2/$p/index.html