2 # zk.zsh -- zettelkästen/wiki/static website helper/generator
3 # Copyright (C) 2022 Sergey Matveev <stargrave@stargrave.org>
11 Print the PAGE's links
13 Print who backlinks to the PAGE
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 [[ $words ]] && links[$p]=${(j: :)words}
49 for p ws (${(kv)links}) {
50 for w (${=ws}) backs[$w]="$p ${backs[$w]}"
52 for p ws (${(kv)backs}) backs[$p]=${(j: :)${(u)=ws}}
55 # nearly the copy-paste of Functions/Misc/relative
60 abs=(${(s:/:)dst:h} $dst:t)
62 while [[ i -le $#abs && $abs[i] == $cur[i] ]] ; do
64 REPLY=${(j:/:)abs[i,-1]}
68 src=${(j:/:)cur[i,-1]/*/..}
69 dst=${(j:/:)abs[i,-1]}
70 REPLY=$src${dst:+/$dst}
76 [[ $# -eq 1 ]] && data=${mapfile[$page]} || data=$2
80 local _links=(${(oi)=links[$page]})
83 data="${data//\[${p}\]/<a href=\"${REPLY}.html\">[$p]</a>}"
85 print "<\!DOCTYPE html>
86 <html><head><title>$page (${pages[$page]})</title></head><body><pre>
89 if [[ $_links ]]; then
90 print "<hr/>Links:<ul>"
93 print "<li><a href=\"${REPLY}.html\">$p</a> <sup>${pages[$p]}</sup></li>"
97 local bs=(${(oi)=${backs[$page]}})
99 print "<hr/>Backlinks:<ul>"
102 print "<li><a href=\"${REPLY}.html\">$p</a> <sup>${pages[$p]}</sup></li>"
106 print "</body></html>"
109 zmodload -F zsh/datetime b:strftime
110 strftime -s now "%F %T"
117 local curdepth=${#${(s:/:)1}}
118 (( curdepth = curdepth + 1 ))
119 for p (${(oi)${(k)pages[(I)$1*]}}) {
120 [[ $p =~ "/Index$" ]] && continue
121 case ${#${(As:/:)p}} in
122 ($curdepth) _links=($p $_links) ;;
123 ( $(( $curdepth + 1 )) ) cats=(${1}${${p#$1}%%/*} $cats) ;;
127 for p (${(oi)_links}) entries=($entries "[$p] (${pages[$p]})")
129 entries=($entries "\nSubdirectories:\n")
130 for p (${(oi)cats}) {
131 entries=($entries "[$p/Index]")
132 _links=($p/Index $_links)
135 links[${1}Index]=${(j: :)_links}
136 genHTML ${1}Index ${(F)entries}
140 (links) for w (${(oi)=${links[$2]}}) print $w ;;
141 (backs) for w (${(oi)=${backs[$2]}}) print $w ;;
143 (html-index) genIndex $2 ;;
145 for p (${(k)pages}) {
147 genHTML $p > $2/$p.html
148 touch -r $p $2/$p.html
150 for p ($cats) pages[${p}/Index]=$now
152 for p ($cats) genIndex $p/ > $2/$p/Index.html
153 genIndex "" > $2/Index.html
154 for p ("" $cats) touch -d ${now/ /T} $2/$p/Index.html