2 # zk.zsh -- zettelkästen/wiki/static website helper/generator
3 # Copyright (C) 2022 Sergey Matveev <stargrave@stargrave.org>
13 Print PAGE's backlinks
20 [[ $# -eq 2 ]] || usage
22 setopt GLOB_STAR_SHORT
23 zmodload -F zsh/stat b:zstat
27 zstat -A mtime -F "%F %T" +mtime $p
28 zstat -A size +size $p
33 for p (**(/)) cats=($p $cats)
36 zmodload -F zsh/files b:zf_mkdir
42 for w (${=mapfile[$p]}) {
43 [[ $w =~ "\[([^] ]+)\]" ]] || continue
45 [[ ${pages[$w]} ]] || {
46 [[ $ZK_SHOW_MISSING ]] && print "missing $w"
51 [[ $ws ]] && links[$p]=${(j: :)ws}
54 for p ws (${(kv)links}) {
55 for w (${=ws}) backs[$w]="$p ${backs[$w]}"
57 for p ws (${(kv)backs}) backs[$p]=${(j: :)${(u)=ws}}
60 # nearly the copy-paste of Functions/Misc/relative
65 abs=(${(s:/:)dst:h} $dst:t)
67 while [[ i -le $#abs && $abs[i] == $cur[i] ]] ; do
69 REPLY=${(j:/:)abs[i,-1]}
73 src=${(j:/:)cur[i,-1]/*/..}
74 dst=${(j:/:)abs[i,-1]}
75 REPLY=$src${dst:+/$dst}
81 [[ $# -eq 1 ]] && data=${mapfile[$page]} || data=$2
85 local _links=(${(oi)=links[$page]})
88 data="${data//\[${p}\]/<a href=\"${REPLY}.html\">[$p]</a>}"
90 print -r "<\!DOCTYPE html>
91 <html><head><title>$page (${pages[$page]})</title></head><body><pre>
94 if [[ $_links ]]; then
95 print "<hr/>Links:<ul>"
98 print "<li><a href=\"${REPLY}.html\">$p</a> <sup>${pages[$p]}</sup></li>"
102 local bs=(${(oi)=${backs[$page]}})
104 print "<hr/>Backlinks:<ul>"
107 print "<li><a href=\"${REPLY}.html\">$p</a> <sup>${pages[$p]}</sup></li>"
111 print "</body></html>"
114 zmodload -F zsh/datetime b:strftime
115 strftime -s now "%F %T"
122 local curdepth=${#${(s:/:)1}}
123 (( curdepth = curdepth + 1 ))
124 for p (${(oi)${(k)pages[(I)$1*]}}) {
125 [[ $p =~ "/Index$" ]] && continue
126 case ${#${(As:/:)p}} in
127 ($curdepth) _links=($p $_links) ;;
128 ( $(( $curdepth + 1 )) ) cats=(${1}${${p#$1}%%/*} $cats) ;;
132 for p (${(oi)_links}) \
133 entries=($entries "[$p] (${pages[$p]}) (${sizes[$p]} bytes)")
135 entries=($entries "\nSubdirectories:\n")
136 for p (${(oi)cats}) {
137 entries=($entries "[$p/Index]")
138 _links=($p/Index $_links)
141 links[${1}Index]=${(j: :)_links}
142 genHTML ${1}Index ${(F)entries}
146 (links) for w (${(oi)=${links[$2]}}) print $w ;;
147 (backs) for w (${(oi)=${backs[$2]}}) print $w ;;
149 (html-index) genIndex $2 ;;
151 for p (${(k)pages}) {
153 genHTML $p > $2/$p.html
154 touch -r $p $2/$p.html
156 for p ($cats) pages[${p}/Index]=$now
158 for p ($cats) genIndex $p/ > $2/$p/Index.html
159 genIndex "" > $2/Index.html
160 for p ("" $cats) touch -d ${now/ /T} $2/$p/Index.html