--- /dev/null
+Cache directory resembles the notes hierarchy. Each file corresponds to
+the note file, keeping at least three lines: versioned magic number,
+inode number and ctime value in seconds. All of that is used to
+determine if note was changed and cache is valid.
+
+All other lines contain the links present in the note.
+
+Any change to the file leads to ctime changing and cache invalidation
+with full parsing of the note.
+
+If note's cache is valid, then also the part of HTML is also kept in
+.html-file nearby. It contains all the data till closing <pre>-tag.
+Links and backlinks are generated every time.
* Ordinary grep, git-jump or similar tools can be used to search
and quickly open results in the editor
-The only thing Vim lacks there is ability to tell who
-backreferences to the specified page. zk.zsh can be used to show
-what pages backreferences to specified page and what pages are
-referenced by it:
+The only thing Vim lacks there is ability to tell who backlinks
+to the specified page. zk.zsh can be used to show what pages
+backlinks to specified page and what pages are referenced by it:
$ zk.zsh links some/page
Another/Page
SomePage
$ zk.zsh backs some/page
[...]
That can be used to make categories and tags on notes. If note
-contains a link to category/tag, then it will be backreferenced.
-
-Currently it does not use any kind of database or cache. It
-parses all files every time.
+contains a link to category/tag (even an empty file), then it
+will be backlinked.
$ zk.zsh htmls path/to/dir
Will convert all your notes to HTMLs with properly created links
-to other pages. It also will include all backreference links in
-them. Each directory will also contain Index page with links to
-all existing pages in current directory and to subdirectories.
+to other pages. It also will include all backlinks in them. Each
+directory will also contain index page with links to all
+existing pages in current directory and to subdirectories.
+
+If ZK_CACHE environment variable contains path to some
+directory, then it will keep caching information for speeding up
+the processes in it. Look for CACHE file for more information.
# Copyright (C) 2022 Sergey Matveev <stargrave@stargrave.org>
set -e
+ZK_VERSION=ZKZSH1
usage() {
cat >&2 <<EOF
zmodload -F zsh/files b:zf_mkdir
typeset -A links
typeset -A backs
+typeset -A cached
typeset -aU ws
for p (${(k)pages}) {
+ [[ $ZK_CACHE ]] && {
+ zstat -A inode +inode $p
+ zstat -A ctime +ctime $p
+ cache=(${(f)mapfile[$ZK_CACHE/$p]})
+ if [[ ( ${cache[1]} = $ZK_VERSION ) &&
+ ( ${cache[2]} = ${inode[1]} ) &&
+ ( ${cache[3]} = ${ctime[1]} ) ]]; then
+ ws=(${cache[4,-1]})
+ [[ $ws ]] && links[$p]=${(j: :)ws}
+ cached[$p]=1
+ continue
+ fi
+ }
ws=()
for w (${=mapfile[$p]}) {
[[ $w =~ "\[([^] ]+)\]" ]] || continue
}
ws=($ws $w)
}
+ [[ $ZK_CACHE ]] && {
+ 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 ]] && links[$p]=${(j: :)ws}
}
-unset ws
+unset cache ws
for p ws (${(kv)links}) {
for w (${=ws}) backs[$w]="$p ${backs[$w]}"
}
local page=$1
local data p
[[ $# -eq 1 ]] && data=${mapfile[$page]} || data=$2
- data=${data//&/&}
- data=${data//</<}
- data=${data//>/>}
local _links=(${(oi)=links[$page]})
- for p ($_links) {
- getrel $page $p
- data="${data//\[${p}\]/<a href=\"${REPLY}.html\">[$p]</a>}"
- }
- print -r "<\!DOCTYPE html>
+ if [[ ( ${cached[$page]} ) && ( -s $ZK_CACHE/${page}.html ) ]]; then
+ cat $ZK_CACHE/${page}.html
+ else
+ data=${data//&/&}
+ data=${data//</<}
+ data=${data//>/>}
+ for p ($_links) {
+ getrel $page $p
+ data="${data//\[${p}\]/<a href=\"${REPLY}.html\">[$p]</a>}"
+ }
+ data="<!DOCTYPE html>
<html><head><title>$page (${pages[$page]})</title></head><body><pre>
-$data
-</pre>"
+$data</pre>"
+ if [[ $ZK_CACHE ]]; then
+ print -r "$data" > $ZK_CACHE/${page}.html
+ cat $ZK_CACHE/${page}.html
+ else
+ print -r "$data"
+ fi
+ fi
if [[ $_links ]]; then
print "<hr/>Links:<ul>"
for p ($_links) {
for p (${(oi)_links}) \
entries=($entries "[$p] (${pages[$p]}) (${sizes[$p]} bytes)")
if [[ $cats ]]; then
- entries=($entries "\nSubdirectories:\n")
+ entries=($entries " " "Subdirectories:" " ")
for p (${(oi)cats}) {
entries=($entries "[$p/Index]")
_links=($p/Index $_links)