1.1 --- a/README.txt Fri Aug 26 23:19:47 2005 +0000
1.2 +++ b/README.txt Sun Aug 28 23:45:07 2005 +0000
1.3 @@ -46,7 +46,7 @@
1.4 ------------------------------------------------
1.5
1.6 Made a new XSLTools package and moved XSLOutput into it.
1.7 -Added XMLTable (to the XSLTools package).
1.8 +Added XMLCalendar and XMLTable (to the XSLTools package).
1.9 Changed in-page requests to contain proper POST data.
1.10 Added Debian package support.
1.11 Added missing COPYING.txt file.
2.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
2.2 +++ b/XSLTools/XMLCalendar.py Sun Aug 28 23:45:07 2005 +0000
2.3 @@ -0,0 +1,47 @@
2.4 +#!/usr/bin/env python
2.5 +
2.6 +import libxml2dom
2.7 +import calendar
2.8 +
2.9 +# Borrowed from calendar, but with a non-global first weekday variable.
2.10 +
2.11 +def monthcalendar(year, month, firstweekday=0):
2.12 + """Return a matrix representing a month's calendar.
2.13 + Each row represents a week; days outside this month are zero."""
2.14 + day1, ndays = calendar.monthrange(year, month)
2.15 + rows = []
2.16 + r7 = range(7)
2.17 + day = (firstweekday - day1 + 6) % 7 - 5 # for leading 0's in first week
2.18 + while day <= ndays:
2.19 + row = [0, 0, 0, 0, 0, 0, 0]
2.20 + for i in r7:
2.21 + if 1 <= day <= ndays: row[i] = day
2.22 + day = day + 1
2.23 + rows.append(row)
2.24 + return rows
2.25 +
2.26 +# XML production functions.
2.27 +
2.28 +def write_calendar_for_month(doc, year, month):
2.29 + weeks = monthcalendar(year, month)
2.30 + month_element = doc.childNodes[-1].appendChild(doc.createElement("month"))
2.31 + for dates in weeks:
2.32 + week_element = month_element.appendChild(doc.createElement("week"))
2.33 + for date in dates:
2.34 + day_element = week_element.appendChild(doc.createElement("day"))
2.35 + if date != 0:
2.36 + day_element.setAttribute("date", "%04d%02d%02d" % (year, month, date))
2.37 +
2.38 +def get_calendar_for_month(year, month):
2.39 + d = libxml2dom.createDocument(None, "calendar", None)
2.40 + write_calendar_for_month(d, year, month)
2.41 + return d
2.42 +
2.43 +if __name__ == "__main__":
2.44 + import sys
2.45 + try:
2.46 + print get_calendar_for_month(int(sys.argv[1]), int(sys.argv[2])).toString()
2.47 + except (IndexError, ValueError):
2.48 + print "Please specify a year and a month (numeric, 1-12)."
2.49 +
2.50 +# vim: tabstop=4 expandtab shiftwidth=4