summaryrefslogtreecommitdiff
path: root/z.scm
blob: 0267a7d7f47cb8bdd7481fa06bf51077ad03efbf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
(import (Alpha Core))
(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")])
    (string/replace path "~" home)))

(import (only (srfi srfi-1) remove))
(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)]))

(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"))

;; 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
      (string/replace ".md" "")
      (string/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 (list-nodes)
  (list-nodes* *zdir*))

(define (latest-node ls)
  (if (eq? ls '())
      0
      (apply max ls)))

(define (make-node title)
  (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))