]> Sergey Matveev's repositories - zk.zsh.git/blob - zk.zsh
Also print links
[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     local _links=(${(oi)=links[$page]})
81     for p ($_links) {
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     if [[ $_links ]]; then
90         print "<hr/>Links:<ul>"
91         for p ($_links) {
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     local bs=(${(oi)=${backs[$page]}})
98     if [[ $bs ]]; then
99         print "<hr/>Backlinks:<ul>"
100         for p ($bs) {
101             getrel $page $p
102             print "<li><a href=\"${REPLY}.html\">$p</a> <sup>${pages[$p]}</sup></li>"
103         }
104         print "</ul>"
105     fi
106     print "</body></html>"
107 }
108
109 zmodload -F zsh/datetime b:strftime
110 strftime -s now "%F %T"
111
112 genIndex() {
113     local p
114     local entries=()
115     local _links=()
116     typeset -aU cats=()
117     local curdepth=${#${(s:/:)1}}
118     (( curdepth = curdepth + 1 ))
119     for p (${(oi)${(k)pages[(I)$1*]}}) {
120         [[ $p =~ "/Index$" ]] && continue
121         case ${#${(As:/:)p}} in
122         ($curdepth) _links=($p $_links) ;;
123         ( $(( $curdepth + 1 )) ) cats=(${1}${${p#$1}%%/*} $cats) ;;
124         (*) continue ;;
125         esac
126     }
127     for p (${(oi)_links}) entries=($entries "[$p] (${pages[$p]})")
128     if [[ $cats ]]; then
129         entries=($entries "\nSubdirectories:\n")
130         for p (${(oi)cats}) {
131             entries=($entries "[$p/Index]")
132             _links=($p/Index $_links)
133         }
134     fi
135     links[${1}Index]=${(j: :)_links}
136     genHTML ${1}Index ${(F)entries}
137 }
138
139 case $1 in
140 (links) for w (${(oi)=${links[$2]}}) print $w ;;
141 (backs) for w (${(oi)=${backs[$2]}}) print $w ;;
142 (html) genHTML $2 ;;
143 (html-index) genIndex $2 ;;
144 (htmls)
145     for p (${(k)pages}) {
146         mkdir -p $2/$p:h
147         genHTML $p > $2/$p.html
148         touch -r $p $2/$p.html
149     }
150     for p ($cats) pages[${p}/Index]=$now
151     pages[Index]=$now
152     for p ($cats) genIndex $p/ > $2/$p/Index.html
153     genIndex "" > $2/Index.html
154     for p ("" $cats) touch -d ${now/ /T} $2/$p/Index.html
155     ;;
156 (*) usage ;;
157 esac