forked from ryenski/radiant-comments
-
Notifications
You must be signed in to change notification settings - Fork 0
/
comment_tags.rb
338 lines (296 loc) · 9.7 KB
/
comment_tags.rb
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
module CommentTags
include Radiant::Taggable
desc "Provides tags and behaviors to support comments in Radiant."
desc %{
Renders the contained elements if comments are enabled on the page.
}
tag "if_enable_comments" do |tag|
tag.expand if (tag.locals.page.enable_comments?)
end
# makes more sense to me
tag "if_comments_enabled" do |tag|
tag.expand if (tag.locals.page.enable_comments?)
end
desc %{
Renders the contained elements unless comments are enabled on the page.
}
tag "unless_enable_comments" do |tag|
tag.expand unless (tag.locals.page.enable_comments?)
end
# makes more sense to me
tag "unless_comments_enabled" do |tag|
tag.expand unless (tag.locals.page.enable_comments?)
end
desc %{
Renders the contained elements if the page has comments.
}
tag "if_comments" do |tag|
tag.expand if tag.locals.page.has_visible_comments?
end
desc %{
Renders the contained elements unless the page has comments.
}
tag "unless_comments" do |tag|
tag.expand unless tag.locals.page.has_visible_comments?
end
desc %{
Renders the contained elements if the page has comments _or_ comment is enabled on it.
}
tag "if_comments_or_enable_comments" do |tag|
tag.expand if(tag.locals.page.has_visible_comments? || tag.locals.page.enable_comments?)
end
desc %{
Gives access to comment-related tags
}
tag "comments" do |tag|
comments = tag.locals.page.comments.approved
tag.expand
end
desc %{
Cycles through each comment and renders the enclosed tags for each.
}
tag "comments:each" do |tag|
page = tag.locals.page
comments = page.comments.approved.to_a
comments << page.selected_comment if page.selected_comment && page.selected_comment.unapproved?
result = []
comments.each_with_index do |comment, index|
tag.locals.comment = comment
tag.locals.index = index
result << tag.expand
end
result
end
desc %{
Gives access to the particular fields for each comment.
}
tag "comments:field" do |tag|
tag.expand
end
desc %{
Renders the index number for this comment.
}
tag 'comments:field:index' do |tag|
tag.locals.index + 1
end
%w(id author author_email author_url content content_html filter_id).each do |field|
desc %{ Print the value of the #{field} field for this comment. }
tag "comments:field:#{field}" do |tag|
options = tag.attr.dup
#options.inspect
value = tag.locals.comment.send(field)
value
end
end
desc %{
Renders the date a comment was created.
*Usage:*
<pre><code><r:date [format="%A, %B %d, %Y"] /></code></pre>
}
tag 'comments:field:date' do |tag|
comment = tag.locals.comment
format = (tag.attr['format'] || '%A, %B %d, %Y')
date = comment.created_at
date.strftime(format)
end
desc %{
Renders a link if there's an author_url, otherwise just the author's name.
}
tag "comments:field:author_link" do |tag|
if tag.locals.comment.author_url.blank?
tag.locals.comment.author
else
%(<a href="#{tag.locals.comment.author_url}">#{tag.locals.comment.author}</a>)
end
end
desc %{
Renders the contained elements if the comment has an author_url specified.
}
tag "comments:field:if_author_url" do |tag|
tag.expand unless tag.locals.comment.author_url.blank?
end
desc %{
Renders the contained elements if the comment is selected - that is, if it is a comment
the user has just posted
}
tag "comments:field:if_selected" do |tag|
tag.expand if tag.locals.comment == tag.locals.page.selected_comment
end
desc %{
Renders the contained elements if the comment has been approved
}
tag "comments:field:if_approved" do |tag|
tag.expand if tag.locals.comment.approved?
end
desc %{
Renders the contained elements if the comment has not been approved
}
tag "comments:field:unless_approved" do |tag|
tag.expand unless tag.locals.comment.approved?
end
desc %{
Renders a Gravatar URL for the author of the comment.
}
tag "comments:field:gravatar_url" do |tag|
email = tag.locals.comment.author_email
size = tag.attr['size']
format = tag.attr['format']
rating = tag.attr['rating']
default = tag.attr['default']
md5 = Digest::MD5.hexdigest(email)
returning "http://www.gravatar.com/avatar/#{md5}" do |url|
url << ".#{format.downcase}" if format
if size || rating || default
attrs = []
attrs << "s=#{size}" if size
attrs << "d=#{default}" if default
attrs << "r=#{rating.downcase}" if rating
url << "?#{attrs.join('&')}"
end
end
end
desc %{
Renders a comment form.
*Usage:*
<r:comment:form [class="comments" id="comment_form"]>...</r:comment:form>
}
tag "comments:form" do |tag|
attrs = tag.attr.symbolize_keys
html_class, html_id = attrs[:class], attrs[:id]
r = %Q{ <form action="#{tag.locals.page.url}#{'comments' unless Radiant::Config['comments.post_to_page?']}}
r << %Q{##{html_id}} unless html_id.blank?
r << %{" method="post" } #comlpete the quotes for the action
r << %{ id="#{html_id}" } unless html_id.blank?
r << %{ class="#{html_class}" } unless html_class.blank?
r << '>' #close the form element
r << tag.expand
r << %{</form>}
r
end
tag 'comments:error' do |tag|
if comment = tag.locals.page.last_comment
if on = tag.attr['on']
if error = comment.errors.on(on)
tag.locals.error_message = error
tag.expand
end
else
tag.expand if !comment.valid?
end
end
end
tag 'comments:error:message' do |tag|
tag.locals.error_message
end
%w(text password hidden).each do |type|
desc %{Builds a #{type} form field for comments.}
tag "comments:#{type}_field_tag" do |tag|
attrs = tag.attr.symbolize_keys
r = %{<input type="#{type}"}
r << %{ id="comment_#{attrs[:name]}"}
r << %{ name="comment[#{attrs[:name]}]"}
r << %{ class="#{attrs[:class]}"} if attrs[:class]
if value = (tag.locals.page.last_comment ? tag.locals.page.last_comment.send(attrs[:name]) : attrs[:value])
r << %{ value="#{value}" }
end
r << %{ />}
end
end
%w(submit reset).each do |type|
desc %{Builds a #{type} form button for comments.}
tag "comments:#{type}_tag" do |tag|
attrs = tag.attr.symbolize_keys
r = %{<input type="#{type}"}
r << %{ id="#{attrs[:name]}"}
r << %{ name="#{attrs[:name]}"}
r << %{ class="#{attrs[:class]}"} if attrs[:class]
r << %{ value="#{attrs[:value]}" } if attrs[:value]
r << %{ />}
end
end
desc %{Builds a text_area form field for comments.}
tag "comments:text_area_tag" do |tag|
attrs = tag.attr.symbolize_keys
r = %{<textarea}
r << %{ id="comment_#{attrs[:name]}"}
r << %{ name="comment[#{attrs[:name]}]"}
r << %{ class="#{attrs[:class]}"} if attrs[:class]
r << %{ rows="#{attrs[:rows]}"} if attrs[:rows]
r << %{ cols="#{attrs[:cols]}"} if attrs[:cols]
r << %{>}
if content = (tag.locals.page.last_comment ? tag.locals.page.last_comment.send(attrs[:name]) : attrs[:content])
r << content
end
r << %{</textarea>}
end
desc %{Build a drop_box form field for the filters avaiable.}
tag "comments:filter_box_tag" do |tag|
attrs = tag.attr.symbolize_keys
value = attrs.delete(:value)
name = attrs.delete(:name)
r = %{<select name="comment[#{name}]"}
unless attrs.empty?
r << " "
r << attrs.map {|k,v| %Q(#{k}="#{v}") }.join(" ")
end
r << %{>}
TextFilter.descendants.each do |filter|
r << %{<option value="#{filter.filter_name}"}
r << %{ selected="selected"} if value == filter.filter_name
r << %{>#{filter.filter_name}</option>}
end
r << %{</select>}
end
desc %{Prints the number of comments. }
tag "comments:count" do |tag|
tag.locals.page.comments.approved.count
end
tag "recent_comments" do |tag|
tag.expand
end
desc %{Returns the last [limit] comments throughout the site.
*Usage:*
<pre><code><r:recent_comments:each [limit="10"]>...</r:recent_comments:each></code></pre>
}
tag "recent_comments:each" do |tag|
limit = tag.attr['limit'] || 10
comments = Comment.approved.recent.all(:limit => limit)
result = []
comments.each_with_index do |comment, index|
tag.locals.comment = comment
tag.locals.index = index
tag.locals.page = comment.page
result << tag.expand
end
result
end
desc %{
Use this to prevent spam bots from filling your site with spam.
*Usage:*
<pre><code>What day comes after Monday? <r:comments:spam_answer_tag answer="Tuesday" /></code></pre>
}
tag "comments:spam_answer_tag" do |tag|
attrs = tag.attr.symbolize_keys
valid_spam_answer = attrs[:answer] || 'hemidemisemiquaver'
md5_answer = Digest::MD5.hexdigest(valid_spam_answer.to_slug)
r = %{<input type="text" id="comment_spam_answer" name="comment[spam_answer]"}
r << %{ class="#{attrs[:class]}"} if attrs[:class]
if value = (tag.locals.page.last_comment ? tag.locals.page.last_comment.send(:spam_answer) : '')
r << %{ value="#{value}" }
end
r << %{ />}
r << %{<input type="hidden" name="comment[valid_spam_answer]" value="#{md5_answer}" />}
end
desc %{
Render the contained elements if using the simple spam filter.
}
tag "if_comments_simple_spam_filter_enabled" do |tag|
tag.expand if Comment.simple_spam_filter_enabled?
end
desc %{
Render the contained elements unless using the simple spam filter.
}
tag "unless_comments_simple_spam_filter_enabled" do |tag|
tag.expand unless Comment.simple_spam_filter_enabled?
end
end