File: recent_comment3.rb

package info (click to toggle)
tdiary 2.0.1-1sarge1
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 7,220 kB
  • ctags: 1,667
  • sloc: ruby: 20,044; lisp: 476; makefile: 91; sql: 32; sh: 31
file content (81 lines) | stat: -rw-r--r-- 2,424 bytes parent folder | download | duplicates (3)
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
# $Revision: 1.13 $
# recent_comment3: ǶΥĥåߤꥹȥåפ
#   ѥ᥿:
#     max:           ɽ(̤:3)
#     sep:           ѥ졼(̤:)
#     date_format:   դΥեޥå(̤:ɽ+ֻ:ʬ)
#     except:        ̵뤹̾(Ĥ⤢,Ƕڤä¤٤)
#
#   @secure = true ʴĶǤưޤ
#
# Copyright (c) 2002 Junichiro KITA <kita@kitaj.no-ip.com>
# Distributed under the GPL
#
require 'pstore'

RECENT_COMMENT3_CACHE = "#{@cache_path}/recent_comments"
RECENT_COMMENT3_NUM = 50

def recent_comment3(max = 3, sep = '&nbsp;',
		date_format = "(#{@date_format + ' %H:%M'})", *except )
	date_format = "(#{@date_format + ' %H:%M'})" unless date_format.respond_to?(:to_str)
	result = []
	idx = 0
	PStore.new(RECENT_COMMENT3_CACHE).transaction do |db|
		break unless db.root?('comments')
		db['comments'].each do |c|
			break if idx >= max or c.nil?
			comment, date, serial = c
			next unless comment.visible?
			next if except.include?(comment.name)
			str = %Q|<strong>#{idx += 1}.</strong><a href="#{@index}#{anchor date.strftime('%Y%m%d')}#c#{'%02d' % serial}" title="#{CGI::escapeHTML(comment.shorten( @conf.comment_length ))}">#{CGI::escapeHTML(comment.name)}#{comment.date.strftime(date_format)}</a>\n|
			result << str
		end
	end
	if result.size == 0
		''
	else
		result.join( sep )
	end
end

add_update_proc do
	if @mode == 'comment' and @comment
		comment = @comment
		serial = 0
		@diaries[@date.strftime('%Y%m%d')].each_comment(100) do
			serial += 1
		end
		PStore.new(RECENT_COMMENT3_CACHE).transaction do |db|
			db['comments'] = Array.new(RECENT_COMMENT3_NUM) unless db.root?('comments')
			if db['comments'][0].nil? or comment != db['comments'][0][0]
				db['comments'].unshift([comment, @date, serial]).pop
			end
		end
	end
end

# I want to use update_proc, but TDiaryShowComment doesn't call update_proc.
add_form_proc do |date|
	if @mode == 'showcomment'
		date = date.strftime('%Y%m%d')

		PStore.new(RECENT_COMMENT3_CACHE).transaction do |db|
			break unless db.root?('comments')

			@diaries[date].each_comment(100) do |dcomment|
				db['comments'].each do |c|
					break if c.nil?
					comment, cdate, serial = c
					next if cdate.strftime('%Y%m%d') != date
					if comment == dcomment and comment.date == dcomment.date
						comment.show = dcomment.visible?
						next
					end
				end
			end
		end
	end
end

# vim: ts=3