diff options
author | Ben Sima <ben@bsima.me> | 2020-05-09 15:03:37 -0700 |
---|---|---|
committer | Ben Sima <ben@bsima.me> | 2020-05-09 15:03:37 -0700 |
commit | 7286ec83c3e6e9781a5e8eea3298a807764a80c5 (patch) | |
tree | 65895ad580989efa75d3c973edeb950ff249e92c | |
parent | f0f74f93d6cce751a4aef2576f7bce837f7b23c2 (diff) |
Init z
-rw-r--r-- | z.scm | 76 |
1 files changed, 76 insertions, 0 deletions
@@ -0,0 +1,76 @@ +(import (Alpha Core)) +(import (Alpha String)) +(import (only (Alpha String) replace)) +(import (only (ice-9 ftw) scandir)) +(import (only (srfi srfi-1) remove)) + +;; general functions + +(define (os.listdir path) + (scandir path)) + +(define (os.path.expanduser path) + (let ([home (getenv "HOME")]) + (replace path "~" home))) + +(define (rm ls item) + (remove (/. x (eq? x item)) ls)) + +(define (set.difference s1 s2) + (cond [(null? s1) '()] + [(not (member? (first s1) s2)) + (cons (first s1) (set.difference (rest s1) s2))] + [else + (set.difference (rest s1) s2)])) + +;; z program + +(define *zdir* (os.path.expanduser "~/test-z-wiki")) + +;; A node 'id' is a monotonically increasing number. + +;; It can be represented as a string, in which case it is encoded in +;; base 36. + +(define (id->string id) + (number->string id 36)) + +(define (string->id s) + (string->number s 36)) + +;; It can be represented as a path, in which case it is +;; `<*zdir*>/<string>.md' + +(define (path->id fname) + "Given a path, parse out the node id." + (-> fname + (replace ".md" "") + (replace *zdir* "") + (string->id))) + +(define (id->path id) + "Given an id, return the absolute path to the file." + (fmt "~a/~a.md" + *zdir* + (id->string id))) + +;; Manipulating nodes + +(define (list-nodes dir) + (->> dir + (os.listdir) + ;; remove '.' and '..' + ((/. s (set.difference s '("." "..")))) + (map path->id))) + +(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))))) |