Kaydet (Commit) 8b7a4cc4 authored tarafından Oz N Tiram's avatar Oz N Tiram Kaydeden (comit) Walter Dörwald

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).
üst 167e0fc2
......@@ -147,7 +147,7 @@ it's the base calendar for all computations.
This class can be used to generate HTML calendars.
:class:`HTMLCalendar` instances have the following methods:
:class:`!HTMLCalendar` instances have the following methods:
.. method:: formatmonth(theyear, themonth, withyear=True)
......@@ -171,6 +171,85 @@ it's the base calendar for all computations.
output (defaulting to the system default encoding).
:class:`!HTMLCalendar` has the following attributes you can override to
customize the CSS classes used by the calendar:
.. attribute:: cssclasses
A list of CSS classes used for each weekday. The default class list is::
cssclasses = ["mon", "tue", "wed", "thu", "fri", "sat", "sun"]
more styles can be added for each day::
cssclasses = ["mon text-bold", "tue", "wed", "thu", "fri", "sat", "sun red"]
Note that the length of this list must be seven items.
.. attribute:: cssclass_noday
The CSS class for a weekday occurring in the previous or coming month.
.. versionadded:: 3.7
.. attribute:: cssclasses_weekday_head
A list of CSS classes used for weekday names in the header row.
The default is the same as :attr:`cssclasses`.
.. versionadded:: 3.7
.. attribute:: cssclass_month_head
The month's head CSS class (used by :meth:`formatmonthname`).
The default value is ``"month"``.
.. versionadded:: 3.7
.. attribute:: cssclass_month
The CSS class for the whole month's table (used by :meth:`formatmonth`).
The default value is ``"month"``.
.. versionadded:: 3.7
.. attribute:: cssclass_year
The CSS class for the whole year's table of tables (used by
:meth:`formatyear`). The default value is ``"year"``.
.. versionadded:: 3.7
.. attribute:: cssclass_year_head
The CSS class for the table head for the whole year (used by
:meth:`formatyear`). The default value is ``"year"``.
.. versionadded:: 3.7
Note that although the naming for the above described class attributes is
singular (e.g. ``cssclass_month`` ``cssclass_noday``), one can replace the
single CSS class with a space separated list of CSS classes, for example::
"text-bold text-red"
Here is an example how :class:`!HTMLCalendar` can be customized::
class CustomHTMLCal(calendar.HTMLCalendar):
cssclasses = [style + " text-nowrap" for style in
calendar.HTMLCalendar.cssclasses]
cssclass_month_head = "text-center month-head"
cssclass_month = "text-center month"
cssclass_year = "text-italic lead"
.. class:: LocaleTextCalendar(firstweekday=0, locale=None)
This subclass of :class:`TextCalendar` can be passed a locale name in the
......
......@@ -103,13 +103,6 @@ New Modules
Improved Modules
================
cgi
---
:func:`~cgi.parse_multipart` returns the same results as
:class:`~FieldStorage` : for non-file fields, the value associated to a key
is a list of strings, not bytes.
(Contributed by Pierre Quentel in :issue:`29979`.)
binascii
--------
......@@ -118,6 +111,22 @@ The :func:`~binascii.b2a_uu` function now accepts an optional *backtick*
keyword argument. When it's true, zeros are represented by ``'`'``
instead of spaces. (Contributed by Xiang Zhang in :issue:`30103`.)
calendar
--------
The :class:`~calendar.HTMLCalendar` has added new class attribute which ease the
customisation the CSS classes in the produced HTML calendar.
(Contributed by Oz Tiram in :issue:`30095`.)
cgi
---
:func:`~cgi.parse_multipart` returns the same results as
:class:`~FieldStorage` : for non-file fields, the value associated to a key
is a list of strings, not bytes.
(Contributed by Pierre Quentel in :issue:`29979`.)
contextlib
----------
......
......@@ -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">&nbsp;</td>' # day outside month
# day outside month
return '<td class="%s">&nbsp;</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))
......
This diff is collapsed.
......@@ -1556,6 +1556,7 @@ Jeremy Thurgood
Eric Tiedemann
July Tikhonov
Tracy Tims
Oz Tiram
Oren Tirosh
Tim Tisdall
Jason Tishler
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment