]> Sergey Matveev's repositories - zk.zsh.git/blob - zk.zsh
eb05aff979a2a5a7796cb6a2698a62c787645e35
[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     typeset -a _links
78     [[ $# -eq 1 ]] && data=`< $page` || data=$2
79     data=${data//&/&amp;}
80     data=${data//</&lt;}
81     data=${data//>/&gt;}
82     [[ ( $page = "Index" ) || ( $page =~ "/Index" ) ]] \
83         && _links=(${(k)pages}) || _links=(${=links[$page]})
84     for p ($_links) {
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