]> Sergey Matveev's repositories - zk.zsh.git/blob - zk.zsh
816782adae3c08904d08726a90128ef456098dfd
[zk.zsh.git] / zk.zsh
1 #!/usr/bin/env zsh
2 # zk.zsh -- zettelkästen/wiki/static website helper/generator
3 # Copyright (C) 2022 Sergey Matveev <stargrave@stargrave.org>
4
5 set -e
6 ZK_VERSION=ZKZSH1
7
8 usage() {
9     cat >&2 <<EOF
10 Usage:
11   \$ $0:t links PAGE
12     Print PAGE's links
13   \$ $0:t backs PAGE
14     Print PAGE's backlinks
15   \$ $0:t htmls DIR
16     Generate HTMLs in DIR
17 EOF
18     exit 1
19 }
20
21 [[ $# -eq 2 ]] || usage
22
23 setopt GLOB_STAR_SHORT
24 zmodload -F zsh/stat b:zstat
25 typeset -A pages
26 typeset -A sizes
27 for p (**(.)) {
28     [[ $p:t == "index" ]] && {
29         echo unacceptable filename: $p >&2
30         exit 1
31     }
32     zstat -A mtime -F "%F %T" +mtime $p
33     zstat -A size +size $p
34     pages[$p]=${mtime[1]}
35     sizes[$p]=${size[1]}
36 }
37 typeset -a cats
38 for p (**(/)) cats=($p $cats)
39
40 zmodload zsh/mapfile
41 zmodload -F zsh/files b:zf_mkdir
42 typeset -A links
43 typeset -A backs
44 typeset -A cached
45 typeset -aU ws
46 for p (${(k)pages}) {
47     [[ $ZK_CACHE ]] && {
48         zstat -A inode +inode $p
49         zstat -A ctime +ctime $p
50         cache=(${(f)mapfile[$ZK_CACHE/$p]})
51         if [[ ( ${cache[1]} = $ZK_VERSION ) &&
52               ( ${cache[2]} = ${inode[1]} ) &&
53               ( ${cache[3]} = ${ctime[1]} ) ]]; then
54             ws=(${cache[4,-1]})
55             [[ $ws ]] && links[$p]=${(j: :)ws}
56             cached[$p]=1
57             continue
58         fi
59     }
60     ws=()
61     for w (${=mapfile[$p]}) {
62         [[ $w =~ "\[([^] ]+)\]" ]] || continue
63         w=${match[1]}
64         [[ ${pages[$w]} ]] || {
65             [[ $ZK_SHOW_MISSING ]] && print "missing $w"
66             continue
67         }
68         ws=($ws $w)
69     }
70     [[ $ZK_CACHE ]] && {
71         zf_mkdir -p $ZK_CACHE/$p:h
72         ws=($ZK_VERSION ${inode[1]} ${ctime[1]} $ws)
73         print -l $ws > $ZK_CACHE/$p
74         ws=(${ws[3,-1]})
75     }
76     [[ $ws ]] && links[$p]=${(j: :)ws}
77 }
78 unset cache ws
79 for p ws (${(kv)links}) {
80     for w (${=ws}) backs[$w]="$p ${backs[$w]}"
81 }
82 for p ws (${(kv)backs}) backs[$p]=${(j: :)${(u)=ws}}
83
84 getrel() {
85     # nearly the copy-paste of Functions/Misc/relative
86     local dst=$2:a
87     local src=$1:h:a
88     local -a cur abs
89     cur=(${(s:/:)src})
90     abs=(${(s:/:)dst:h} $dst:t)
91     integer i=1
92     while [[ i -le $#abs && $abs[i] == $cur[i] ]] ; do
93         ((++i > $#cur)) && {
94             REPLY=${(j:/:)abs[i,-1]}
95             return
96         }
97     done
98     src=${(j:/:)cur[i,-1]/*/..}
99     dst=${(j:/:)abs[i,-1]}
100     REPLY=$src${dst:+/$dst}
101 }
102
103 genHTML() {
104     local page=$1
105     local data p
106     [[ $# -eq 1 ]] && data=${mapfile[$page]} || data=$2
107     local _links=(${(oi)=links[$page]})
108     if [[ ( ${cached[$page]} ) && ( -s $ZK_CACHE/${page}.html ) ]]; then
109         cat $ZK_CACHE/${page}.html
110     else
111         data=${data//&/&amp;}
112         data=${data//</&lt;}
113         data=${data//>/&gt;}
114         for p ($_links) {
115             getrel $page $p
116             data="${data//\[${p}\]/<a href=\"${REPLY}.html\">[$p]</a>}"
117         }
118         data="<!DOCTYPE html>
119 <html><head><title>$page (${pages[$page]})</title></head><body><pre>
120 $data</pre>"
121         if [[ $ZK_CACHE ]]; then
122             print -r "$data" > $ZK_CACHE/${page}.html
123             cat $ZK_CACHE/${page}.html
124         else
125             print -r "$data"
126         fi
127     fi
128     if [[ $_links ]]; then
129         print "<hr/>Links:<ul>"
130         for p ($_links) {
131             getrel $page $p
132             print "<li><a href=\"${REPLY}.html\">$p</a> <sup>${pages[$p]}</sup></li>"
133         }
134         print "</ul>"
135     fi
136     local bs=(${(oi)=${backs[$page]}})
137     if [[ $bs ]]; then
138         print "<hr/>Backlinks:<ul>"
139         for p ($bs) {
140             getrel $page $p
141             print "<li><a href=\"${REPLY}.html\">$p</a> <sup>${pages[$p]}</sup></li>"
142         }
143         print "</ul>"
144     fi
145     print "</body></html>"
146 }
147
148 zmodload -F zsh/datetime b:strftime
149 strftime -s now "%F %T"
150
151 genIndex() {
152     local p
153     local entries=()
154     local _links=()
155     typeset -aU cats=()
156     local curdepth=${#${(s:/:)1}}
157     (( curdepth = curdepth + 1 ))
158     for p (${(oi)${(k)pages[(I)$1*]}}) {
159         [[ ( $p =~ "/index$" ) || ( $p = "index" ) ]] && continue
160         case ${#${(As:/:)p}} in
161         ($curdepth) _links=($p $_links) ;;
162         ( $(( $curdepth + 1 )) ) cats=(${1}${${p#$1}%%/*} $cats) ;;
163         (*) continue ;;
164         esac
165     }
166     for p (${(oi)_links}) \
167         entries=($entries "[$p] (${pages[$p]}) (${sizes[$p]} bytes)")
168     if [[ $cats ]]; then
169         entries=($entries " " "Subdirectories:" " ")
170         for p (${(oi)cats}) {
171             entries=($entries "[$p/index]")
172             _links=($p/index $_links)
173         }
174     fi
175     links[${1}index]=${(j: :)_links}
176     genHTML ${1}index ${(F)entries}
177 }
178
179 case $1 in
180 (links) for w (${(oi)=${links[$2]}}) print $w ;;
181 (backs) for w (${(oi)=${backs[$2]}}) print $w ;;
182 (html) genHTML $2 ;;
183 (html-index) genIndex $2 ;;
184 (htmls)
185     for p (${(k)pages}) {
186         zf_mkdir -p $2/$p:h
187         genHTML $p > $2/$p.html
188         touch -r $p $2/$p.html
189     }
190     for p ($cats) pages[${p}/index]=$now
191     pages[index]=$now
192     for p ($cats) genIndex $p/ > $2/$p/index.html
193     genIndex "" > $2/index.html
194     for p ("" $cats) touch -d ${now/ /T} $2/$p/index.html
195     ;;
196 (*) usage ;;
197 esac