]> Sergey Matveev's repositories - zk.zsh.git/blob - zk.zsh
d478ed037baf9b28624f938f06c0e6417a146ee5
[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 autoload -U relative
56 getrel() {
57     # nearly the copy-paste of Functions/Misc/relative
58     local dst=$2:a
59     local src=$1:h:a
60     local -a cur abs
61     cur=(${(s:/:)src})
62     abs=(${(s:/:)dst:h} $dst:t)
63     integer i=1
64     while [[ i -le $#abs && $abs[i] == $cur[i] ]] ; do
65         ((++i > $#cur)) && {
66             REPLY=${(j:/:)abs[i,-1]}
67             return
68         }
69     done
70     src=${(j:/:)cur[i,-1]/*/..}
71     dst=${(j:/:)abs[i,-1]}
72     REPLY=$src${dst:+/$dst}
73 }
74
75 genhtml() {
76     local page=$1
77     local data
78     [[ $# -eq 1 ]] && data=`< $page` || data=$2
79     data="${data//&/&amp;}"
80     data="${data//</&lt;}"
81     data="${data//>/&gt;}"
82     for p (${(k)pages}) {
83         [[ $p = index ]] && continue
84         getrel $page $p
85         data="${data//${p}/<a href=\"${REPLY}.html\">$p</a>}"
86     }
87     print "<\!DOCTYPE html>
88 <html><head><title>$page (${pages[$page]})</title></head><body><pre>
89 $data
90 </pre><hr/><ul>"
91     for p (${(oi)=${backs[$page]}}) {
92         getrel $page $p
93         print "<li><a href=\"${REPLY}.html\">$p</a> <sup>${pages[$p]}</sup></li>"
94     }
95     print "</ul></body></html>"
96 }
97
98 case $1 in
99 (links) for w (${(oi)=${links[$2]}}) print $w ;;
100 (backs) for w (${(oi)=${backs[$2]}}) print $w ;;
101 (html) genhtml $2 ;;
102 (htmls)
103     for p (${(k)pages}) {
104         local subdir=$p:h
105         mkdir -p $2/$subdir
106         genhtml $p > $2/$p.html
107     }
108     local all=()
109     for p (${(Oi)${(k)pages}}) all=("$p (${pages[$p]})" $all)
110     zmodload -F zsh/datetime b:strftime
111     pages[ALL]=$(strftime "%F %T")
112     genhtml ALL ${(j:\n:)all} > $2/ALL.html
113     ;;
114 (*) usage ;;
115 esac