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)
38 for w (${=mapfile[$p]}) {
39 [[ $w =~ "\[([^] ]+)\]" ]] || continue
41 [[ ${pages[$w]} ]] || {
42 [[ $ZK_SHOW_MISSING ]] && print "missing $w"
47 [[ $ws ]] && links[$p]=${(j: :)ws}
50 for p ws (${(kv)links}) {
51 for w (${=ws}) backs[$w]="$p ${backs[$w]}"
53 for p ws (${(kv)backs}) backs[$p]=${(j: :)${(u)=ws}}
56 # nearly the copy-paste of Functions/Misc/relative
61 abs=(${(s:/:)dst:h} $dst:t)
63 while [[ i -le $#abs && $abs[i] == $cur[i] ]] ; do
65 REPLY=${(j:/:)abs[i,-1]}
69 src=${(j:/:)cur[i,-1]/*/..}
70 dst=${(j:/:)abs[i,-1]}
71 REPLY=$src${dst:+/$dst}
77 [[ $# -eq 1 ]] && data=${mapfile[$page]} || data=$2
81 local _links=(${(oi)=links[$page]})
84 data="${data//\[${p}\]/<a href=\"${REPLY}.html\">[$p]</a>}"
86 print "<\!DOCTYPE html>
87 <html><head><title>$page (${pages[$page]})</title></head><body><pre>
90 if [[ $_links ]]; then
91 print "<hr/>Links:<ul>"
94 print "<li><a href=\"${REPLY}.html\">$p</a> <sup>${pages[$p]}</sup></li>"
98 local bs=(${(oi)=${backs[$page]}})
100 print "<hr/>Backlinks:<ul>"
103 print "<li><a href=\"${REPLY}.html\">$p</a> <sup>${pages[$p]}</sup></li>"
107 print "</body></html>"
110 zmodload -F zsh/datetime b:strftime
111 strftime -s now "%F %T"
118 local curdepth=${#${(s:/:)1}}
119 (( curdepth = curdepth + 1 ))
120 for p (${(oi)${(k)pages[(I)$1*]}}) {
121 [[ $p =~ "/Index$" ]] && continue
122 case ${#${(As:/:)p}} in
123 ($curdepth) _links=($p $_links) ;;
124 ( $(( $curdepth + 1 )) ) cats=(${1}${${p#$1}%%/*} $cats) ;;
128 for p (${(oi)_links}) entries=($entries "[$p] (${pages[$p]})")
130 entries=($entries "\nSubdirectories:\n")
131 for p (${(oi)cats}) {
132 entries=($entries "[$p/Index]")
133 _links=($p/Index $_links)
136 links[${1}Index]=${(j: :)_links}
137 genHTML ${1}Index ${(F)entries}
141 (links) for w (${(oi)=${links[$2]}}) print $w ;;
142 (backs) for w (${(oi)=${backs[$2]}}) print $w ;;
144 (html-index) genIndex $2 ;;
146 for p (${(k)pages}) {
148 genHTML $p > $2/$p.html
149 touch -r $p $2/$p.html
151 for p ($cats) pages[${p}/Index]=$now
153 for p ($cats) genIndex $p/ > $2/$p/Index.html
154 genIndex "" > $2/Index.html
155 for p ("" $cats) touch -d ${now/ /T} $2/$p/Index.html