-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwiki.rkt
executable file
·80 lines (67 loc) · 2.63 KB
/
wiki.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#!/bin/env racket
#lang racket/base
(require racket/cmdline)
(require racket/file)
(require racket/match)
(require racket/system)
(require racket/string)
(define wiki-root-directory (getenv "WIKI_PATH"))
(define (wiki/get-page-path page-name)
(path-add-extension
(build-path wiki-root-directory page-name) #".md"))
(define (wiki/get-page-content page-path)
(file->string page-path))
(define (wiki/create-page page-name)
(display-to-file
(string-append "#" page-name "#")
(wiki/get-page-path page-name)))
(define (wiki/get-or-create-path page-name)
(let ((page-path (wiki/get-page-path page-name)))
(if (file-exists? page-path)
page-path
(cdr (cons (wiki/create-page page-name) page-path)))))
(define (wiki/list-pages-names wiki-directory)
(map
(lambda (x) (path->string (path-replace-extension x #"")))
(directory-list wiki-directory)))
(define (wiki/list-pages-paths wiki-directory)
(map
(lambda (x) (path->string (path->complete-path x wiki-root-directory)))
(directory-list wiki-directory)))
(define (wiki/edit-page page-name)
(let ((page-path (wiki/get-or-create-path page-name)))
(system (string-join (list (getenv "EDITOR") (path->string page-path))))))
(define/match (wiki/run cmd . page-name)
[("help" '()) (help)]
[("edit" page-name) (wiki/edit-page (car page-name))]
[("get" page-name)
(printf "~a\n" (wiki/get-page-content
(wiki/get-or-create-path (car page-name))))]
[("get-path" page-name)
(printf "~a\n" (path->string (wiki/get-or-create-path (car page-name))))]
[("list" '())
(void (map
(lambda (page) (printf "~a\n" page))
(wiki/list-pages-names wiki-root-directory)))]
[("list-paths" '())
(void (map
(lambda (page) (printf "~a\n" page))
(wiki/list-pages-paths wiki-root-directory)))]
[(list a)
(void (printf "unknown command\n\n") (help))])
(define (help)
(void (printf "wiki is simple wiki utilit\n")
(printf "\n")
(printf "Arguments:\n")
(printf "\tget PAGE_NAME - get wiki page\n")
(printf "\tget-path PAGE_NAME - get wiki page path\n")
(printf "\tedit PAGE_NAME - edit wiki page\n")
(printf "\tlist - list all pages\n")
(printf "\tlist-paths - list all pages paths\n")
(printf "\thelp - show this page\n")))
(let ((cmd (vector->list (current-command-line-arguments))))
(if (equal? (length cmd) 0)
(help)
(if (string-environment-variable-name? "WIKI_PATH")
(apply wiki/run cmd)
(printf "WIKI_PATH is not set\n"))))