# notさんの「schedule.rb,v 1.1.1.1 2000/11/23 10:34:06」をいじくる # edit 2001/3/15-2003/9/14 # 謝)すぐりさん http://sgr.vis.ne.jp/index.shtml =begin schedule.rb -> schedule_z.rb nDiary Plug-in スケジュールの表示 ここんとこ暫くのスケジュールを表示します。 option: title=str スケジュールに付けるタイトル max=num 表示する最大件数 past=num[0] num日前の情報から表示 come=num[30] num日後までの情報を表示 nored=true 今日前の過去スケジュールに色つけるのやめる suguri=true 期間終端基準で過去スケジュールに色つけ month=true 今月分を表示します 余) 日付の記述に「?」等(つまり期間)を持たせました。その為 (1)format-optionの放棄;「month/day(etc)」に表示固定 (2)記述法の限定;「2002/9/上旬」「2002/09/1-10」「2002/9/?」等 (2')「20020903-2002090?」等もいけるはずだけど未テスト となりました。 今日を越してるスケジュールは色変えました(期間始端基準が標準)。 日付文字列頭に「x,*,_,u,d,c」でstrike,strong,under,font+-,色,入ります。 一部html出力のulとかclass回りいじってます。 exsample: =end require 'date' unless Date.new def plug(opt) #設定,読込,展開,抽出,並替,出力の6構成 #設定;optionとinitialize # opt['format'] = opt['format'] ? opt['format'] : '%0m/%0d' past = opt['past'] ? opt['past'].to_i : 0 come = opt['come'] ? opt['come'].to_i : 30 max = opt['max'] ? opt['max'].to_i : 8 flag_red = opt['nored'] ? false : true flag_timebase = opt['suguri'] ? true : false flag_month = opt['month'] ? true : false #日付制御は、元は8桁数値文字列だったが、Timeに変更 today = Time.now start = today - past * 86400 last = today + come * 86400 if flag_month y, m = today.year, today.month start = Time.local(y, m) if m < 12 m = m + 1 else y, m = y+1, 1 end last = Time.local(y,m)-1 end #最終データは、元はHashだったが、Arrayに変更 schedule = [] data = [] #読込;1行1件、tab区切り、日付と文字列 begin open(opt['file'], 'r').readlines.each{ |line| tmp = line.chomp.chomp("\r").split(/\t/) tmp.collect!{ |val| val.strip } if tmp.size == 2 data << [tmp[0], tmp[1]] else $stderr.puts "ERR; cannot read this line.; " + line.chomp end } rescue Errno::ENOENT return '' end #展開;日付展開;日付→マーク,表示日付文字列,日付x2(始端/終端) data.collect!{ |date, value| #日付→日付,マーク;(date->date,mark) mark = '' if date =~ /^([x\*_udc]+)(.+)$/i mark, date = $1.downcase, $2 end #日付→始端日付,終端日付;(date->time[0], time[1]) date = date + '-' + date if date !~ /-/ #日付x1→x2 time = date.split('-') if time.size != 2 $stderr.puts "ERR; date-string.; " + date continue end year, month, day = '', '', '' #日付→年,月,日;省略項は補完 tmp = time[0].split(/[\/\.]/) #まず始端日付 year = tmp.shift if tmp.size > 0 month = tmp.shift if tmp.size > 0 day = tmp.shift if tmp.size > 0 if (year.size > 4) && (month == '') && (day == '') year = time[0][0..3] month = time[0][4..5] if time[0].size > 4 day = time[0][6..-1] if time[0].size > 6 end time[0] = [year, month, day] tmp = time[1].split(/[\/\.]/) #次に終端日付 day = tmp.pop if tmp.size > 0 month = tmp.pop if tmp.size > 0 year = tmp.pop if tmp.size > 0 if (day.size > 4) && (time[1] !~ /\//) year, month, day = time[1][0..3], time[1][4..5], time[1][6..-1] end time[1] = [year, month, day] #日付→表示日付文字列;「月/日」が基準で、後は略せるだけ略;(time->datestr) tmp0 = [time[0][1], time[0][2]].join('/') tmp1 = '' tmp1 = time[0].join('/') if time[0][0] != time[1][0] tmp1 = time[1].join('/') if time[0][0] != time[1][0] tmp1 = [time[1][1], time[1][2]].join('/') if time[0][1] != time[1][1] tmp1 = time[1][2] if time[0][2] != time[1][2] datestr = tmp0 datestr << '-' + tmp1 if tmp1 != '' #日付イレギュラー文字列展開;年,月,日→数値と「?」のみで構成される8桁日付 time[0][2].sub!(/上[旬]*/,'01') time[0][2].sub!(/中[旬]*/,'11') time[0][2].sub!(/下[旬]*/,'21') time[0][2].sub!(/前[期]*/,'01') time[0][2].sub!(/後[期]*/,'16') time[0][2].sub!(/未[定]*/,'01') time[0][2].sub!(/不[明]*/,'01') time[1][2].sub!(/上[旬]*/,'10') time[1][2].sub!(/中[旬]*/,'20') time[1][2].sub!(/下[旬]*/,'31') time[1][2].sub!(/前[期]*/,'15') time[1][2].sub!(/後[期]*/,'31') time[1][2].sub!(/未[定]*/,'31') time[1][2].sub!(/不[明]*/,'31') time.collect!{ |year, month, day| #「x」→「?」 year.gsub!(/x/i,'?') month.gsub!(/x/i,'?') day.gsub!(/x/i,'?') #まだ残る謎文字列→削除 year.gsub!(/[^\d\?]+/,'') month.gsub!(/[^\d\?]+/,'') day.gsub!(/[^\d\?]+/,'') #数字は「0」補完桁合せ year = '0' * (4-year.size) + year if year =~ /^\d[\d\?]*$/ month = '0' * (2-month.size) + month if month =~ /^\d[\d\?]*$/ day = '0' * (2-day.size) + day if day =~ /^\d[\d\?]*$/ #隙間は「?」補完桁合せ year = '?' * (4-year.size) + year month = '?' * (2-month.size) + month day = '?' * (2-day.size) + day str = year + month + day if str.size > 8 $stderr.puts "ERR; date-string.; " + str continue end str } #「?」展開 time[0].gsub!(/\?/,'0') #「?」を始端方向へリセット time[0][4..5] = '01' if (time[0][4..5].to_i == 0) || (time[0][4..5].to_i > 12) time[0][6..7] = '01' if (time[0][6..7].to_i == 0) || (time[0][6..7].to_i > 31) time[1].gsub!(/\?/,'9') #「?」を終端方向へリセット time[1][4..5] = '12' if (time[1][4..5].to_i == 0) || (time[1][4..5].to_i > 12) time[1][6..7] = '31' if (time[1][6..7].to_i == 0) || (time[1][6..7].to_i > 31) year, month, day = time[1][0..3].to_i, time[1][4..5].to_i, time[1][6..7].to_i if Date.exist?(year, month, day).nil? while Date.exist?(year, month, day).nil? day = day - 1 end time[1][6..7] = day.to_s end #8桁日付→Time日付 time[0] = Time.local(time[0][0..3].to_i, time[0][4..5].to_i, time[0][6..7].to_i) time[1] = Time.local(time[1][0..3].to_i, time[1][4..5].to_i, time[1][6..7].to_i) line = [time[0], time[1], datestr, value, mark] } #抽出;日付期間が表示期間に被ってる件のみ data.each{ |line| time0, time1, datestr, value, mark = line flag = false flag = true if (start <= time0) && (time0 <= last) flag = true if (start <= time1) && (time1 <= last) flag = true if (time0 <= start) && (last <= time1) schedule << line if flag == true } #並替;月毎に;数値と「?」以外を含む表示日付は上に month = {} schedule.each { |line| #まず月毎に分割 key = line[1].strftime("%Y%m") month[key] = [] if month[key].nil? month[key] << line } schedule = [] month.sort.each{ |key, mon| #月内で2分割 data0, data1 = [], [] mon.each { |line| if line[2] =~ /[^\d\?\/-]/ data0 << line #特殊文字列含 else data1 << line #数値と「?」 end } #個々にソートして結合 data0.sort! data1.sort! data0.each { |line| schedule << line } data1.each { |line| schedule << line } } #出力 str = '' cnt = schedule.size < max ? schedule.size : max # str << "\n" if opt['title'] str = "
\n
#{opt['title']}
\n
#{str}
\n
" end return str end