Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 8b7a4cc

Browse files
oz123doerwalter
authored andcommitted
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).
1 parent 167e0fc commit 8b7a4cc

5 files changed

Lines changed: 205 additions & 44 deletions

File tree

Doc/library/calendar.rst

Lines changed: 80 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ it's the base calendar for all computations.
147147
This class can be used to generate HTML calendars.
148148

149149

150-
:class:`HTMLCalendar` instances have the following methods:
150+
:class:`!HTMLCalendar` instances have the following methods:
151151

152152
.. method:: formatmonth(theyear, themonth, withyear=True)
153153

@@ -171,6 +171,85 @@ it's the base calendar for all computations.
171171
output (defaulting to the system default encoding).
172172

173173

174+
:class:`!HTMLCalendar` has the following attributes you can override to
175+
customize the CSS classes used by the calendar:
176+
177+
.. attribute:: cssclasses
178+
179+
A list of CSS classes used for each weekday. The default class list is::
180+
181+
cssclasses = ["mon", "tue", "wed", "thu", "fri", "sat", "sun"]
182+
183+
more styles can be added for each day::
184+
185+
cssclasses = ["mon text-bold", "tue", "wed", "thu", "fri", "sat", "sun red"]
186+
187+
Note that the length of this list must be seven items.
188+
189+
190+
.. attribute:: cssclass_noday
191+
192+
The CSS class for a weekday occurring in the previous or coming month.
193+
194+
.. versionadded:: 3.7
195+
196+
197+
.. attribute:: cssclasses_weekday_head
198+
199+
A list of CSS classes used for weekday names in the header row.
200+
The default is the same as :attr:`cssclasses`.
201+
202+
.. versionadded:: 3.7
203+
204+
205+
.. attribute:: cssclass_month_head
206+
207+
The month's head CSS class (used by :meth:`formatmonthname`).
208+
The default value is ``"month"``.
209+
210+
.. versionadded:: 3.7
211+
212+
213+
.. attribute:: cssclass_month
214+
215+
The CSS class for the whole month's table (used by :meth:`formatmonth`).
216+
The default value is ``"month"``.
217+
218+
.. versionadded:: 3.7
219+
220+
221+
.. attribute:: cssclass_year
222+
223+
The CSS class for the whole year's table of tables (used by
224+
:meth:`formatyear`). The default value is ``"year"``.
225+
226+
.. versionadded:: 3.7
227+
228+
229+
.. attribute:: cssclass_year_head
230+
231+
The CSS class for the table head for the whole year (used by
232+
:meth:`formatyear`). The default value is ``"year"``.
233+
234+
.. versionadded:: 3.7
235+
236+
237+
Note that although the naming for the above described class attributes is
238+
singular (e.g. ``cssclass_month`` ``cssclass_noday``), one can replace the
239+
single CSS class with a space separated list of CSS classes, for example::
240+
241+
"text-bold text-red"
242+
243+
Here is an example how :class:`!HTMLCalendar` can be customized::
244+
245+
class CustomHTMLCal(calendar.HTMLCalendar):
246+
cssclasses = [style + " text-nowrap" for style in
247+
calendar.HTMLCalendar.cssclasses]
248+
cssclass_month_head = "text-center month-head"
249+
cssclass_month = "text-center month"
250+
cssclass_year = "text-italic lead"
251+
252+
174253
.. class:: LocaleTextCalendar(firstweekday=0, locale=None)
175254

176255
This subclass of :class:`TextCalendar` can be passed a locale name in the

Doc/whatsnew/3.7.rst

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -103,13 +103,6 @@ New Modules
103103
Improved Modules
104104
================
105105

106-
cgi
107-
---
108-
109-
:func:`~cgi.parse_multipart` returns the same results as
110-
:class:`~FieldStorage` : for non-file fields, the value associated to a key
111-
is a list of strings, not bytes.
112-
(Contributed by Pierre Quentel in :issue:`29979`.)
113106

