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
26 zstat -A reply -F "%F %T" +mtime $p
30 for p (**(/)) cats=($p $cats)
33 zmodload -F zsh/files b:zf_mkdir
39 for w (${=mapfile[$p]}) {
40 [[ $w =~ "\[([^] ]+)\]" ]] || continue
42 [[ ${pages[$w]} ]] || {
43 [[ $ZK_SHOW_MISSING ]] && print "missing $w"
48 [[ $ws ]] && links[$p]=${(j: :)ws}
51 for p ws (${(kv)links}) {
52 for w (${=ws}) backs[$w]="$p ${backs[$w]}"
54 for p ws (${(kv)backs}) backs[$p]=${(j: :)${(u)=ws}}
57 # nearly the copy-paste of Functions/Misc/relative
62 abs=(${(s:/:)dst:h} $dst:t)
64 while [[ i -le $#abs && $abs[i] == $cur[i] ]] ; do
66 REPLY=${(j:/:)abs[i,-1]}
70 src=${(j:/:)cur[i,-1]/*/..}
71 dst=${(j:/:)abs[i,-1]}
72 REPLY=$src${dst:+/$dst}
78 [[ $# -eq 1 ]] && data=${mapfile[$page]} || data=$2
82 local _links=(${(oi)=links[$page]})
85 data="${data//\[${p}\]/<a href=\"${REPLY}.html\">[$p]</a>}"
87 print -r "<\!DOCTYPE html>
88 <html><head><title>$page (${pages[$page]})</title></head><body><pre>
91 if [[ $_links ]]; then
92 print "<hr/>Links:<ul>"
95 print "<li><a href=\"${REPLY}.html\">$p</a> <sup>${pages[$p]}</sup></li>"
99 local bs=(${(oi)=${backs[$page]}})
101 print "<hr/>Backlinks:<ul>"
104 print "<li><a href=\"${REPLY}.html\">$p</a> <sup>${pages[$p]}</sup></li>"
108 print "</body></html>"
111 zmodload -F zsh/datetime b:strftime
112 strftime -s now "%F %T"
119 local curdepth=${#${(s:/:)1}}
120 (( curdepth = curdepth + 1 ))
121 for p (${(oi)${(k)pages[(I)$1*]}}) {
122 [[ $p =~ "/Index$" ]] && continue
123 case ${#${(As:/:)p}} in
124 ($curdepth) _links=($p $_links) ;;
125 ( $(( $curdepth + 1 )) ) cats=(${1}${${p#$1}%%/*} $cats) ;;
129 for p (${(oi)_links}) entries=($entries "[$p] (${pages[$p]})")
131 entries=($entries "\nSubdirectories:\n")
132 for p (${(oi)cats}) {
133 entries=($entries "[$p/Index]")
134 _links=($p/Index $_links)
137 links[${1}Index]=${(j: :)_links}
138 genHTML ${1}Index ${(F)entries}
142 (links) for w (${(oi)=${links[$2]}}) print $w ;;
143 (backs) for w (${(oi)=${backs[$2]}}) print $w ;;
145 (html-index) genIndex $2 ;;
147 for p (${(k)pages}) {
149 genHTML $p > $2/$p.html
150 touch -r $p $2/$p.html
152 for p ($cats) pages[${p}/Index]=$now
154 for p ($cats) genIndex $p/ > $2/$p/Index.html
155 genIndex "" > $2/Index.html
156 for p ("" $cats) touch -d ${now/ /T} $2/$p/Index.html