| 1 | import time |
|---|
| 2 | import calendar |
|---|
| 3 | from cStringIO import StringIO |
|---|
| 4 | |
|---|
| 5 | cell_template = '<td%(style)s><a href="%(url)s"%(class)s>%(day)s</a></td>' |
|---|
| 6 | |
|---|
| 7 | def execute(hdf, fmt, env): |
|---|
| 8 | today = time.localtime() |
|---|
| 9 | year = int(hdf.getValue("args.year", str(today.tm_year))) |
|---|
| 10 | month = int(hdf.getValue("args.month", str(today.tm_mon))) |
|---|
| 11 | curr_day = None |
|---|
| 12 | if year == today.tm_year and month == today.tm_mon: |
|---|
| 13 | curr_day = today.tm_mday |
|---|
| 14 | |
|---|
| 15 | # Can use this to change the day the week starts on, but this |
|---|
| 16 | # is a system-wide setting |
|---|
| 17 | #calendar.setfirstweekday(calendar.SUNDAY) |
|---|
| 18 | cal = calendar.monthcalendar(year, month) |
|---|
| 19 | |
|---|
| 20 | |
|---|
| 21 | date = [year, month] + [1] * 7 |
|---|
| 22 | |
|---|
| 23 | # url to the current page (used in the navigation links) |
|---|
| 24 | thispageURL = env.href.wiki(hdf.getValue('wiki.page_name', '')) |
|---|
| 25 | # for the prev/next navigation links |
|---|
| 26 | prevMonth = month-1 |
|---|
| 27 | prevYear = year |
|---|
| 28 | nextMonth = month+1 |
|---|
| 29 | nextYear = year |
|---|
| 30 | # check for year change (KISS version) |
|---|
| 31 | if prevMonth == 0: |
|---|
| 32 | prevMonth = 12 |
|---|
| 33 | prevYear -= 1 |
|---|
| 34 | if nextMonth == 13: |
|---|
| 35 | nextMonth = 1 |
|---|
| 36 | nextYear += 1 |
|---|
| 37 | |
|---|
| 38 | # building the output |
|---|
| 39 | buff = StringIO() |
|---|
| 40 | buff.write('<table><caption>') |
|---|
| 41 | |
|---|
| 42 | # prev month link |
|---|
| 43 | prevMonthURL = thispageURL+'?month=%d&year=%d' % (prevMonth, prevYear) |
|---|
| 44 | buff.write('<a href="%s">< </a>' % prevMonthURL) |
|---|
| 45 | # the caption |
|---|
| 46 | buff.write(time.strftime('%B %Y', tuple(date))) |
|---|
| 47 | # next month link |
|---|
| 48 | nextMonthURL = thispageURL+'?month=%d&year=%d' % (nextMonth, nextYear) |
|---|
| 49 | buff.write('<a href="%s"> ></a>' % nextMonthURL) |
|---|
| 50 | buff.write('</caption>\n<thead><tr align="center">') |
|---|
| 51 | |
|---|
| 52 | for day in calendar.weekheader(2).split(): |
|---|
| 53 | buff.write('<th scope="col">%s</th>' % day) |
|---|
| 54 | buff.write('</tr></thead>\n<tbody>\n') |
|---|
| 55 | |
|---|
| 56 | for row in cal: |
|---|
| 57 | buff.write('<tr align="right">') |
|---|
| 58 | for day in row: |
|---|
| 59 | if not day: |
|---|
| 60 | buff.write('<td> </td>') |
|---|
| 61 | else: |
|---|
| 62 | date[2] = day |
|---|
| 63 | wiki = time.strftime(fmt, tuple(date)) |
|---|
| 64 | exists = env._wiki_pages.has_key(wiki) |
|---|
| 65 | url = env.href.wiki(wiki) |
|---|
| 66 | if not exists: |
|---|
| 67 | url += "?action=edit" |
|---|
| 68 | buff.write(cell_template % { |
|---|
| 69 | 'url': url, |
|---|
| 70 | 'day': day, |
|---|
| 71 | 'style': day == curr_day and ' style="border: 1px solid #b00;"' or '', |
|---|
| 72 | 'class': not exists and ' class="missing"' or '', |
|---|
| 73 | }) |
|---|
| 74 | |
|---|
| 75 | buff.write('</tr>\n') |
|---|
| 76 | |
|---|
| 77 | buff.write('</tbody>\n</table>') |
|---|
| 78 | |
|---|
| 79 | table = buff.getvalue() |
|---|
| 80 | buff.close() |
|---|
| 81 | return table |
|---|