File: recent_comment3.rb

package info (click to toggle)
tdiary 5.4.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,088 kB
  • sloc: ruby: 23,031; javascript: 1,029; xml: 325; makefile: 26; sh: 4
file content (184 lines) | stat: -rw-r--r-- 5,553 bytes parent folder | download | duplicates (5)
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
#
# recent_comment3: 最近のツッコミをリストアップする
#
# Copyright (c) 2002 Junichiro KITA <kita@kitaj.no-ip.com>
# Distributed under the GPL2 or any later version.
#
require 'pstore'
require 'fileutils'
require 'time'
require 'pathname'
require 'tdiary/diary_container'

def recent_comment3_format(format, *args)
	format.gsub(/\$(\d)/) {|s| args[$1.to_i - 1]}
end

def migrate_old_data
	# backward compatibility
	if File.exist?("#{@cache_path}/recent_comments") && !File.exist?("{#{@conf.data_path}/recent_comments")
		FileUtils.mv( "#{@cache_path}/recent_comments", "#{@conf.data_path}/recent_comments" )
	end
	# workaround for "/foo//bar" doesn't equal "/foo/bar"
	if @conf['recent_comment3.cache'] &&
			Pathname(@conf['recent_comment3.cache']).cleanpath == Pathname("#{@cache_path}/recent_comments").cleanpath
		@conf['recent_comment3.cache'] = "#{@conf.data_path}/recent_comments"
	end
end

def recent_comment3_init
	@conf['recent_comment3.cache'] ||= "#{@conf.data_path}/recent_comments"
	@conf['recent_comment3.cache_size'] ||= 50
	@conf['recent_comment3.max'] ||= 3
	@conf['recent_comment3.date_format'] ||= "(%m-%d)"
	@conf['recent_comment3.except_list'] ||= ''
	@conf['recent_comment3.format'] ||= '<a href="$2" title="$3">$4 $5</a>'
	@conf['recent_comment3.tree'] ||= ""
	@conf['recent_comment3.titlelen'] ||= 20
	@conf['recent_comment.notfound_msg'] ||= ''
end

def recent_comment3(ob_max = 'OBSOLUTE' ,sep = 'OBSOLUTE',ob_date_format = 'OBSOLUTE',*ob_except )
	migrate_old_data
	recent_comment3_init

	cache = @conf['recent_comment3.cache']
	date_format = @conf['recent_comment3.date_format']
	excepts = @conf['recent_comment3.except_list'].split(/,/)
	format = @conf['recent_comment3.format']
	titlelen = @conf['recent_comment3.titlelen']
	notfound_msg = @conf['recent_comment.notfound_msg']

	entries = {}
	tree_order =[]
	order = []
	idx = 0

	PStore.new(cache).transaction do |db|
		break unless db.root?('comments')
		db['comments'].each do |c|
			break if c.nil? || idx >= @conf['recent_comment3.max']

			comment, date, serial = c
			next if excepts.include?(comment.name) || !comment.visible?

			a = h( @index ) + anchor("#{date.strftime('%Y%m%d')}#c#{'%02d' % serial}")
			# XXX handling Encoding::CompatibilityError
			popup = h(comment.shorten(@conf.comment_length)) rescue nil
			str = h(comment.name)
			date_str = h( comment.date.strftime(date_format) )

			idx += 1

			entry_date = "#{date.strftime('%Y%m%d')}"
			comment_str = entries[entry_date]

			if comment_str.nil?
				comment_str = []
				tree_order << entry_date
			end

			comment_str << recent_comment3_format(format, idx, a, popup, str, date_str)
			entries[entry_date] = comment_str
			order << entry_date
		end
		db.abort
	end

	result = []

	if @conf['recent_comment3.tree'] == "t"
		if entries.size == 0
			notfound_msg
		else
			tree_order.each do |entry_date|
				a_entry = @index + anchor(entry_date)
				diary = DiaryContainer::find_by_day(@conf, entry_date)
				title = diary.diaries[entry_date].title.gsub( /<[^>]*>/, '' ) if diary

				if title.nil? || title.length == 0 || title.strip.delete(' ').delete(' ').length == 0 then
					date = Time.parse(entry_date)
					title = "#{date.strftime @date_format}"
				end

				result << "<li>"
				result << %Q|<a href="#{h( a_entry )}">#{h( @conf.shorten( title, titlelen ) )}</a><br>|
				entries[entry_date].sort.each do |comment_str|
					result << comment_str + "<br>"
				end
				result << "</li>\n"
			end

			%Q|<ul class="recent-comment">\n| + result.join( '' ) + "</ul>\n"
		end
	else
		if entries.size == 0
			''
		else
			order.each do | entry_date |
				result << "<li>#{entries[entry_date][0]}</li>\n"
				entries[entry_date].shift
			end
			%Q|<ol class="recent-comment">\n| + result.join( '' ) + "</ol>\n"
		end
	end
end

add_update_proc do
	migrate_old_data
	recent_comment3_init

	date = @date.strftime( '%Y%m%d' )
	cache = @conf['recent_comment3.cache']
	size = @conf['recent_comment3.cache_size']

	if @mode == 'comment' && @comment && @comment.visible?
		PStore.new(cache).transaction do |db|
			comment = @comment
			serial = 0
			@diaries[date].each_comment { serial += 1 }
			db['comments'] = Array.new( size ) unless db.root?('comments')
			if db['comments'][0].nil? or comment != db['comments'][0][0]
				db['comments'].unshift([comment, @date, serial]).pop
			end
		end
	elsif @mode == 'showcomment'
		PStore.new( cache ).transaction do |db|
			break unless db.root?('comments')

			@diaries[date].each_comment do |dcomment|
				db['comments'].each do |c|
					break if c.nil?

					comment, cdate, _ = c
					next if cdate.strftime('%Y%m%d') != date

					if comment == dcomment && comment.date.to_s == dcomment.date.to_s
						comment.show = dcomment.visible?
						next
					end
				end
			end
		end
	end
end

if @mode == 'saveconf'
	def saveconf_recent_comment3
		@conf['recent_comment3.max'] = @cgi.params['recent_comment3.max'][0].to_i
		@conf['recent_comment3.date_format'] = @cgi.params['recent_comment3.date_format'][0]
		@conf['recent_comment3.except_list'] = @cgi.params['recent_comment3.except_list'][0]
		@conf['recent_comment3.format'] = @cgi.params['recent_comment3.format'][0]
		@conf['recent_comment3.tree'] = @cgi.params['recent_comment3.tree'][0]
		@conf['recent_comment3.titlelen'] = @cgi.params['recent_comment3.titlelen'][0].to_i
		@conf['recent_comment.notfound_msg'] = @cgi.params['recent_comment.notfound_msg'][0]
	end
end

# Local Variables:
# mode: ruby
# indent-tabs-mode: t
# tab-width: 3
# ruby-indent-level: 3
# End:
# vim: ts=3