File: calendar2.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 (155 lines) | stat: -rw-r--r-- 4,286 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
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
# calendar2.rb $Revision: 1.12.2.1 $
#
# calendar2: ɤǸ褦ʥɲä
#   ѥ᥿:
#     days_format: 򸽤String鹽Array
#                  nilꤹȥǥեͤ򤵤롥
#                  ("пڶ".split(//))
#     nav_format:  ɽString鹽Array
#                  nilꤹȥǥեͤ򤵤롥
#                  (["", "%dǯ<br>%d", ""])
#     show_todo:   ǻꤷʸ󤬱줿˥֥ȥȤƸ
#                  ͽȤpopup롥
#                  (nil)
#
# Copyright (c) 2001,2002 Junichiro KITA <kita@kitaj.no-ip.com>
# Distributed under the GPL
#
=begin ChangeLog
2003-09-25 TADA Tadashi
	* use @conf.shorten.

2003-01-10 NT <nt@be.to>
	* @options['calendar2.erb'] -> @options['apply_plugin']
	* use Plugin#shorten.

2002-12-23 Hiroyuki Ikezoe <zoe@kasumi.sakura.ne.jp>
	* use Plugin#apply_plugin.
	* visible subsubtitle.
	
2002-12-06 TADA Tadashi <sho@spc.gr.jp>
	* without escapeHTML for title attribules.
=end

def calendar2_make_cal(year, month)
	result = []
	t = Time.local(year, month, 1)
	r = Array.new(t.wday, nil)
	r << 1
	2.upto(31) do |i|
		break if Time.local(year, month, i).month != month
		r << i
	end
	r += Array.new((- r.size) % 7, nil)
	0.step(r.size - 1, 7) do |i|
		result << r[i, 7]
	end
	result
end

def calendar2_prev_current_next
	yyyymm = if @mode == "latest"
					Time.now
				else
					@date
				end.strftime "%Y%m"
	yms = [yyyymm]
	@years.keys.each do |y|
		yms |= @years[y].collect {|i| y + i}
	end
	yms.sort!
	yms.unshift nil
	yms.push nil
	i = yms.index(yyyymm)
	yms[i - 1, 3]
end

def calendar2_make_anchor(ym, str)
	if ym
		%Q|<a href="#{@index}#{anchor ym}">#{str}</a>|
	else
		str
	end
end

def calendar2(days_format = nil, nav_format = nil, show_todo = nil)
 	days_format ||= "пڶ".split(//)
	nav_format ||= ["", "%dǯ<br>%d", ""]

	return '' if /TAMATEBAKO/ =~ @cgi.user_agent
	date = if @mode == "latest"
				Time.now
			else
				@date
			end
	year = date.year
	month = date.month  
	p_c_n = calendar2_prev_current_next

	result = <<CALENDAR_HEAD
<table class="calendar" summary="calendar">
<tr>
 <td class="image" colspan="7"></td>
</tr>
<tr>
 <td class="calendar-prev-month" colspan="2">#{calendar2_make_anchor(p_c_n[0], nav_format[0] % [year, month])}</td>
 <td class="calendar-current-month" colspan="3">#{calendar2_make_anchor(p_c_n[1], nav_format[1] % [year, month])}</td>
 <td class="calendar-next-month" colspan="2">#{calendar2_make_anchor(p_c_n[2], nav_format[2] % [year, month])}</td>
</tr>
CALENDAR_HEAD
	result << "<tr>"
	result << %Q| <td class="calendar-sunday">#{days_format[0]}</td>\n|
	1.upto(5) do |i|
		result << %Q| <td class="calendar-weekday">#{days_format[i]}</td>\n|
	end
	result << %Q| <td class="calendar-saturday">#{days_format[6]}</td>\n|
	result << "</tr>\n"
	calendar2_make_cal(year, month).each do |week|
		result << "<tr>\n"
		week.each do |day|
			if day == nil
				result << %Q| <td class="calendar-day"></td>\n|
			else
				date = "%04d%02d%02d" % [year, month, day]
				result << %Q| <td class="calendar-day">%s</td>\n| %
					if @diaries[date] == nil
						day.to_s
					elsif ! @diaries[date].visible?
						if show_todo
							todos = []
							@diaries[date].each_section do |section|
								if show_todo === section.subtitle
									todos << section.body
								end
							end
							if todos.size != 0
								%Q|<a title="#{todos.join( "\n" ).gsub( /"/, '&quot;' )}"><span class="calendar-todo">#{day}</span></a>|
							else
								day.to_s
							end
						else
							day.to_s
						end
					else
						subtitles = []
						idx = "01"
						@diaries[date].each_section do |section|
							if section.subtitle
								text = section.subtitle_to_html
							else
								text = section.body_to_html
							end
							subtitles <<  %Q|#{idx}. #{@conf.shorten(apply_plugin( text, true )).gsub(/"/, '&quot;')}|
							idx.succ!
						end
						%Q|<a href="#{@index}#{anchor date}" title="#{subtitles.join("&#13;&#10;")}">#{day}</a>|
					end
			end
		end
		result << "</tr>\n"
	end
	result << "</table>\n"
end

#@calendar2_cache = CacheMonth.new(@date, :calender2, method(:calendar2))
#add_update_proc @calendar2_cache.writer