]> Sergey Matveev's repositories - zk.zsh.git/blob - zk.zsh
mapfile instead of cat-like call is faster
[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
7 usage() {
8     cat >&2 <<EOF
9 Usage:
10   \$ $0:t links PAGE
11     Print the PAGE's links
12   \$ $0:t backs PAGE
13     Print who backlinks to the PAGE
14   \$ $0:t htmls DIR
15     Generate HTMLs in DIR
16 EOF
17     exit 1
18 }
19
20 [[ $# -eq 2 ]] || usage
21
22 setopt GLOB_STAR_SHORT
23 zmodload -F zsh/stat b:zstat
24 typeset -A pages
25 for p (**(.)) {
26     zstat -A reply -F "%F %T" +mtime $p
27     pages[$p]=${reply[1]}
28 }
29 typeset -a cats
30 for p (**(/)) cats=($p $cats)
31
32 zmodload zsh/mapfile
33 typeset -A links
34 typeset -A backs
35 typeset -aU words
36 for p (${(k)pages}) {
37     words=()
38     for w (${=mapfile[$p]}) {
39         [[ $w =~ "\[(.*)\]" ]] || continue
40         w=${match[1]}
41         [[ ${pages[$w]} ]] || {
42             [[ $ZK_SHOW_MISSING ]] && print "missing $w"
43             continue
44         }
45         words=($words $w)
46     }
47     [[ $words ]] && links[$p]=${(j: :)words}
48 }
49 for p ws (${(kv)links}) {
50     for w (${=ws}) backs[$w]="$p ${backs[$w]}"
51 }
52 for p ws (${(kv)backs}) backs[$p]=${(j: :)${(u)=ws}}
53
54 getrel() {
55     # nearly the copy-paste of Functions/Misc/relative
56     local dst=$2:a
57     local src=$1:h:a
58     local -a cur abs
59     cur=(${(s:/:)src})
60     abs=(${(s:/:)dst:h} $dst:t)
61     integer i=1
62     while [[ i -le $#abs && $abs[i] == $cur[i] ]] ; do
63         ((++i > $#cur)) && {
64             REPLY=${(j:/:)abs[i,-1]}
65             return
66         }
67     done
68     src=${(j:/:)cur[i,-1]/*/..}
69     dst=${(j:/:)abs[i,-1]}
70     REPLY=$src${dst:+/$dst}
71 }
72
73 genHTML() {
74     local page=$1
75     local data p
76     [[ $# -eq 1 ]] && data=${mapfile[$page]} || data=$2
77     data=${data//&/&amp;}
78     data=${data//</&lt;}
79     data=${data//>/&gt;}
80     for p (${=links[$page]}) {
81         getrel $page $p
82         data="${data//\[${p}\]/<a href=\"${REPLY}.html\">[$p]</a>}"
83     }
84     print "<\!DOCTYPE html>
85 <html><head><title>$page (${pages[$page]})</title></head><body><pre>
86 $data
87 </pre>"
88     local bs=(${(oi)=${backs[$page]}})
89     if [[ $bs ]]; then
90         print "<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>"
96     fi
97     print "</body></html>"
98 }
99
100 zmodload -F zsh/datetime b:strftime
101 strftime -s now "%F %T"
102
103 genIndex() {
104     local p
105     local entries=()
106     local _links=()
107     typeset -aU cats=()
108     local curdepth=${#${(s:/:)1}}
109     (( curdepth = curdepth + 1 ))
110     for p (${(oi)${(k)pages[(I)$1*]}}) {
111         [[ $p =~ "/Index$" ]] && continue
112         case ${#${(As:/:)p}} in
113         ($curdepth) _links=($p $_links) ;;
114         ( $(( $curdepth + 1 )) ) cats=(${1}${${p#$1}%%/*} $cats) ;;
115         (*) continue ;;
116         esac
117     }
118     for p (${(oi)_links}) entries=($entries "[$p] (${pages[$p]})")
119     if [[ $cats ]]; then
120         entries=($entries "\nSubdirectories:\n")
121         for p (${(oi)cats}) {
122             entries=($entries "[$p/Index]")
123             _links=($p/Index $_links)
124         }
125     fi
126     links[${1}Index]=${(j: :)_links}
127     genHTML ${1}Index ${(F)entries}
128 }
129
130 case $1 in
131 (links) for w (${(oi)=${links[$2]}}) print $w ;;
132 (backs) for w (${(oi)=${backs[$2]}}) print $w ;;
133 (html) genHTML $2 ;;
134 (html-index) genIndex $2 ;;
135 (htmls)
136     for p (${(k)pages}) {
137         mkdir -p $2/$p:h
138         genHTML $p > $2/$p.html
139         touch -r $p $2/$p.html
140     }
141     for p ($cats) pages[${p}/Index]=$now
142     pages[Index]=$now
143     for p ($cats) genIndex $p/ > $2/$p/Index.html
144     genIndex "" > $2/Index.html
145     for p ("" $cats) touch -d ${now/ /T} $2/$p/Index.html
146     ;;
147 (*) usage ;;
148 esac