X-Git-Url: http://www.git.stargrave.org/?a=blobdiff_plain;f=zk.zsh;h=9b65aecc25db5e0c1709f2717b8651bd94a46d5c;hb=HEAD;hp=9b2d0a5426a106855aed0bf6eb8b75ca43292d99;hpb=b4b5b51ec351b6bf7964db72b04671506d610f9f;p=zk.zsh.git diff --git a/zk.zsh b/zk.zsh index 9b2d0a5..9b65aec 100755 --- a/zk.zsh +++ b/zk.zsh @@ -1,12 +1,12 @@ #!/usr/bin/env zsh # zk.zsh -- zettelkästen/wiki/static website helper/generator -# Copyright (C) 2022 Sergey Matveev +# Copyright (C) 2022-2024 Sergey Matveev set -e ZK_VERSION=ZKZSH1 usage() { - cat >&2 <&2 <&2 + exit 1 + } zstat -A mtime -F "%F %T" +mtime $p zstat -A size +size $p pages[$p]=${mtime[1]} sizes[$p]=${size[1]} } -typeset -a cats -for p (**(/)) cats=($p $cats) +typeset -A cats +for p (**(/)) cats[$p]=1 zmodload zsh/mapfile zmodload -F zsh/files b:zf_mkdir @@ -57,6 +62,10 @@ for p (${(k)pages}) { for w (${=mapfile[$p]}) { [[ $w =~ "\[([^] ]+)\]" ]] || continue w=${match[1]} + [[ ( $w =~ "/$" ) && ( ${cats[$w[1,-2]]} ) ]] && { + ws=($ws $w) + continue + } [[ ${pages[$w]} ]] || { [[ $ZK_SHOW_MISSING ]] && print "missing $w" continue @@ -67,7 +76,7 @@ for p (${(k)pages}) { zf_mkdir -p $ZK_CACHE/$p:h ws=($ZK_VERSION ${inode[1]} ${ctime[1]} $ws) print -l $ws > $ZK_CACHE/$p - ws=(${ws[3,-1]}) + ws=(${ws[4,-1]}) } [[ $ws ]] && links[$p]=${(j: :)ws} } @@ -102,21 +111,25 @@ genHTML() { [[ $# -eq 1 ]] && data=${mapfile[$page]} || data=$2 local _links=(${(oi)=links[$page]}) if [[ ( ${cached[$page]} ) && ( -s $ZK_CACHE/${page}.html ) ]]; then - cat $ZK_CACHE/${page}.html + < $ZK_CACHE/${page}.html else data=${data//&/&} data=${data///>} for p ($_links) { getrel $page $p + [[ -d $p ]] && REPLY=$REPLY/index data="${data//\[${p}\]/[$p]}" } data=" -$page (${pages[$page]})
+
+$page (${pages[$page]})
+
+
 $data
" if [[ $ZK_CACHE ]]; then print -r "$data" > $ZK_CACHE/${page}.html - cat $ZK_CACHE/${page}.html + < $ZK_CACHE/${page}.html else print -r "$data" fi @@ -148,28 +161,45 @@ genIndex() { local p local entries=() local _links=() - typeset -aU cats=() + typeset -aU _cats=() local curdepth=${#${(s:/:)1}} (( curdepth = curdepth + 1 )) - for p (${(oi)${(k)pages[(I)$1*]}}) { - [[ ( $p =~ "/index$" ) || ( $p = "index" ) ]] && continue + for p (${(k)pages[(I)$1*]}) { case ${#${(As:/:)p}} in ($curdepth) _links=($p $_links) ;; - ( $(( $curdepth + 1 )) ) cats=(${1}${${p#$1}%%/*} $cats) ;; + ( $(( $curdepth + 1 )) ) _cats=(${1}${${p#$1}%%/*} $_cats) ;; (*) continue ;; esac } - for p (${(oi)_links}) \ - entries=($entries "[$p] (${pages[$p]}) (${sizes[$p]} bytes)") - if [[ $cats ]]; then - entries=($entries " " "Subdirectories:" " ") - for p (${(oi)cats}) { - entries=($entries "[$p/index]") - _links=($p/index $_links) + local page=${1}index + print " + + +$page ($now) +
    " + for p (${(oi)_links}) { + getrel $page $p + print "
  • $p ${pages[$p]} (${sizes[$p]} bytes)
  • " + } + print "
" + if [[ $_cats ]]; then + print "
Subdirectories:
    " + for p (${(oi)_cats}) { + getrel $page $p/index + print "
  • $p
  • " + } + print "
" + fi + local bs=(${(oi)=${backs[$1]}}) + if [[ $bs ]]; then + print "
Backlinks:
    " + for p ($bs) { + getrel $page $p + print "
  • $p ${pages[$p]}
  • " } + print "
" fi - links[${1}index]=${(j: :)_links} - genHTML ${1}index ${(F)entries} + print "" } case $1 in @@ -183,11 +213,9 @@ case $1 in genHTML $p > $2/$p.html touch -r $p $2/$p.html } - for p ($cats) pages[${p}/index]=$now - pages[index]=$now - for p ($cats) genIndex $p/ > $2/$p/index.html + for p (${(k)cats}) genIndex $p/ > $2/$p/index.html genIndex "" > $2/index.html - for p ("" $cats) touch -d ${now/ /T} $2/$p/index.html + for p ("" ${(k)cats}) touch -d ${now/ /T} $2/$p/index.html ;; (*) usage ;; esac