#!/usr/bin/ruby HTML_HEADER = < Calendar EOH HTML_FOOTER = < EOH MONTH = %w[Janvier Fevrier Mars Avril Mai Juin Juillet Aout Septembre Octobre Novembre Decembre] DAY = %w[lundi mardi mercredi jeudi vendredi samedi dimanche] class Time def weekday # wday => sun = 0, mon = 1, ... (wday - 1) % 7 end end def month(m, y) while m < 1 # -4/2017 => 08/2016 m += 12 y -= 1 end while m > 12 # 13/2017 => 01/2018 m -= 12 y += 1 end html = [] date = Time.mktime(y, m, 1, 12) #

janvier 2017

html << "\t\t

#{MONTH[date.month-1]} #{date.year}

" html << "\t\t" html << "\t\t\t#{DAY.map { |wday| "" }.join}" week_html = "\t\t\t" date.weekday.times { # monday is previous month: skip table entries week_html << '' } while date.month == m # create table entries with day nr in them week_html << "" if date.weekday == 6 # done sunday, start new week week_html << '' html << week_html week_html = "\t\t\t" end date += 24*3600 end if date.weekday > 0 # sunday is following month: skip table entries while date.weekday > 0 week_html << '' date += 24*3600 end week_html << '' html << week_html end html end case ARGV.length when 0 m = nil y = Time.now.year when 1 m = nil y = ARGV[0].to_i if y >= 1 and y <= 12 m = y y = Time.now.year end when 2 m, y = ARGV[0].to_i, ARGV[1].to_i m, y = y, m if m > y end outfile = "cal.html" puts "Generating #{outfile}" File.open(outfile, 'wb') { |fd| fd.puts HTML_HEADER if m puts "#{m}/%04d" % y fd.puts month(m, y) else 12.times { |m| m += 1 puts "#{m}/%04d" % y fd.puts month(m, y) fd.puts } end fd.puts HTML_FOOTER }
#{wday}
#{date.day}