# $Id: schedule.rb,v 1.2 2003/08/21 12:38:37 not Exp $
# 2003/09/14 微修正
=begin
schedule.rb
nDiary Plug-in スケジュールの表示
今後のスケジュールを表示します。
option:
format=str strは任意の文字列。以下の文字列は展開されます。
%A 曜日の名称(Sunday, Monday...)
%a 曜日の省略名(Sun, Mon...)
%B 月の名称(January, February...)
%b 月の省略名(Jan, Feb...)
%c 日付と時刻
%d 日(01-31)
%j 年中の通算日(001-366)
%m 月を表す数字(01-12)
%U 週を表す数.最初の日曜日が第1週の始まり(00-53)
%W 週を表す数.最初の月曜日が第1週の始まり(00-53)
%w 曜日を表す数.日曜日が0(0-6)
%x 日付
%Y 西暦を表す数
%y 西暦の下2桁(00-99)
%% %自身
title=str スケジュールに付けるタイトルを指定します。
max=num 表示する最大行数
past=num num日前の経過してしまった日付の情報から表示します
month=true 今月分を表示します
style=normal
を利用した表記をします
exsample:
=end
def plug(opt)
max = opt['max'].to_i
max = 8 if max == 0
flag_month = opt['month'] ? true : false
today = Time::now
past = today - opt['past'].to_i * 86400
coming = today + 30 * 86400
start = past.strftime("%Y%m%d")
last = coming.strftime("%Y%m%d")
now = today.strftime("%Y%m%d")
schedule = {}
opt['format'] = opt['format'] ? opt['format'] : '%0m/%0d'
begin
open(opt['file'], 'r'){ |file|
file.each{ |line|
date, value = line.split(/\t/)
date.gsub!(/[-\/]/, '')
if (flag_month == false && start <= date && date <= last) || (flag_month && now[0..5] == date[0..5])
schedule[date] = [] unless schedule[date]
schedule[date] << value
end
}
}
rescue Errno::ENOENT
return ''
end
cnt = 0
str = ''
if opt['sytle'] == 'normal'
str << %Q!\n!
else
str << %Q!\n!
end
schedule.sort.each{ |date, schedules|
break if cnt >= max
t = nil
begin
t = Time::local(date[0..3].to_i, date[4..5].to_i, date[6..7].to_i)
day = t.strftime2(opt['format'])
rescue
day = date
end
if cnt <= max then
schedules.sort.each{ |line|
cnt += 1
if opt['sytle'] == 'normal'
str << %Q!\t
- #{day} : #{line}
\n!
else
if date < now
str << %Q!#{day} : #{line}
\n!
else
str << %Q!#{day} : #{line}
\n!
end
end
}
end
}
if opt['sytle'] == 'normal'
str << "\n"
else
str << "\n"
end
if opt['title'] then
str = %Q!\n- #{opt['title']}
\n- #{str}
\n
!
end
return str
end