-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathblog.arc
107 lines (85 loc) · 2.39 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
98
99
100
101
102
103
104
105
; Blog tool example. 20 Jan 08.
; To run:
; arc> (load "blog.arc")
; arc> (bsv)
; go to http://localhost:8080/blog
(= postdir* "arc/posts/" maxid* 0 posts* (table))
(= blogtitle* "A Blog")
(deftem post
id nil
title nil
text nil)
(def load-posts ()
(each id (map [coerce _ 'int] (dir postdir*))
(= maxid* (max maxid* id)
(posts* id) (temload 'post (string postdir* id)))))
(def save-post (p)
(save-table p (string postdir* (p 'id))))
(def post (id) (posts* (errsafe (coerce id 'int))))
(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
(aif (post (arg req "id"))
(post-page (get-user req) it)
(notfound)))
(def permalink (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) (permalink p)))
(when user
(sp)
(link "[edit]" (string "editpost?id=" (p 'id))))
(br2)
(pr (p 'text)))
(def notfound ()
(blogpage (pr "No such post.")))
(defopl newpost req
(whitepage
(aform (fn (req)
(let user (get-user req)
(post-page user
(addpost user (arg req "t") (arg req "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 (++ maxid*) 'title title 'text text)
(save-post p)
(= (posts* (p 'id)) p)))
(defopl editpost req
(aif (post (arg req "id"))
(edit-page (get-user req) it)
(notfound)))
(def edit-page (user p)
(whitepage
(vars-form user
`((string title ,(p 'title) t t)
(text 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 maxid*)))
(tag li (link (p 'title) (permalink p)))))))
(defop blog req
(let user (get-user req)
(blogpage
(for i 0 4
(awhen (posts* (- maxid* i))
(display-post user it)
(br 3))))))
(def bsv ()
(ensure-dir postdir*)
(load-posts)
(asv))