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 setopt GLOB_STAR_SHORT
24 zmodload -F zsh/stat b:zstat
28 [[ $p:t == "index" ]] && {
29 echo unacceptable filename: $p >&2
32 zstat -A mtime -F "%F %T" +mtime $p
33 zstat -A size +size $p
38 for p (**(/)) cats=($p $cats)
41 zmodload -F zsh/files b:zf_mkdir
48 zstat -A inode +inode $p
49 zstat -A ctime +ctime $p
50 cache=(${(f)mapfile[$ZK_CACHE/$p]})
51 if [[ ( ${cache[1]} = $ZK_VERSION ) &&
52 ( ${cache[2]} = ${inode[1]} ) &&
53 ( ${cache[3]} = ${ctime[1]} ) ]]; then
55 [[ $ws ]] && links[$p]=${(j: :)ws}
61 for w (${=mapfile[$p]}) {
62 [[ $w =~ "\[([^] ]+)\]" ]] || continue
64 [[ ${pages[$w]} ]] || {
65 [[ $ZK_SHOW_MISSING ]] && print "missing $w"
71 zf_mkdir -p $ZK_CACHE/$p:h
72 ws=($ZK_VERSION ${inode[1]} ${ctime[1]} $ws)
73 print -l $ws > $ZK_CACHE/$p
76 [[ $ws ]] && links[$p]=${(j: :)ws}
79 for p ws (${(kv)links}) {
80 for w (${=ws}) backs[$w]="$p ${backs[$w]}"
82 for p ws (${(kv)backs}) backs[$p]=${(j: :)${(u)=ws}}
85 # nearly the copy-paste of Functions/Misc/relative
90 abs=(${(s:/:)dst:h} $dst:t)
92 while [[ i -le $#abs && $abs[i] == $cur[i] ]] ; do
94 REPLY=${(j:/:)abs[i,-1]}
98 src=${(j:/:)cur[i,-1]/*/..}
99 dst=${(j:/:)abs[i,-1]}
100 REPLY=$src${dst:+/$dst}
106 [[ $# -eq 1 ]] && data=${mapfile[$page]} || data=$2
107 local _links=(${(oi)=links[$page]})
108 if [[ ( ${cached[$page]} ) && ( -s $ZK_CACHE/${page}.html ) ]]; then
109 cat $ZK_CACHE/${page}.html
111 data=${data//&/&}
116 data="${data//\[${p}\]/<a href=\"${REPLY}.html\">[$p]</a>}"
118 data="<!DOCTYPE html>
119 <html><head><title>$page (${pages[$page]})</title></head><body><pre>
121 if [[ $ZK_CACHE ]]; then
122 print -r "$data" > $ZK_CACHE/${page}.html
123 cat $ZK_CACHE/${page}.html
128 if [[ $_links ]]; then
129 print "<hr/>Links:<ul>"
132 print "<li><a href=\"${REPLY}.html\">$p</a> <sup>${pages[$p]}</sup></li>"
136 local bs=(${(oi)=${backs[$page]}})
138 print "<hr/>Backlinks:<ul>"
141 print "<li><a href=\"${REPLY}.html\">$p</a> <sup>${pages[$p]}</sup></li>"
145 print "</body></html>"
148 zmodload -F zsh/datetime b:strftime
149 strftime -s now "%F %T"
156 local curdepth=${#${(s:/:)1}}
157 (( curdepth = curdepth + 1 ))
158 for p (${(oi)${(k)pages[(I)$1*]}}) {
159 [[ ( $p =~ "/index$" ) || ( $p = "index" ) ]] && continue
160 case ${#${(As:/:)p}} in
161 ($curdepth) _links=($p $_links) ;;
162 ( $(( $curdepth + 1 )) ) cats=(${1}${${p#$1}%%/*} $cats) ;;
166 for p (${(oi)_links}) \
167 entries=($entries "[$p] (${pages[$p]}) (${sizes[$p]} bytes)")
169 entries=($entries " " "Subdirectories:" " ")
170 for p (${(oi)cats}) {
171 entries=($entries "[$p/index]")
172 _links=($p/index $_links)
175 links[${1}index]=${(j: :)_links}
176 genHTML ${1}index ${(F)entries}
180 (links) for w (${(oi)=${links[$2]}}) print $w ;;
181 (backs) for w (${(oi)=${backs[$2]}}) print $w ;;
183 (html-index) genIndex $2 ;;
185 for p (${(k)pages}) {
187 genHTML $p > $2/$p.html
188 touch -r $p $2/$p.html
190 for p ($cats) pages[${p}/index]=$now
192 for p ($cats) genIndex $p/ > $2/$p/index.html
193 genIndex "" > $2/index.html
194 for p ("" $cats) touch -d ${now/ /T} $2/$p/index.html