114107
binascii
115108
--------
@@ -118,6 +111,22 @@ The :func:`~binascii.b2a_uu` function now accepts an optional *backtick*
118111
keyword argument. When it's true, zeros are represented by ``'`'``
119112
instead of spaces. (Contributed by Xiang Zhang in :issue:`30103`.)
120113

114+
115+
calendar
116+
--------
117+
118+
The :class:`~calendar.HTMLCalendar` has added new class attribute which ease the
119+
customisation the CSS classes in the produced HTML calendar.
120+
(Contributed by Oz Tiram in :issue:`30095`.)
121+
122+
cgi
123+
---
124+
125+
:func:`~cgi.parse_multipart` returns the same results as
126+
:class:`~FieldStorage` : for non-file fields, the value associated to a key
127+
is a list of strings, not bytes.
128+
(Contributed by Pierre Quentel in :issue:`29979`.)
129+
121130
contextlib
122131
----------
123132

Lib/calendar.py

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -382,12 +382,31 @@ class HTMLCalendar(Calendar):
382382
# CSS classes for the day <td>s
383383
cssclasses = ["mon", "tue", "wed", "thu", "fri", "sat", "sun"]
384384

385+
# CSS classes for the day <th>s
386+
cssclasses_weekday_head = cssclasses
387+
388+
# CSS class for the days before and after current month
389+
cssclass_noday = "noday"
390+
391+
# CSS class for the month's head
392+
cssclass_month_head = "month"
393+
394+
# CSS class for the month
395+
cssclass_month = "month"
396+
397+
# CSS class for the year's table head
398+
cssclass_year_head = "year"
399+
400+
# CSS class for the whole year table
401+
cssclass_year = "year"
402+
385403
def formatday(self, day, weekday):
386404
"""
387405
Return a day as a table cell.
388406
"""
389407
if day == 0:
390-
return '<td class="noday">&nbsp;</td>' # day outside month
408+
# day outside month
409+
return '<td class="%s">&nbsp;</td>' % self.cssclass_noday
391410
else:
392411
return '<td class="%s">%d</td>' % (self.cssclasses[weekday], day)
393412

@@ -402,7 +421,8 @@ def formatweekday(self, day):
402421
"""
403422
Return a weekday name as a table header.
404423
"""
405-
return '<th class="%s">%s</th>' % (self.cssclasses[day], day_abbr[day])
424+
return '<th class="%s">%s</th>' % (
425+
self.cssclasses_weekday_head[day], day_abbr[day])
406426

407427
def formatweekheader(self):
408428
"""
@@ -419,15 +439,17 @@ def formatmonthname(self, theyear, themonth, withyear=True):
419439
s = '%s %s' % (month_name[themonth], theyear)
420440
else:
421441
s = '%s' % month_name[themonth]
422-
return '<tr><th colspan="7" class="month">%s</th></tr>' % s
442+
return '<tr><th colspan="7" class="%s">%s</th></tr>' % (
443+
self.cssclass_month_head, s)
423444

424445
def formatmonth(self, theyear, themonth, withyear=True):
425446
"""
426447
Return a formatted month as a table.
427448
"""
428449
v = []
429450
a = v.append
430-
a('<table border="0" cellpadding="0" cellspacing="0" class="month">')
451+
a('<table border="0" cellpadding="0" cellspacing="0" class="%s">' % (
452+
self.cssclass_month))
431453
a('\n')
432454
a(self.formatmonthname(theyear, themonth, withyear=withyear))
433455
a('\n')
@@ -447,9 +469,11 @@ def formatyear(self, theyear, width=3):
447469
v = []
448470
a = v.append
449471
width = max(width, 1)
450-
a('<table border="0" cellpadding="0" cellspacing="0" class="year">')
472+
a('<table border="0" cellpadding="0" cellspacing="0" class="%s">' %
473+
self.cssclass_year)
451474
a('\n')
452-
a('<tr><th colspan="%d" class="year">%s</th></tr>' % (width, theyear))
475+
a('<tr><th colspan="%d" class="%s">%s</th></tr>' % (
476+
width, self.cssclass_year_head, theyear))
453477
for i in range(January, January+12, width):
454478
# months in this row
455479
months = range(i, min(i+width, 13))

0 commit comments

Comments
 (0)