From 25216ec2e522ff49b4a8c1bf9415f79667aa68a5 Mon Sep 17 00:00:00 2001 From: Sergey Matveev Date: Tue, 15 Mar 2022 20:55:03 +0300 Subject: [PATCH] Caching --- CACHE | 13 +++++++++++++ README | 23 ++++++++++++----------- zk.zsh | 54 ++++++++++++++++++++++++++++++++++++++++++------------ 3 files changed, 67 insertions(+), 23 deletions(-) create mode 100644 CACHE diff --git a/CACHE b/CACHE new file mode 100644 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
-tag.
+Links and backlinks are generated every time.
diff --git a/README b/README
index 32abec0..206b6dd 100644
--- 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 7d5f556..a4984cc 100755
--- a/zk.zsh
+++ b/zk.zsh
@@ -3,6 +3,7 @@
 # Copyright (C) 2022 Sergey Matveev 
 
 set -e
+ZK_VERSION=ZKZSH1
 
 usage() {
     cat >&2 < $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//&/&}
-    data=${data///>}
     local _links=(${(oi)=links[$page]})
-    for p ($_links) {
-        getrel $page $p
-        data="${data//\[${p}\]/[$p]}"
-    }
-    print -r "<\!DOCTYPE html>
+    if [[ ( ${cached[$page]} ) && ( -s $ZK_CACHE/${page}.html ) ]]; then
+        cat $ZK_CACHE/${page}.html
+    else
+        data=${data//&/&}
+        data=${data///>}
+        for p ($_links) {
+            getrel $page $p
+            data="${data//\[${p}\]/[$p]}"
+        }
+        data="
 $page (${pages[$page]})
-$data
-
" +$data
" + 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 "
Links: