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 (${(oi)${(k)pages[(I)$1*]}}) {
160 [[ ( $p =~ "/index$" ) || ( $p = "index" ) ]] && continue
161 case ${#${(As:/:)p}} in
162 ($curdepth) _links=($p $_links) ;;
163 ( $(( $curdepth + 1 )) ) cats=(${1}${${p#$1}%%/*} $cats) ;;
167 for p (${(oi)_links}) \
168 entries=($entries "[$p] (${pages[$p]}) (${sizes[$p]} bytes)")
170 entries=($entries $separator "Subdirectories:" $separator)
171 for p (${(oi)cats}) {
172 entries=($entries "[$p/index]")
173 _links=($p/index $_links)
176 links[${1}index]=${(j: :)_links}
177 genHTML ${1}index ${(F)entries}
181 (links) for w (${(oi)=${links[$2]}}) print $w ;;
182 (backs) for w (${(oi)=${backs[$2]}}) print $w ;;
184 (html-index) genIndex $2 ;;
186 for p (${(k)pages}) {
188 genHTML $p > $2/$p.html
189 touch -r $p $2/$p.html
191 for p ($cats) pages[${p}/index]=$now
193 for p ($cats) genIndex $p/ > $2/$p/index.html
194 genIndex "" > $2/index.html
195 for p ("" $cats) touch -d ${now/ /T} $2/$p/index.html