]> Sergey Matveev's repositories - zk.zsh.git/blob - zk.zsh
348a5b521b2e066d581741ded0f5baf0fbf83dcf
[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 typeset -a cats
29 for p (**(/)) cats=($p $cats)
30
31 # Determine the links between them
32 typeset -A links
33 typeset -A backs
34 for p (${(k)pages}) {
35     for w (`< $p`) {
36         [[ $w =~ "\[(.*)\]" ]] || continue
37         w=${match[1]}
38         [[ ${pages[$w]} ]] || {
39             [[ $ZK_SHOW_MISSING ]] && print "Missing $w"
40             continue
41         }
42         links[$p]="$w ${links[$p]}"
43     }
44 }
45
46 # Deduplicate all references
47 for p w (${(kv)links}) {
48     local ws=(${(u)=w})
49     links[$p]=${(j: :)ws}
50     for w ($ws) backs[$w]="$p ${backs[$w]}"
51 }
52 for p w (${(kv)backs}) {
53     local ws=(${(u)=w})
54     backs[$p]=${(j: :)ws}
55 }
56
57 getrel() {
58     # nearly the copy-paste of Functions/Misc/relative
59     local dst=$2:a
60     local src=$1:h:a
61     local -a cur abs
62     cur=(${(s:/:)src})
63     abs=(${(s:/:)dst:h} $dst:t)
64     integer i=1
65     while [[ i -le $#abs && $abs[i] == $cur[i] ]] ; do
66         ((++i > $#cur)) && {
67             REPLY=${(j:/:)abs[i,-1]}
68             return
69         }
70     done
71     src=${(j:/:)cur[i,-1]/*/..}
72     dst=${(j:/:)abs[i,-1]}
73     REPLY=$src${dst:+/$dst}
74 }
75
76 genHTML() {
77     local page=$1
78     local data p
79     [[ $# -eq 1 ]] && data=`< $page` || data=$2
80     data="${data//&/&amp;}"
81     data="${data//</&lt;}"
82     data="${data//>/&gt;}"
83     for p (${(k)pages}) {
84         [[ $p = index ]] && continue
85         getrel $page $p
86         data="${data//\[${p}\]/<a href=\"${REPLY}.html\">[$p]</a>}"
87     }
88     print "<\!DOCTYPE html>
89 <html><head><title>$page (${pages[$page]})</title></head><body><pre>
90 $data
91 </pre>"
92     local bs=(${(oi)=${backs[$page]}})
93     if [[ $bs ]]; then
94         print "<hr/><ul>"
95         for p (${(oi)=${backs[$page]}}) {
96             getrel $page $p
97             print "<li><a href=\"${REPLY}.html\">$p</a> <sup>${pages[$p]}</sup></li>"
98         }
99         print "</ul>"
100     fi
101     print "</body></html>"
102 }
103
104 zmodload -F zsh/datetime b:strftime
105 now=$(strftime "%F %T")
106
107 genIndex() {
108     local p
109     local entries=()
110     typeset -aU cats=()
111     local curdepth=${#${(s:/:)1}}
112     (( curdepth = curdepth + 1 ))
113     for p (${(oi)${(k)pages[(I)$1*]}}) {
114         [[ $p =~ "/Index$" ]] && continue
115         case ${#${(As:/:)p}} in
116         ($curdepth) entries=($entries "[$p] (${pages[$p]})") ;;
117         ( $(( $curdepth + 1 )) ) cats=(${1}${${p#$1}%%/*} $cats) ;;
118         (*) continue ;;
119         esac
120     }
121     if [[ $cats ]]; then
122         entries=($entries "------------------------ >8 ------------------------")
123         for p (${(oi)cats}) entries=($entries "[$p/Index]")
124     fi
125     genHTML ${1}Index ${(F)entries}
126 }
127
128 case $1 in
129 (links) for w (${(oi)=${links[$2]}}) print $w ;;
130 (backs) for w (${(oi)=${backs[$2]}}) print $w ;;
131 (html) genHTML $2 ;;
132 (html-index) genIndex $2 ;;
133 (htmls)
134     for p (${(k)pages}) {
135         mkdir -p $2/$p:h
136         genHTML $p > $2/$p.html
137         touch -r $p $2/$p.html
138     }
139     for p ($cats) pages[${p}/Index]=$now
140     pages[Index]=$now
141     for p ($cats) genIndex $p/ > $2/$p/Index.html
142     genIndex "" > $2/Index.html
143     for p ("" $cats) touch -d ${now/ /T} $2/$p/Index.html
144     ;;
145 (*) usage ;;
146 esac