diff options
author | Oz N Tiram <oz.tiram@gmail.com> | 2017-06-06 09:35:59 (GMT) |
---|---|---|
committer | Walter Dörwald <walter@livinglogic.de> | 2017-06-06 09:35:59 (GMT) |
commit | 8b7a4cc40e9b2f34da94efb75b158da762624015 (patch) | |
tree | 5dd313ea843dbb65f0dd5702b61c5feafb43e5a6 /Lib/calendar.py | |
parent | 167e0fc211c06df52aff1a81f513763374cb4f88 (diff) | |
download | cpython-8b7a4cc40e9b2f34da94efb75b158da762624015.zip cpython-8b7a4cc40e9b2f34da94efb75b158da762624015.tar.gz cpython-8b7a4cc40e9b2f34da94efb75b158da762624015.tar.bz2 |
bpo-30095: Make CSS classes used by calendar.HTMLCalendar customizable (GH-1439)
Several class attributes have been added to calendar.HTMLCalendar that allow customization of the CSS classes used in the resulting HTML. This can be done by subclasses HTMLCalendar and overwriting those class attributes (Patch by Oz Tiram).
Diffstat (limited to 'Lib/calendar.py')
-rw-r--r-- | Lib/calendar.py | 36 |
1 files changed, 30 insertions, 6 deletions
diff --git a/Lib/calendar.py b/Lib/calendar.py index 28ac56f..0218e2d 100644 --- a/Lib/calendar.py +++ b/Lib/calendar.py @@ -382,12 +382,31 @@ class HTMLCalendar(Calendar): # CSS classes for the day <td>s cssclasses = ["mon", "tue", "wed", "thu", "fri", "sat", "sun"] + # CSS classes for the day <th>s + cssclasses_weekday_head = cssclasses + + # CSS class for the days before and after current month + cssclass_noday = "noday" + + # CSS class for the month's head + cssclass_month_head = "month" + + # CSS class for the month + cssclass_month = "month" + + # CSS class for the year's table head + cssclass_year_head = "year" + + # CSS class for the whole year table + cssclass_year = "year" + def formatday(self, day, weekday): """ Return a day as a table cell. """ if day == 0: - return '<td class="noday"> </td>' # day outside month + # day outside month + return '<td class="%s"> </td>' % self.cssclass_noday else: return '<td class="%s">%d</td>' % (self.cssclasses[weekday], day) @@ -402,7 +421,8 @@ class HTMLCalendar(Calendar): """ Return a weekday name as a table header. """ - return '<th class="%s">%s</th>' % (self.cssclasses[day], day_abbr[day]) + return '<th class="%s">%s</th>' % ( + self.cssclasses_weekday_head[day], day_abbr[day]) def formatweekheader(self): """ @@ -419,7 +439,8 @@ class HTMLCalendar(Calendar): s = '%s %s' % (month_name[themonth], theyear) else: s = '%s' % month_name[themonth] - return '<tr><th colspan="7" class="month">%s</th></tr>' % s + return '<tr><th colspan="7" class="%s">%s</th></tr>' % ( + self.cssclass_month_head, s) def formatmonth(self, theyear, themonth, withyear=True): """ @@ -427,7 +448,8 @@ class HTMLCalendar(Calendar): """ v = [] a = v.append - a('<table border="0" cellpadding="0" cellspacing="0" class="month">') + a('<table border="0" cellpadding="0" cellspacing="0" class="%s">' % ( + self.cssclass_month)) a('\n') a(self.formatmonthname(theyear, themonth, withyear=withyear)) a('\n') @@ -447,9 +469,11 @@ class HTMLCalendar(Calendar): v = [] a = v.append width = max(width, 1) - a('<table border="0" cellpadding="0" cellspacing="0" class="year">') + a('<table border="0" cellpadding="0" cellspacing="0" class="%s">' % + self.cssclass_year) a('\n') - a('<tr><th colspan="%d" class="year">%s</th></tr>' % (width, theyear)) + a('<tr><th colspan="%d" class="%s">%s</th></tr>' % ( + width, self.cssclass_year_head, theyear)) for i in range(January, January+12, width): # months in this row months = range(i, min(i+width, 13)) |