''' Python script to generate HTML for an empty calendar
    The year number should be supplied as a command line parameter
    Output should probably be redirected to a file, like this:
         python calendar.py 2010 > 2010diary.html'''

import sys
from datetime import date, timedelta


# The resulting file can then be edited in any editor to add calendar entries
#  dayoff class and holiday class can also be used to denote table cells
#
# This script was created by activityworkshop.net and is placed in the public domain.


def generate_year(year):
    '''Outputs a html page containing an empty calendar for the given year'''
    print(get_start_html(year))
    print(get_year_head(year))
    for month in range(12):
        start_day = date(year, month+1, 1)
        print("<p class='monthHead'>%s</p>" % start_day.strftime("%B %Y"))
        print(get_month_start())
        print("<tr>")
        num_blanks = start_day.isoweekday() - 1
        for _ in range(num_blanks):
            print("<td></td>")
        curr_day = start_day
        one_day = timedelta(days=1)
        while curr_day.month == (month+1):
            if curr_day.isoweekday() == 1 and curr_day.day > 1:
                print("</tr><tr>")
            style = " class='dayoff'" if curr_day.isoweekday() > 5 else ""
            print("<td" + style + "><div class='date'>%d</div></td>" % curr_day.day)
            curr_day += one_day
        print("</tr>")
        print("</table>")
    print("<br>\n" + get_year_head(year, False))
    print("\n" + get_end_html())

def get_start_html(year):
    '''Returns the html for the start of the page, including styles'''
    return ("<!DOCTYPE html>\n<html lang=\"en\">\n<head>\n" +
            "<title>" + str(year) + "</title>\n" +
            '''    <style type="text/css">
            body {font-family:Arial}
            h1 {margin:0px;padding:0px; text-decoration:underline}
            .dayHead {width:14%; font-weight:bold; font-size:110%}
            .monthHead {font-weight:bold; font-size:150%; margin-bottom:2px}
            .dayCell {border:1px solid}
            .date {float:left; border:1px solid; font-size:70%; margin-right:3px; padding:1px;
                   background-color:#fff; color:#000}
            .dayoff {background-color:#ccc; border:1px solid}
            .holiday {background-color:#900; color:#fff; border:1px solid}
            tr {vertical-align:top}
        </style>
    </head>
    <body>''')

def get_end_html():
    '''Returns the html for the end of the page'''
    return "</body>\n</html>"

def get_year_head(year, show_year=True):
    '''Returns the heading with links to previous and next years.
       If show_year is true then it shows the current year too'''
    html = "<table border='0' width='90%'><tr>\n"
    html = html + "<td width='20%'><p align='left'><a href='" + str(year-1) + \
           "diary.html'>&lt; - " + str(year-1) + "</a></p></td>\n"
    if show_year:
        html = html + "<td width='60%'><center><h1>" + str(year) + "</h1></center></td>\n"
    else:
        html = html + "<td width='60%'>&nbsp;</td>\n"
    html = html + "<td width='20%'><p align='right'><a href='" + str(year+1) + \
           "diary.html'>" + str(year+1) + " - &gt;</a></p></td>\n</tr>\n</table>"
    return html

def get_month_start():
    '''Returns the table definition including day names on the top row'''
    html = "<table border='1' width='100%'><tr>"
    test_date = date.today()
    one_day = timedelta(days=1)
    while test_date.isoweekday() > 1:
        test_date = test_date + one_day
    for _ in range(7):
        html = html + "<td class='dayHead'>" + test_date.strftime("%a") + "</td>"
        test_date = test_date + one_day
    html = html + "</tr>"
    return html

# Use the given command line arguments to call generate_year
if __name__ == "__main__":
    NUM_ARGS = len(sys.argv)
    if NUM_ARGS == 1:
        print("Calendar generator")
        print("You need to give a year number too, like 'python calendar.py 2010'")
    elif NUM_ARGS == 2:
        try:
            YEAR = int(sys.argv[1])
            if YEAR < 50:
                YEAR += 2000
            if YEAR >= 1900 and YEAR <= 3000:
                generate_year(YEAR)
            else:
                print("Calendar generator")
                print("The year should be between 1900 and 3000")
        except ValueError:
            print("Calendar generator")
            print("The year should be a number, not '%s'" % sys.argv[1])
    else:
        print("Calendar generator")
        print("You gave more than one parameter, only a year number is required")
