]> Sergey Matveev's repositories - zk.zsh.git/blob - zk.zsh
cfc95a4796212116896c973267ab707a6c74552f
[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 PAGE's links
12   \$ $0:t backs PAGE
13     Print PAGE's backlinks
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 ws
36 for p (${(k)pages}) {
37     ws=()
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         ws=($ws $w)
46     }
47     [[ $ws ]] && links[$p]=${(j: :)ws}
48 }
49 unset ws
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=${mapfile[$page]} || data=$2
78     data=${data//&/&amp;}
79     data=${data//</&lt;}
80     data=${data//>/&gt;}
81     local _links=(${(oi)=links[$page]})
82     for p ($_links) {
83         getrel $page $p
84         data="${data//\[${p}\]/<a href=\"${REPLY}.html\">[$p]</a>}"
85     }
86     print -r "<\!DOCTYPE html>
87 <html><head><title>$page (${pages[$page]})</title></head><body><pre>
88 $data
89 </pre>"
90     if [[ $_links ]]; then
91         print "<hr/>Links:<ul>"
92         for p ($_links) {
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     local bs=(${(oi)=${backs[$page]}})
99     if [[ $bs ]]; then
100         print "<hr/>Backlinks:<ul>"
101         for p ($bs) {
102             getrel $page $p
103             print "<li><a href=\"${REPLY}.html\">$p</a> <sup>${pages[$p]}</sup></li>"
104         }
105         print "</ul>"
106     fi
107     print "</body></html>"
108 }
109
110 zmodload -F zsh/datetime b:strftime
111 strftime -s now "%F %T"
112
113 genIndex() {
114     local p
115     local entries=()
116     local _links=()
117     typeset -aU cats=()
118     local curdepth=${#${(s:/:)1}}
119     (( curdepth = curdepth + 1 ))
120     for p (${(oi)${(k)pages[(I)$1*]}}) {
121         [[ $p =~ "/Index$" ]] && continue
122         case ${#${(As:/:)p}} in
123         ($curdepth) _links=($p $_links) ;;
124         ( $(( $curdepth + 1 )) ) cats=(${1}${${p#$1}%%/*} $cats) ;;
125         (*) continue ;;
126         esac
127     }
128     for p (${(oi)_links}) entries=($entries "[$p] (${pages[$p]})")
129     if [[ $cats ]]; then
130         entries=($entries "\nSubdirectories:\n")
131         for p (${(oi)cats}) {
132             entries=($entries "[$p/Index]")
133             _links=($p/Index $_links)
134         }
135     fi
136     links[${1}Index]=${(j: :)_links}
137     genHTML ${1}Index ${(F)entries}
138 }
139
140 case $1 in
141 (links) for w (${(oi)=${links[$2]}}) print $w ;;
142 (backs) for w (${(oi)=${backs[$2]}}) print $w ;;
143 (html) genHTML $2 ;;
144 (html-index) genIndex $2 ;;
145 (htmls)
146     for p (${(k)pages}) {
147         mkdir -p $2/$p:h
148         genHTML $p > $2/$p.html
149         touch -r $p $2/$p.html
150     }
151     for p ($cats) pages[${p}/Index]=$now
152     pages[Index]=$now
153     for p ($cats) genIndex $p/ > $2/$p/Index.html
154     genIndex "" > $2/Index.html
155     for p ("" $cats) touch -d ${now/ /T} $2/$p/Index.html
156     ;;
157 (*) usage ;;
158 esac