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