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 zstat -A mtime -F "%F %T" +mtime $p
29 zstat -A size +size $p
34 for p (**(/)) cats=($p $cats)
37 zmodload -F zsh/files b:zf_mkdir
44 zstat -A inode +inode $p
45 zstat -A ctime +ctime $p
46 cache=(${(f)mapfile[$ZK_CACHE/$p]})
47 if [[ ( ${cache[1]} = $ZK_VERSION ) &&
48 ( ${cache[2]} = ${inode[1]} ) &&
49 ( ${cache[3]} = ${ctime[1]} ) ]]; then
51 [[ $ws ]] && links[$p]=${(j: :)ws}
57 for w (${=mapfile[$p]}) {
58 [[ $w =~ "\[([^] ]+)\]" ]] || continue
60 [[ ${pages[$w]} ]] || {
61 [[ $ZK_SHOW_MISSING ]] && print "missing $w"
67 zf_mkdir -p $ZK_CACHE/$p:h
68 ws=($ZK_VERSION ${inode[1]} ${ctime[1]} $ws)
69 print -l $ws > $ZK_CACHE/$p
72 [[ $ws ]] && links[$p]=${(j: :)ws}
75 for p ws (${(kv)links}) {
76 for w (${=ws}) backs[$w]="$p ${backs[$w]}"
78 for p ws (${(kv)backs}) backs[$p]=${(j: :)${(u)=ws}}
81 # nearly the copy-paste of Functions/Misc/relative
86 abs=(${(s:/:)dst:h} $dst:t)
88 while [[ i -le $#abs && $abs[i] == $cur[i] ]] ; do
90 REPLY=${(j:/:)abs[i,-1]}
94 src=${(j:/:)cur[i,-1]/*/..}
95 dst=${(j:/:)abs[i,-1]}
96 REPLY=$src${dst:+/$dst}
102 [[ $# -eq 1 ]] && data=${mapfile[$page]} || data=$2
103 local _links=(${(oi)=links[$page]})
104 if [[ ( ${cached[$page]} ) && ( -s $ZK_CACHE/${page}.html ) ]]; then
105 cat $ZK_CACHE/${page}.html
107 data=${data//&/&}
112 data="${data//\[${p}\]/<a href=\"${REPLY}.html\">[$p]</a>}"
114 data="<!DOCTYPE html>
115 <html><head><title>$page (${pages[$page]})</title></head><body><pre>
117 if [[ $ZK_CACHE ]]; then
118 print -r "$data" > $ZK_CACHE/${page}.html
119 cat $ZK_CACHE/${page}.html
124 if [[ $_links ]]; then
125 print "<hr/>Links:<ul>"
128 print "<li><a href=\"${REPLY}.html\">$p</a> <sup>${pages[$p]}</sup></li>"
132 local bs=(${(oi)=${backs[$page]}})
134 print "<hr/>Backlinks:<ul>"
137 print "<li><a href=\"${REPLY}.html\">$p</a> <sup>${pages[$p]}</sup></li>"
141 print "</body></html>"
144 zmodload -F zsh/datetime b:strftime
145 strftime -s now "%F %T"
152 local curdepth=${#${(s:/:)1}}
153 (( curdepth = curdepth + 1 ))
154 for p (${(oi)${(k)pages[(I)$1*]}}) {
155 [[ ( $p =~ "/Index$" ) || ( $p = "Index" ) ]] && continue
156 case ${#${(As:/:)p}} in
157 ($curdepth) _links=($p $_links) ;;
158 ( $(( $curdepth + 1 )) ) cats=(${1}${${p#$1}%%/*} $cats) ;;
162 for p (${(oi)_links}) \
163 entries=($entries "[$p] (${pages[$p]}) (${sizes[$p]} bytes)")
165 entries=($entries " " "Subdirectories:" " ")
166 for p (${(oi)cats}) {
167 entries=($entries "[$p/Index]")
168 _links=($p/Index $_links)
171 links[${1}Index]=${(j: :)_links}
172 genHTML ${1}Index ${(F)entries}
176 (links) for w (${(oi)=${links[$2]}}) print $w ;;
177 (backs) for w (${(oi)=${backs[$2]}}) print $w ;;
179 (html-index) genIndex $2 ;;
181 for p (${(k)pages}) {
183 genHTML $p > $2/$p.html
184 touch -r $p $2/$p.html
186 for p ($cats) pages[${p}/Index]=$now
188 for p ($cats) genIndex $p/ > $2/$p/Index.html
189 genIndex "" > $2/Index.html
190 for p ("" $cats) touch -d ${now/ /T} $2/$p/Index.html