# $Id: calendar.rb,v 1.12 2003/12/10 16:42:05 not Exp $ # 2003/12/27 calender2photo 修正 =begin calendar.rb nDiary Plug-in カレンダーの作成 各日付へのリンク付きのカレンダーを作成します。 option: lang=en 月名、日付を英語表記にします。(Sun, Mon, ...) lang=en2 月名、日付を英語表記にします。(Su, Mo, ...) width=14 横幅(列数)指定。7の倍数で無い場合上の曜日行が無くなります。 height=2 縦幅(行数)指定。 width=max 列数を月の日数分に。行数は1になります。 width=over 列数を月の週分に(前後に先来月日が入る)。行数は1になります。 week=off 曜日行が無くなります。 title=off 年月行が無くなります。 titlelink=off 年月行のリンクが無くなります。 today=off 生成日に div class=today をつけます。 fill=off 前後月の日付を表示しません。 monthpart=off 「上中下旬」の行を表示しません。 monthpart=title 「上中下旬」の行を月表記の隣に表示しす。 partstr=1 2 3 「上中下旬」の文字列の指定、空白で区切って並べます。 nextstr=pre nxt 「前次月」の文字列の指定、空白で区切って並べます。 # part=true ファイルに含まれる日付分のみ主題とします。 mode=enikki enikkiの絵を数字の変わりに入れます。 photo=on imagefiles/enikkiの絵を数字の変わりに入れます。 config: ndiary.conf等内 CALENDER_IMAGESIZE = 32 #画像最大サイズ(不明時のサイズ)指定 exsample: =end def plug(opt) require 'date' if RUBY_VERSION =~ /^1.8/ begin require 'date/holiday' holiday_flag = true rescue LoadError begin if RUBY_VERSION !~ /^1.8/ require '../user/lib/date2' require '../user/lib/holiday' holiday_flag = true end rescue LoadError holiday_flag = false end end width = opt['width'] =~ /^\d+$/ ? opt['width'].to_i : 14 #数字なら height = opt['height'] =~ /^\d+$/ ? opt['height'].to_i: -1 #数字なら width_option = opt['width'] =~ /^\D+$/ ? opt['width'] : '' #非数字なら flag_week = opt['week'] ? false : true flag_title = opt['title'] ? false : true flag_titlelink = opt['titlelink'] ? false : true flag_today = opt['today'] ? false : true flag_fill = opt['fill'] ? false : true flag_part = opt['part'] ? true : false flag_photo = opt['photo'] ? true : false flag_enikki = true if opt['mode'] == 'enikki' flag_monthpart = true flag_monthpart = false if opt['monthpart'] flag_monthpart = 'title' if opt['monthpart'] == 'title' flag_monthpart = false if @diary.config['MONTH_PART_NAMES'].size == 0 width_option = 'max' if width == 0 #幅0の処理 width_option = 'max' if height == 0 #高さ0の処理 flag_week = false if width % 7 != 0 flag_week = true if width_option == 'over' flag_today = false unless self.kind_of?(LastDiary) && flag_today today = Date.today image_size = 32 if @diary.respond_to?('config') then image_size = @diary.config['CALENDER_IMAGESIZE'].to_i unless @diary.config['CALENDER_IMAGESIZE'].nil? end if self.kind_of?(LastDiary) or self.kind_of?(Topics) then thismonth = @diaries[0][0..5] elsif self.kind_of?(PastDiary) then thismonth = @targetMonth else return end y = thismonth[0..3].to_i m = thismonth[4..5].to_i if m == 12 then nextmonth = sprintf("%04d01", y + 1) else nextmonth = sprintf("%04d%02d", y, m + 1) end if m == 1 then prevmonth = sprintf("%04d12", y - 1) else prevmonth = sprintf("%04d%02d", y, m - 1) end nextpart = '' prevpart = '' # if flag_part headday = @diaries.sort[0] tailday = @diaries.sort[-1] alldiaries = @allDiaries.keys.sort firstdiaryindex = alldiaries.index(@diaries[0]) lastdiaryindex = alldiaries.index(@diaries[-1]) firstdiaryindex, lastdiaryindex = lastdiaryindex, firstdiaryindex if self.kind_of?(LastDiary) thismonth = @diaries[0] prevpart = alldiaries[alldiaries.index(headday)-1] if firstdiaryindex > 1 nextpart = alldiaries[alldiaries.index(tailday)+1] if lastdiaryindex < alldiaries.size - 1 headday = Date.new(headday[0..3].to_i, headday[4..5].to_i, headday[6..7].to_i) tailday = Date.new(tailday[0..3].to_i, tailday[4..5].to_i, tailday[6..7].to_i) # end monthlink = date2monthlyfilelink(thismonth) nextmonthlink = date2monthlyfilelink(nextmonth) prevmonthlink = date2monthlyfilelink(prevmonth) nextpartlink = date2monthlyfilelink(nextpart) prevpartlink = date2monthlyfilelink(prevpart) nextmonthlink = %Q!>>! unless nextmonthlink.empty? prevmonthlink = %Q!<<! unless prevmonthlink.empty? nextpartlink = %Q!>! unless nextpartlink.empty? prevpartlink = %Q!<! unless prevpartlink.empty? if opt['lang'] == 'en' then wdayname = %w(Sun Mon Tue Wed Thu Fri Sat) mo = Time::local(y,m).strftime("%Y/%m") mopartname = %w(A B C) monextname = %w(Prev Next) elsif opt['lang'] == 'en2' then wdayname = %w(Su Mo Tu We Th Fr Sa) mo = Time::local(y,m).strftime("%Y-%m") mopartname = %w(a b c) monextname = %w(Pr Ne) else wdayname = %w(日 月 火 水 木 金 土) mo = Time::local(y,m).strftime2("%Y年%m月") mopartname = %w(上旬 中旬 下旬) monextname = %w(前月 次月) end css = %w(sunday monday tuesday wedneday thursday friday saturday) if opt['partstr'] mopartname = opt['partstr'].split end if opt['nextstr'] monextname = opt['nextstr'].split end # day = (1..28).to_a.concat((29..31).to_a.delete_if{ |d| # begin # !Date::valid_date?(y, m, d) # rescue NameError # !Date::exist?(y, m, d) # end # }).collect!{ |d| # d.to_s # } day = [] Date.new(y,m).upto((Date.new(y,m)>>1)-1){ |d| day << d } wday = day.first.wday if (flag_week) && (width_option != 'max') while day.first.wday != 0 day.unshift(day.first - 1) end wday = 0 end if height != -1 width = (day.size.to_f/height.to_f).ceil if flag_week width = (width / 7.0).ceil * 7 end end if width_option == 'max' width = day.size end if width_option == 'over' width = (day.size.to_f/7.0).ceil * 7 end height = (day.size.to_f/width.to_f).ceil while day.size < width * height day << day[-1] + 1 end # if (flag_week) && (width % 7 != 0) && (height > 1) # flag_week = false # end topic = {} topicmonth = day[0] while topicmonth <= day[-1] month = "%04d%02d" % [topicmonth.year, topicmonth.month] begin open(@logDirectory + month + '.topic').each{ |line| d, t = line.split(/\t/,2) t.gsub!(/\t/, " ") t.gsub!(/<\/?([!\w]+)[^>]*>/i, '') # topic[d] = %! title="#{t.chomp}"! topic[d] = t.chomp.to_s } rescue end topicmonth = topicmonth >> 1 end enikkifiles = {} if flag_enikki || flag_photo Dir::find(@outputDirectory + 'enikki/'){ |file| if /\/(\d{8})\.(jpg|png|gif)$/i =~ file then enikkifiles[$1] = '../' * @filename.scan(%r"[^/]+/").size + file.sub(@outputDirectory, '') end } end if flag_photo @imagefiles = {} imagemonth = day[0] #.imageの読込 while imagemonth <= day[-1] month = "%04d%02d" % [imagemonth.year, imagemonth.month] open(@logDirectory + month + '.image').each{ |line| date, anchor, file, w, h, etc = line.chomp.split("\t") if @imagefiles[date].nil? if /^\d{8}$/i =~ date && !file.nil? file = @outputDirectory + file if file[0,1] == '.' @imagefiles[date] = [file, w, h] end end } if File.exist?(@logDirectory + month + '.image') imagemonth = imagemonth >> 1 end enikkifiles.each{ |key, file| #enikkiの上書読込 @imagefiles[key] = [file] } end if flag_enikki @imagefiles = enikkifiles flag_photo = true end str = %Q!
\n! str = %Q!
\n! if flag_enikki if flag_monthpart != false monthpartnames = @monthPartNames.dup monthpartnames.shift names = monthpartnames.uniq sizes = {} names.each{ |name| sizes[name] = 0 } monthpartnames.each{ |name| sizes[name] += 1 } size = width / names.size mopartname = names if names.size != mopartname.size monthpart = [] names.each_index{ |i| j = 0 while monthpartnames[j] != names[i] j += 1 end link = date2monthlyfilelink(thismonth + ("%02d" % (j+1))) if File::readable?(@outputDirectory + link) then monthpart << "#{mopartname[i]}" else monthpart << mopartname[i] end } end if flag_title if flag_titlelink if flag_monthpart != 'title' size1, size2 = 2, width-4 size1, size2 = 1, 1 if width < 4 str << < HTML_END else nextmonthlink = date2monthlyfilelink(nextmonth) prevmonthlink = date2monthlyfilelink(prevmonth) unless nextmonthlink.empty? nextmonthlink = %Q![#{monextname[1]}]! else nextmonthlink = %Q![#{monextname[1]}]! end unless prevmonthlink.empty? prevmonthlink = %Q![#{monextname[0]}]! else prevmonthlink = %Q![#{monextname[0]}]! end mopart_str = '' mopart_str = '( ' + monthpart.join(' / ') + ' )' if flag_monthpart == 'title' str << < HTML_END end else str << < HTML_END end end if flag_monthpart == true size = width / monthpart.size str << "\t\n" names.each_index{ |i| j = 0 while monthpartnames[j] != names[i] j += 1 end if height == 1 size = sizes[names[i]] end str << " \n" } str << "\t\n" end if flag_week str << "\t\n" for i in 0..(width-1) str << %Q!\t\t\n! end str << "\t\n" end while day.size != 0 str << "\t\n" for i in 0..(width-1) d = day.shift dd = "%04d%02d%02d" % [d.year, d.month, d.day] c = css[d.wday] begin if holiday_flag and d != ' ' and d.national_holiday? then c = 'holiday' end rescue NameError #for ruby1.6.8 and date2 if holiday_flag and d != ' ' and d.nhol? c = 'holiday' end end if (flag_fill == false) && (d.month != m) dstr = ' ' str << %Q!\t\t\n! else if (d.month != m) || ((flag_part) && ((d < headday) || (tailday < d))) dstr = '' + d.day.to_s + '' elsif flag_today && (today == d) dstr = '' + d.day.to_s + '' else dstr = d.day.to_s end if flag_photo if @imagefiles.key?(dd) url, w, h = @imagefiles[dd] if w.nil? require 'lib/imagesize' w, h = ImageSize::size(url) end w, h = w.to_i, h.to_i w, h = image_size, image_size if w.zero? l = 1 max = w > h ? w : h while (max / l) > image_size l += 1 end w, h = w / l, h / l topic[dd] = d.day.to_s + ': ' + topic[dd] dstr = "\"#{topic[dd]}\""' : '>') end end #calender2photo(enikki)対応 if @allDiaries.key?(dd) then link = date2monthlyfilelink(dd, true) str << %Q!\t\t\n! else str << %Q!\t\t\n! end end end str << "\t\n" end str << "
#{prevmonthlink} #{prevpartlink} #{mo} #{nextpartlink} #{nextmonthlink}
#{mo} #{mopart_str} #{prevmonthlink} #{nextmonthlink}
#{mo}
#{monthpart[i]}
#{wdayname[(i + wday) % 7]}
#{dstr}#{dstr}#{dstr}
\n" return str end