]> Sergey Matveev's repositories - zk.zsh.git/blob - zk.zsh
More performance optimizations
[zk.zsh.git] / zk.zsh
1 #!/usr/bin/env zsh
2 # zk.zsh -- zettelkästen-related helper
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 # Collect all pages
23 setopt GLOB_STAR_SHORT
24 zmodload -F zsh/stat b:zstat
25 typeset -A pages
26 for p (**(.)) {
27     zstat -A reply -F "%F %T" +mtime $p
28     pages[$p]=${reply[1]}
29 }
30 typeset -a cats
31 for p (**(/)) cats=($p $cats)
32
33 # Determine the links between them
34 typeset -A links
35 typeset -A backs
36 typeset -aU words
37 for p (${(k)pages}) {
38     words=()
39     for w (`< $p`) {
40         [[ $w =~ "\[(.*)\]" ]] || continue
41         w=${match[1]}
42         [[ ${pages[$w]} ]] || {
43             [[ $ZK_SHOW_MISSING ]] && print "missing $w"
44             continue
45         }
46         words=($words $w)
47     }
48     [[ $words ]] && links[$p]=${(j: :)words}
49 }
50 for p ws (${(kv)links}) {
51     for w (${=ws}) backs[$w]="$p ${backs[$w]}"
52 }
53 for p ws (${(kv)backs}) backs[$p]=${(j: :)${(u)=ws}}
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 (${=links[$page]}) {
82         getrel $page $p
83         data="${data//\[${p}\]/<a href=\"${REPLY}.html\">[$p]</a>}"
84     }
85     print "<\!DOCTYPE html>
86 <html><head><title>$page (${pages[$page]})</title></head><body><pre>
87 $data
88 </pre>"
89     local bs=(${(oi)=${backs[$page]}})
90     if [[ $bs ]]; then
91         print "<hr/><ul>"
92         for p (${(oi)=${backs[$page]}}) {
93             getrel $page $p
94             print "<li><a href=\"${REPLY}.html\">$p</a> <sup>${pages[$p]}</sup></li>"
95         }
96         print "</ul>"
97     fi
98     print "</body></html>"
99 }
100
101 zmodload -F zsh/datetime b:strftime
102 now=$(strftime "%F %T")
103
104 genIndex() {
105     local p
106     local entries=()
107     local _links=()
108     typeset -aU cats=()
109     local curdepth=${#${(s:/:)1}}
110     (( curdepth = curdepth + 1 ))
111     for p (${(oi)${(k)pages[(I)$1*]}}) {
112         [[ $p =~ "/Index$" ]] && continue
113         case ${#${(As:/:)p}} in
114         ($curdepth) _links=($p $_links) ;;
115         ( $(( $curdepth + 1 )) ) cats=(${1}${${p#$1}%%/*} $cats) ;;
116         (*) continue ;;
117         esac
118     }
119     for p (${(oi)_links}) entries=($entries "[$p] (${pages[$p]})")
120     if [[ $cats ]]; then
121         entries=($entries "\nBacklinks:\n")
122         for p (${(oi)cats}) {
123             entries=($entries "[$p/Index]")
124             _links=($p/Index $_links)
125         }
126     fi
127     links[${1}Index]=${(j: :)_links}
128     genHTML ${1}Index ${(F)entries}
129 }
130
131 case $1 in
132 (links) for w (${(oi)=${links[$2]}}) print $w ;;
133 (backs) for w (${(oi)=${backs[$2]}}) print $w ;;
134 (html) genHTML $2 ;;
135 (html-index) genIndex $2 ;;
136 (htmls)
137     for p (${(k)pages}) {
138         mkdir -p $2/$p:h
139         genHTML $p > $2/$p.html
140         touch -r $p $2/$p.html
141     }
142     for p ($cats) pages[${p}/Index]=$now
143     pages[Index]=$now
144     for p ($cats) genIndex $p/ > $2/$p/Index.html
145     genIndex "" > $2/Index.html
146     for p ("" $cats) touch -d ${now/ /T} $2/$p/Index.html
147     ;;
148 (*) usage ;;
149 esac