Skip to content

Instantly share code, notes, and snippets.

@ne-sachirou
Last active December 19, 2015 04:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ne-sachirou/5899816 to your computer and use it in GitHub Desktop.
Save ne-sachirou/5899816 to your computer and use it in GitHub Desktop.
Calendar generator for mitsui. RubyにカレンダーっぽいHTMLを吐き出さすまで物語 - c4se記:さっちゃんですよ☆ http://c4se.hatenablog.com/entry/2013/07/01/193049
.bundle/
vendor/
setl et
setl sw=2
setl sts=2
setl ff=unix
<table>
<tr>
<td colspan="7"><%=weeks.year%>年<%=weeks.month%>月</td>
</tr>
<tr class="dow">
<td class="holiday">日</td>
<td class="weekend">月</td>
<td class="weekend">火</td>
<td class="weekend">水</td>
<td class="weekend">木</td>
<td class="weekend">金</td>
<td class="holiday">土</td>
</tr>
<% weeks.each do |week| %>
<tr>
<% week.each do |date|
class_name =
weeks.miss?(date) ? 'null' :
(date.holiday? ? 'holiday' : 'weekday') %>
<td class="<%=class_name%>"><%=date.day%></td>
<% end %>
</tr>
<% end %>
</table>
# encoding=utf-8
require 'date'
require 'holiday_jp'
require 'erubis'
class Date
# A Date which is the end of the month.
#
# return [Date]
def end_of_month
next_month.prev_day
end
# Is Japanese holiday?
#
# return [Boolean]
def holiday?
wday == 0 || wday == 6 || HolidayJp.holiday?(self)
end
end
# Weeks of a month.
class Weeks
include Enumerable
attr_accessor :year, :month, :weeks
# param [Number] year
# param [Number] month
def initialize year, month
@year = year
@month = month
@weeks = generate_weeks
end
# Isn't the date in current month?
#
# param [Date] date
def miss? date
@month != date.month
end
# Each week (7 Date).
#
# param [block] &proc
def each &proc
@weeks.each &proc
end
private
# Generate weeks.
#
# return [Array] of weeks (7 Date)
def generate_weeks
dates = gen_prev_month + gen_current_month + gen_next_month
dates.each_slice(7).to_a
end
# Generate a Date sequence of prev month.
#
# return [Array] of Date
def gen_prev_month
prev_month_end_day = Date.new(@year, @month).prev_month.end_of_month.day
start_date = Date.new @year, @month, 1
gen_sequence (1 - start_date.wday .. 0).map{|n| n + prev_month_end_day }, @month - 1
end
# Generate a Date sequence of current month.
#
# return [Array] of Date
def gen_current_month
end_date = Date.new(@year, @month).end_of_month
gen_sequence 1 .. end_date.day
end
# Generate a Date sequence of next month.
#
# return [Array] of Date
def gen_next_month
end_date = Date.new(@year, @month).end_of_month
gen_sequence (end_date.wday + 1 .. 6).each_with_index.map{|n, date| date + 1 }, @month + 1
end
# Generate a Date sequence.
#
# param [Enumerable] days of Number
# param [Number] month
# param [Number] year
# return [Array] of Date
def gen_sequence days, month=@month, year=@year
if month <= 0
year -= 1
month += 12
elsif month > 12
year += 1
month %= 12
end
days.map{|day| Date.new year, month, day }
end
end
# Render a calendar from a template.
#
# param [Weeks] weeks
# return [String]
def render_calendar weeks
open 'calendar.erb', 'r:utf-8' do |file|
eruby = Erubis::Eruby.new file.read
eruby.result binding
end
end
# Copy html String to a clip board.
#
# param [String] html
def copy html
if RbConfig::CONFIG['target_os'] =~ /mswin|mingw|cygwin/
open('tmp.html', 'w:cp932'){|file| file.write html }
system 'clip < tmp.html'
File.delete 'tmp.html'
else
system "echo '#{html}' | pbcopy"
end
end
if ARGV[0]
match = ARGV[0].match %r{(?:(\d{4})[-/])?(\d{1,2})}
year = match[1] ? match[1].to_i : Date.today.year
month = match[2].to_i
else
year = Date.today.year
month = Date.today.month
end
weeks = Weeks.new year, month
html = render_calendar weeks
copy html
puts 'calendar copyed!'
source 'https://rubygems.org'
gem 'holiday_jp'
gem 'erubis'
GEM
remote: https://rubygems.org/
specs:
erubis (2.7.0)
holiday_jp (0.3.1)
PLATFORMS
x86-mingw32
DEPENDENCIES
erubis
holiday_jp
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment