forked from tensorfork/tlarc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
blog.arc
99 lines (76 loc) · 2.34 KB
/
blog.arc
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
; Blog tool example. 20 Jan 08, rev 21 May 09.
; To run:
; arc> (load "blog.arc")
; arc> (bsv)
; go to http://localhost:8080/blog
(= postdir* "arc/posts/"
blogtitle* "A Blog")
(or= postid* 0 posts* (table))
(deftem post id nil title nil text nil)
(def load-posts ()
(each id (map int (dir postdir*))
(= postid* (max postid* id)
(posts* id) (temload 'post (string postdir* id)))))
(def save-post (p) (save-table p (string postdir* p!id)))
(def post (id) (posts* (errsafe:int id)))
(mac blogpage body
`(whitepage
(center
(widtable 600
(tag b (link blogtitle* "blog"))
(br 3)
,@body
(br 3)
(w/bars (link "archive")
(link "new post" "newpost"))))))
(defop viewpost req (blogop post-page req))
(def blogop (f req)
(aif (post (arg req "id"))
(f (get-user req) it)
(blogpage (pr "No such post."))))
(def post-url (p) (string "/viewpost?id=" p!id))
(def post-page (user p) (blogpage (display-post user p)))
(def display-post (user p)
(tag b (link p!title (post-url p)))
(when user
(sp)
(link "[edit]" (string "/editpost?id=" p!id)))
(br2)
(pr p!text))
(defopl newpost req
(whitepage
(aform [let u (get-user _)
(post-page u (addpost u (arg _ "t") (md-from-form:arg _ "b")))]
(tab (row "title" (input "t" "" 60))
(row "text" (textarea "b" 10 80))
(row "" (submit))))))
(def addpost (user title text)
(let p (inst 'post 'id (++ postid*) 'title title 'text text)
(save-post p)
(= (posts* p!id) p)))
(defopl editpost req (blogop edit-post-page req))
(def edit-post-page (user p)
(whitepage
(vars-form user
`((string title ,p!title t t)
(mdtext text ,p!text t t))
(fn (name val) (= (p name) val))
(fn () (save-post p)
(post-page user p)))))
(defop archive req
(blogpage
(tag ul
(each p (map post (rev (range 1 postid*)))
(tag li (link p!title (post-url p)))))))
(def blogmain ((o user (get-user)))
(blogpage
(for i 0 4
(awhen (posts* (- postid* i))
(display-post user it)
(br 3)))))
(defop blog req (blogmain (get-user req)))
(defop || req (blogmain (get-user req)))
(def bsv ((o port 8080))
(ensure-dir postdir*)
(load-posts)
(asv port))