]> Sergey Matveev's repositories - zk.zsh.git/blob - zk.zsh
f4b7a240ba5cd651bcc5d5ce4b10e4f074c46272
[zk.zsh.git] / zk.zsh
1 #!/usr/bin/env zsh
2
3 set -e
4
5 usage() {
6     cat >&2 <<EOF
7 Usage:
8   \$ $0:t links PAGE
9     Print the PAGE's links
10   \$ $0:t backs PAGE
11     Print who backlinks to the PAGE
12   \$ $0:t htmls DIR
13     Generate HTMLs in DIR
14 EOF
15     exit 1
16 }
17
18 [[ $# -eq 2 ]] || usage
19
20 # Collect all pages
21 setopt GLOB_STAR_SHORT
22 zmodload -F zsh/stat b:zstat
23 typeset -A pages
24 for p (**(.)) {
25     zstat -A reply -F "%F %T" +mtime $p
26     pages[$p]=${reply[1]}
27 }
28
29 # Determine the links between them
30 typeset -A links
31 typeset -A backs
32 for p (${(k)pages}) {
33     for w (`< $p`) {
34         [[ $w =~ "\[(.*)\]" ]] || continue
35         w=${match[1]}
36         [[ ${pages[$w]} ]] || {
37             [[ $ZK_SHOW_MISSING ]] && print "Missing $w"
38             continue
39         }
40         links[$p]="$w ${links[$p]}"
41     }
42 }
43
44 # Deduplicate all references
45 for p w (${(kv)links}) {
46     local ws=(${(u)=w})
47     links[$p]=${(j: :)ws}
48     for w ($ws) backs[$w]="$p ${backs[$w]}"
49 }
50 for p w (${(kv)backs}) {
51     local ws=(${(u)=w})
52     backs[$p]=${(j: :)ws}
53 }
54
55 getrel() {
56     # nearly the copy-paste of Functions/Misc/relative
57     local dst=$2:a
58     local src=$1:h:a
59     local -a cur abs
60     cur=(${(s:/:)src})
61     abs=(${(s:/:)dst:h} $dst:t)
62     integer i=1
63     while [[ i -le $#abs && $abs[i] == $cur[i] ]] ; do
64         ((++i > $#cur)) && {
65             REPLY=${(j:/:)abs[i,-1]}
66             return
67         }
68     done
69     src=${(j:/:)cur[i,-1]/*/..}
70     dst=${(j:/:)abs[i,-1]}
71     REPLY=$src${dst:+/$dst}
72 }
73
74 genhtml() {
75     local page=$1
76     local data p
77     [[ $# -eq 1 ]] && data=`< $page` || data=$2
78     data="${data//&/&amp;}"
79     data="${data//</&lt;}"
80     data="${data//>/&gt;}"
81     for p (${(k)pages}) {
82         [[ $p = index ]] && continue
83         getrel $page $p
84         data="${data//${p}/<a href=\"${REPLY}.html\">$p</a>}"
85     }
86     print "<\!DOCTYPE html>
87 <html><head><title>$page (${pages[$page]})</title></head><body><pre>
88 $data
89 </pre><hr/><ul>"
90     for p (${(oi)=${backs[$page]}}) {
91         getrel $page $p
92         print "<li><a href=\"${REPLY}.html\">$p</a> <sup>${pages[$p]}</sup></li>"
93     }
94     print "</ul></body></html>"
95 }
96
97 case $1 in
98 (links) for w (${(oi)=${links[$2]}}) print $w ;;
99 (backs) for w (${(oi)=${backs[$2]}}) print $w ;;
100 (html) genhtml $2 ;;
101 (htmls)
102     for p (${(k)pages}) {
103         local subdir=$p:h
104         mkdir -p $2/$subdir
105         genhtml $p > $2/$p.html
106     }
107     local all=()
108     for p (${(Oi)${(k)pages}}) all=("$p (${pages[$p]})" $all)
109     zmodload -F zsh/datetime b:strftime
110     pages[ALL]=$(strftime "%F %T")
111     genhtml ALL ${(j:\n:)all} > $2/ALL.html
112     ;;
113 (*) usage ;;
114 esac