/
post.lisp
142 lines (119 loc) · 4.22 KB
/
post.lisp
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
(in-package :herrblog)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defclass post (object-with-creation-timestamp
object-with-pages-cache)
((m-content
:initarg :content
:accessor access-content
:type list) ;;for generating the html
(m-title
:initarg :title
:type string
:accessor access-title) ;;any string
(m-id
:type string
:initarg :id) ;;url string, created from the title
(m-comments
:initform nil)
(m-tags-list ;;AKA categories
:accessor access-tags-list
:type list
:initarg :tags-list)
(m-preceding
:accessor access-preceding)
(m-following
:accessor access-following)))
(defmethod initialize-instance ((object post) &key title id tags-list)
(setf (slot-value object 'm-title) title
(slot-value object 'm-id) id
(slot-value object 'm-tags-list) tags-list)
(call-next-method))
(defclass post-comment (object-with-timestamp
object-with-creation-timestamp)
((m-content
:initarg :content
:accessor access-content
:type string)
(m-author
:initarg :author
:accessor access-author
:type string)))
(defmethod to-html ((p post)
(rules hash-table))
(with-accessors ((m-tags-list access-tags-list)
(m-title access-title)
(m-content access-content)) p
(apply #'markup*
(cons (list :h2 m-title)
(cons (list :font :color "SlateGray" (format nil "~a" m-tags-list))
(expand-tree m-content
rules))))))
(defmethod add-post ((blog main-container)
(new-entry post))
(with-slots ((id m-id)
(tags-list m-tags-list)) new-entry
(with-slots ((posts m-posts)
(post-ids m-post-ids)
(post-count m-post-count)
(timestamp m-timestamp)
(categories m-categories)) blog
(mapc (lambda (x) (multiple-value-bind (cat found) (gethash x categories)
(add-post (if found cat (new-category x blog))
new-entry)))
tags-list)))
(call-next-method))
(defmethod remove-post ((container main-container)
(id string))
(with-slots ((categories m-categories)
(posts m-posts)) container
(multiple-value-bind (post found) (gethash id posts)
(if found
(progn
(mapcan (lambda (x) (remove-post x id))
(mapcar (lambda (y) (gethash y categories))
(slot-value post 'm-tags-list)))
(call-next-method))
(error "No such post")))))
(defmethod remove-post ((container posts-container)
(id string))
(with-slots ((posts m-posts)
(post-ids m-post-ids)
(post-count m-post-count)
(timestamp m-timestamp)) container
(if (nth-value 1 (gethash id posts))
(progn
(decf post-count)
(delete id post-ids :test :equal)
(remhash id posts)
(update-timestamp container))
(error "No such post"))))
(defmethod add-post ((blog posts-container)
(new-entry post))
(with-slots ((id m-id))
new-entry
(with-slots ((posts m-posts)
(post-ids m-post-ids)
(post-count m-post-count)
(timestamp m-timestamp))
blog
(if (null (gethash id posts))
(progn
(incf post-count)
(setf (gethash id posts)
new-entry)
(push id
post-ids)
(update-timestamp blog))
(error "Entry already present")))))
(defun make-post (title html-content time &optional (tags nil))
(declare (type string title)
(type list html-content)
(type list tags))
(let ((id (string-to-id title)))
(make-instance 'post
:title title
:tags-list (mapcar #'string-to-id tags)
:cached-page-index id
:id id
:creation-timestamp time
:content html-content)))