Skip to content

Instantly share code, notes, and snippets.

@melborne

melborne/mycal.rb

Created Apr 30, 2009
Embed
What would you like to do?
Sinatra Demo Calendar
require "rubygems"
require "sinatra"
get '/' do
@year = Time.now.year
@ycal = cal(@year)
erb :index
end
get '/:year' do |year|
@year = year.to_i
@ycal = cal(@year)
erb :year
end
get '/:year/:mon' do |*ym|
@year, @mon = ym.map { |x| x.to_i }
@mcal = cal(@year, @mon)
erb :mon
end
get '/:year/:mon/:day' do |*ymd|
@year, @mon, @day = ymd.map { |x| x.to_i }
@dcal = cal(@year, @mon).sub(/\s#{@day}\s/, '<span id="target">\0</span>')
@message = 'Webカレンダー完成予定日'
@message += '<br/>って、期限過ぎてるじゃん!' if Time.local(*ymd) - Time.now < 0
erb :day
end
configure do
MONTHS = %w(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec)
end
helpers do
def cal(*date)
year, mon = date
cal = if mon
calendar(mon, year)
else
(1..12).inject("") do |s, m|
monthly_cal = "<div class='mon_cal' id='mon_#{m}'>" + calendar(m, year) + "</div>"
#make days link
monthly_cal.gsub!(/(\s([1-3]?[0-9](?=\D)))/) do
%Q{<a class="days_link" href="/#{year}/#{m}/#{$2}">#{$1}</a>}
end
#make today tag
today = Time.now
if m == today.mon and year == today.year
monthly_cal.sub!(/\s#{today.day}(?=\D)/, '<span id="today">\0</span>')
end
s << monthly_cal
end
end
cal.gsub(/((#{MONTHS.join('|')})\w*)/) do
%Q{<a href="/#{year}/#{MONTHS.index($2)+1}">#{$1}</a>}
end
end
def calendar(m, y)
`cal #{m} #{y}`
end
end
__END__
@@layout
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html>
<head>
<meta http-equiv="Content-type" content="text/html; charset=utf-8">
<title>mycal</title>
<style type="text/css" media="screen">
* { margin: 0; padding: 0;}
body {background:#eed;}
#banner { margin:20px; margin-left:auto; margin-right:auto; width:700px; color:#906; }
#title { margin-left:250px;}
#year_pred { float:left; }
#year_next { float:right; }
#main { position:absolute; left:50%; margin:30px 0 0 -360px; height:60%;}
.ycal { color:#514; font-weight:bold;}
.mon_cal { float:left; margin:5px 10px 5px 10px;}
#mon_5, #mon_9 {clear:left;}
#mon { margin-left:100px;}
.mcal { color:#217; font-size:32pt; margin-top:10px; clear:right; }
#day { margin-left:250px;}
.dcal { color:#217; font-size:16pt; clear:right; }
h3#date { font-size:24pt; color:#c06;}
#message { font-size:14pt; color:#360; margin:30px 0 30px 0;}
#target { color:#c06;}
a {text-decoration:none; color:#514;}
a.days_link:link, a.days_link:visited { color:#217;}
a:hover, a.days_link:hover { color:#383;}
#today { color:#060;}
</style>
</head>
<body id="mycal">
<div id="banner">
<h1><a id='title' href='/'>Web Calendar</a></h1>
<div id="year_pred"><a href="/<%= @year-1 %>">Previous Year</a></div>
<div id="year_next"><a href="/<%= @year+1 %>">Next Year</a></div>
</div>
<div id="main">
<%= yield %>
</div>
</body>
</html>
@@index
<div id="index">
<pre class="ycal"><%= @ycal %></pre>
</div>
@@year
<div id="year">
<pre class="ycal"><%= @ycal %></pre>
</div>
@@mon
<div id="mon">
<pre class="mcal"><%= @mcal %></pre>
</div>
@@day
<div id="day">
<h3 id="date"><%= @year %>年<%= @mon %>月<%= @day %>日</h3>
<p id="message"><%= @message %></p>
<pre class="dcal"><%= @dcal %></pre>
</div>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.