From 22b95c42c5b096a3206f80e38b71aac3565ab6b7 Mon Sep 17 00:00:00 2001 From: Ben Sima Date: Mon, 11 May 2020 22:13:59 -0700 Subject: Refactor: nodes are now objects --- z.scm | 114 ++++++++++++++++++++++++++++++++++++++++-------------------------- 1 file changed, 70 insertions(+), 44 deletions(-) diff --git a/z.scm b/z.scm index 6d203fa..e379066 100644 --- a/z.scm +++ b/z.scm @@ -2,6 +2,7 @@ (import (prefix (Alpha String) string.)) (import (srfi srfi-1)) (import (sxml simple)) +(import (oop goops)) (import (only (srfi srfi-19) date->string current-date)) (import (only (ice-9 match) match)) (import (prefix (dict) dict.)) @@ -89,47 +90,72 @@ 0 (apply max ls))) -(define (make-node) - (let ([path (-> (list-nodes) latest-node inc id->path)]) - (call-with-output-file path - - (fn [file] - (format file "title:\n") - (format file "created: ~a\n" - (date->string (current-date) "~Y.~m.~d..~H.~M")) - (format file "tags:\n") - (display "---\n" file))) - path)) - -(define (read-node id) - (readlines (id->path id))) - -(define (cat-node id) - (prn (read-node id))) - - ;; Metadata - -(define (get-title node) - (-> node +(define (next-id) + (-> (list-nodes) latest-node inc)) + +(define-class () + (id #:getter id #:init-keyword #:id) + (title #:accessor title #:init-keyword #:title) + (created #:getter created + #:init-form (date->string (current-date) "~Y.~m.~d..~H.~M")) + (tags #:accessor tags #:init-keyword #:tags) + (content #:accessor content #:init-keyword #:content) + (path #:getter path + #:allocation #:virtual + #:slot-set! (lambda (_) #f) + #:slot-ref (lambda (self) + (id->path (id self))))) + +(define-generic save-node!) +(define-method (save-node! (node )) + (call-with-output-file (path node) + (fn [file] + (format file "title: ~a\n" (title node)) + (format file "created: ~a\n" (created node)) + (format file "tags: ~{~a ~}" (tags node)) + (display "---\n" file) + (format file "~a" (content node))))) + +(define (parse-title node-data) + (-> node-data (re.match "title: ([^\n]*)") (re.group 1))) -(define (get-tags node) - (-> node +(define (parse-tags node-data) + (-> node-data (re.match "tags: ([^\n]*)") (re.group 1) (string.split #\space) seq)) +(define (parse-created node-data) + (-> node-data + (re.match "created: ([^\n]*)") + (re.group 1))) + +(define (parse-content node-data) + (-> node-data + (re.match "---\n(.*)$") + (re.group 1))) + +(define (load-node id) + (let ([data (readlines (id->path id))]) + (make + #:id id + #:title (parse-title data) + #:created (parse-created data) + #:tags (parse-tags data) + #:content (parse-content data)))) + ;; Indexing (define *tags* (dict.empty)) (define *titles* (dict.empty)) (define (index-node id) - (let ([node (read-node id)]) - (dict.set *titles* (get-title node) id) - (for (get-tags node) + (let ([node (load-node id)]) + (dict.set *titles* (title node) id) + (for (tags node) (/. tag (if tag (dict.update @@ -157,10 +183,7 @@ (for (list-nodes) (lambda (id) (pr id) (pr ": ") - (prn (get-title (read-node id)))))) - -(define (print-node id) - (pr (read-node id))) + (prn (title (load-node id)))))) ;; webserver @@ -221,11 +244,12 @@ (lambda (id) `(li (a (@ (href ,(fmt "/node/~a" id))) ,(fmt "~a: ~a" - id (get-title (read-node id))))))))))] + id (title (load-node id))))))))))] [("node" id) - (let ([node (read-node (string->id id))]) - (respond `((pre ,node)) - #:title (fmt "z - ~a" (get-title node))))] + (let* ([node (load-node (string->id id))] + [txt (or (content node) "")]) + (respond `((pre ,txt)) + #:title (fmt "z - ~a" (title node))))] [("tag") (let ((tags (dict.keys *tags*))) (reindex) @@ -242,11 +266,11 @@ `(li (a (@ (href ,(fmt "/node/~a" id))) ,(fmt "~a: ~a" id - (get-title (read-node id)))))))))))] + (title (load-node id)))))))))))] [_ (respond "not found")])) (define* (serve #:key (port 8080)) - (prn "z server") + (prn "z") (prn (fmt "port ~a" port)) (http.run-server routes 'http `(#:port ,port))) @@ -264,9 +288,9 @@ where 'command' is: (define (main args) (match (rest args) ['() - (let ([path (make-node)] + (let ([node (make )] [editor (os.getenv "EDITOR")]) - (subprocess.call (list editor path)) + (subprocess.call (list editor (path node))) (exit EXIT_SUCCESS))] [("ls") (print-titles)] [("tagged" tag) @@ -274,11 +298,13 @@ where 'command' is: (reindex) (for (tagged tag) (lambda (id) - (let* ([node (read-node id)] - [title (get-title node)]) - (pr id) - (pr ": ") - (pr title)))))] + (pr id) + (pr ": ") + (pr (title (load-node id))))))] + [("tags") + (begin + (reindex) + (map prn (dict.keys *tags*)))] [("web") (serve)] [("web" port) (serve #:port (string->number port))] [_ (usage)])) -- cgit v1.2.3