]> Sergey Matveev's repositories - zk.zsh.git/blob - zk.zsh
7d5f556236057922836232c478666b97ef469435
[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 typeset -A sizes
26 for p (**(.)) {
27     zstat -A mtime -F "%F %T" +mtime $p
28     zstat -A size +size $p
29     pages[$p]=${mtime[1]}
30     sizes[$p]=${size[1]}
31 }
32 typeset -a cats
33 for p (**(/)) cats=($p $cats)
34
35 zmodload zsh/mapfile
36 zmodload -F zsh/files b:zf_mkdir
37 typeset -A links
38 typeset -A backs
39 typeset -aU ws
40 for p (${(k)pages}) {
41     ws=()
42     for w (${=mapfile[$p]}) {
43         [[ $w =~ "\[([^] ]+)\]" ]] || continue
44         w=${match[1]}
45         [[ ${pages[$w]} ]] || {
46             [[ $ZK_SHOW_MISSING ]] && print "missing $w"
47             continue
48         }
49         ws=($ws $w)
50     }
51     [[ $ws ]] && links[$p]=${(j: :)ws}
52 }
53 unset ws
54 for p ws (${(kv)links}) {
55     for w (${=ws}) backs[$w]="$p ${backs[$w]}"
56 }
57 for p ws (${(kv)backs}) backs[$p]=${(j: :)${(u)=ws}}
58
59 getrel() {
60     # nearly the copy-paste of Functions/Misc/relative
61     local dst=$2:a
62     local src=$1:h:a
63     local -a cur abs
64     cur=(${(s:/:)src})
65     abs=(${(s:/:)dst:h} $dst:t)
66     integer i=1
67     while [[ i -le $#abs && $abs[i] == $cur[i] ]] ; do
68         ((++i > $#cur)) && {
69             REPLY=${(j:/:)abs[i,-1]}
70             return
71         }
72     done
73     src=${(j:/:)cur[i,-1]/*/..}
74     dst=${(j:/:)abs[i,-1]}
75     REPLY=$src${dst:+/$dst}
76 }
77
78 genHTML() {
79     local page=$1
80     local data p
81     [[ $# -eq 1 ]] && data=${mapfile[$page]} || data=$2
82     data=${data//&/&amp;}
83     data=${data//</&lt;}
84     data=${data//>/&gt;}
85     local _links=(${(oi)=links[$page]})
86     for p ($_links) {
87         getrel $page $p
88         data="${data//\[${p}\]/<a href=\"${REPLY}.html\">[$p]</a>}"
89     }
90     print -r "<\!DOCTYPE html>
91 <html><head><title>$page (${pages[$page]})</title></head><body><pre>
92 $data
93 </pre>"
94     if [[ $_links ]]; then
95         print "<hr/>Links:<ul>"
96         for p ($_links) {
97             getrel $page $p
98             print "<li><a href=\"${REPLY}.html\">$p</a> <sup>${pages[$p]}</sup></li>"
99         }
100         print "</ul>"
101     fi
102     local bs=(${(oi)=${backs[$page]}})
103     if [[ $bs ]]; then
104         print "<hr/>Backlinks:<ul>"
105         for p ($bs) {
106             getrel $page $p
107             print "<li><a href=\"${REPLY}.html\">$p</a> <sup>${pages[$p]}</sup></li>"
108         }
109         print "</ul>"
110     fi
111     print "</body></html>"
112 }
113
114 zmodload -F zsh/datetime b:strftime
115 strftime -s now "%F %T"
116
117 genIndex() {
118     local p
119     local entries=()
120     local _links=()
121     typeset -aU cats=()
122     local curdepth=${#${(s:/:)1}}
123     (( curdepth = curdepth + 1 ))
124     for p (${(oi)${(k)pages[(I)$1*]}}) {
125         [[ $p =~ "/Index$" ]] && continue
126         case ${#${(As:/:)p}} in
127         ($curdepth) _links=($p $_links) ;;
128         ( $(( $curdepth + 1 )) ) cats=(${1}${${p#$1}%%/*} $cats) ;;
129         (*) continue ;;
130         esac
131     }
132     for p (${(oi)_links}) \
133         entries=($entries "[$p] (${pages[$p]}) (${sizes[$p]} bytes)")
134     if [[ $cats ]]; then
135         entries=($entries "\nSubdirectories:\n")
136         for p (${(oi)cats}) {
137             entries=($entries "[$p/Index]")
138             _links=($p/Index $_links)
139         }
140     fi
141     links[${1}Index]=${(j: :)_links}
142     genHTML ${1}Index ${(F)entries}
143 }
144
145 case $1 in
146 (links) for w (${(oi)=${links[$2]}}) print $w ;;
147 (backs) for w (${(oi)=${backs[$2]}}) print $w ;;
148 (html) genHTML $2 ;;
149 (html-index) genIndex $2 ;;
150 (htmls)
151     for p (${(k)pages}) {
152         zf_mkdir -p $2/$p:h
153         genHTML $p > $2/$p.html
154         touch -r $p $2/$p.html
155     }
156     for p ($cats) pages[${p}/Index]=$now
157     pages[Index]=$now
158     for p ($cats) genIndex $p/ > $2/$p/Index.html
159     genIndex "" > $2/Index.html
160     for p ("" $cats) touch -d ${now/ /T} $2/$p/Index.html
161     ;;
162 (*) usage ;;
163 esac