-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmint-node.rkt
executable file
·60 lines (46 loc) · 1.4 KB
/
mint-node.rkt
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
#! /usr/bin/env racket
#lang racket
(require racket/runtime-path)
(require left-pad)
(require 2htdp/batch-io)
(define-runtime-path NODE_DIR "_nodes/")
(define (path-remove-extension path)
(path-replace-extension path #""))
(define (node-path->uid-string path)
(path->string (path-remove-extension path)))
(define DIGIT_TABLE
(string->list "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"))
(define BASE
(length DIGIT_TABLE))
(define (uid-string->int uid)
(define digits (string->list uid))
(for/fold ([sum 0]
[place 1]
#:result sum)
([x (in-list (reverse digits))])
(values
(+ sum (* place (index-of DIGIT_TABLE x)))
(* BASE place))))
(define (int->uid-string ix)
(do ([digits
(list)
(cons (list-ref DIGIT_TABLE (modulo n BASE)) digits)]
[n ix (quotient n BASE)])
((<= n 0)
(left-pad (list->string digits) 4 "0"))))
(define (node-path->int path)
(uid-string->int (node-path->uid-string path)))
(define (uid-committed? uid-string)
(not (string-prefix? uid-string "_")))
(define all-committed-uids
(filter
uid-committed?
(map
node-path->uid-string
(directory-list NODE_DIR))))
(define next-uid-string
(int->uid-string
(+ 1
(apply max (map uid-string->int all-committed-uids)))))
(printf "Next node: ~a\n" next-uid-string)
(write-file (string-append "_nodes/" next-uid-string ".md") "---\ntitle:\n---")