From fe73a3bae04f01d3aafdc3d926b37017fce6bf40 Mon Sep 17 00:00:00 2001 From: Ben Sima Date: Sat, 9 May 2020 17:53:32 -0700 Subject: Add cat-node, and a bit of refactoring --- z.scm | 47 ++++++++++++++++++++++++++++++++++------------- 1 file changed, 34 insertions(+), 13 deletions(-) diff --git a/z.scm b/z.scm index e897f86..0267a7d 100644 --- a/z.scm +++ b/z.scm @@ -1,18 +1,17 @@ (import (Alpha Core)) -(import (Alpha String)) -(import (only (Alpha String) replace)) -(import (only (ice-9 ftw) scandir)) -(import (only (srfi srfi-1) remove)) +(import (prefix (Alpha String) string/)) ;; general functions +(import (only (ice-9 ftw) scandir)) (define (os.listdir path) (scandir path)) (define (os.path.expanduser path) (let ([home (getenv "HOME")]) - (replace path "~" home))) + (string/replace path "~" home))) +(import (only (srfi srfi-1) remove)) (define (rm ls item) (remove (/. x (eq? x item)) ls)) @@ -23,6 +22,12 @@ [else (set.difference (rest s1) s2)])) +(import (only (ice-9 rdelim) read-delimited)) +(define (readlines fname) + (call-with-input-file fname + (lambda [p] + (read-delimited "" p 'concat)))) + ;; z program (define *zdir* (os.path.expanduser "~/test-z-wiki")) @@ -44,8 +49,8 @@ (define (path->id fname) "Given a path, parse out the node id." (-> fname - (replace ".md" "") - (replace *zdir* "") + (string/replace ".md" "") + (string/replace *zdir* "") (string->id))) (define (id->path id) @@ -56,21 +61,37 @@ ;; Manipulating nodes -(define (list-nodes dir) +(define (list-nodes* dir) (->> dir (os.listdir) ;; remove '.' and '..' ((/. s (set.difference s '("." "..")))) (map path->id))) +(define (list-nodes) + (list-nodes* *zdir*)) + (define (latest-node ls) (if (eq? ls '()) 0 (apply max ls))) (define (make-node title) - (let ([id (inc (-> *zdir* list-nodes latest-node))]) - (call-with-output-file (id->path id) - (fn [file] - (format file "title: ~a" title) - (newline file))))) + (call-with-output-file + (-> (list-nodes) latest-node inc id->path) + (fn [file] + (format file "title: ~a\n" title) + (format file "created: ~a\n" + (date->string (current-date) "~Y.~m.~d..~H.~M")) + (format file "tags:\n") + (display "---\n" file)))) + +(define (cat-node id) + (prn (readlines (id->path id)))) + +;; Indexing + +(comment + (define index-titles) + (define index-links) + (define index-tags)) -- cgit v1.2.3