]> Sergey Matveev's repositories - zk.zsh.git/commitdiff
Caching
authorSergey Matveev <stargrave@stargrave.org>
Tue, 15 Mar 2022 17:55:03 +0000 (20:55 +0300)
committerSergey Matveev <stargrave@stargrave.org>
Tue, 15 Mar 2022 18:14:38 +0000 (21:14 +0300)
CACHE [new file with mode: 0644]
README
zk.zsh

diff --git a/CACHE b/CACHE
new file mode 100644 (file)
index 0000000..fb583ef
--- /dev/null
+++ b/CACHE
@@ -0,0 +1,13 @@
+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.
diff --git a/README b/README
index 32abec060cb87c744db0d1e69bcc70e9fbef8cf4..206b6dd949ec80ec0761d014eb01aab822c00ecd 100644 (file)
--- a/README
+++ b/README
@@ -20,23 +20,24 @@ zk.zsh -- zettelkästen/wiki/static website helper/generator
 * 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.
diff --git a/zk.zsh b/zk.zsh
index 7d5f556236057922836232c478666b97ef469435..a4984cc0fa3731550e40388b537bc812fb762dc4 100755 (executable)
--- a/zk.zsh
+++ b/zk.zsh
@@ -3,6 +3,7 @@
 # Copyright (C) 2022 Sergey Matveev <stargrave@stargrave.org>
 
 set -e
+ZK_VERSION=ZKZSH1
 
 usage() {
     cat >&2 <<EOF
@@ -36,8 +37,22 @@ zmodload zsh/mapfile
 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
@@ -48,9 +63,15 @@ for p (${(k)pages}) {
         }
         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]}"
 }
@@ -79,18 +100,27 @@ genHTML() {
     local page=$1
     local data p
     [[ $# -eq 1 ]] && data=${mapfile[$page]} || data=$2
-    data=${data//&/&amp;}
-    data=${data//</&lt;}
-    data=${data//>/&gt;}
     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//&/&amp;}
+        data=${data//</&lt;}
+        data=${data//>/&gt;}
+        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) {
@@ -132,7 +162,7 @@ genIndex() {
     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)