Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
3be1afa
Add more styling options to HTMLCalendar
oz123 Apr 23, 2017
f2609fb
Consitent naming
oz123 Apr 24, 2017
fe05aac
Add test case for subclassing
oz123 May 1, 2017
363e373
Add documentation for customizing HTMLCalendar
oz123 May 3, 2017
4ae2a28
Documentation updates
oz123 May 8, 2017
28cfb91
Correct spelling and fix styling
oz123 May 10, 2017
02ebee9
Format white space to comply with the style guide
oz123 May 10, 2017
0cffae6
Rewording and addition of sphinx directives
oz123 May 11, 2017
4752eb6
Refactor Styles -> CSS Classes
May 22, 2017
d444708
Rename class atrribute again and fix test
oz123 May 30, 2017
075d86c
Fix wrong usage of CSS classes
Jun 1, 2017
fd2bda3
Add documation strings for the HTMLCalendar class attributes
Jun 1, 2017
1c63cfa
Mention in which version class attributes were added
Jun 1, 2017
e4b95a0
Fix failing test
Jun 1, 2017
2efe925
Update NEWS and ACKS
Jun 1, 2017
712cf53
Merge upstream changes for what is new in 3.7
Jun 1, 2017
c54116e
Fix versionadded directives
oz123 Jun 1, 2017
45d68c2
pep8 white space fixes
oz123 Jun 1, 2017
29a4661
Merge branch 'master' into bpo-30095
oz123 Jun 1, 2017
6571d3f
Move HTMLCalendar example into class description block.
doerwalter Jun 1, 2017
496c194
Whitespace.
doerwalter Jun 2, 2017
2eae99d
Update variable name that contains a list of CSS classes to be plural.
doerwalter Jun 2, 2017
7ffa9cc
Avoid link to the start of the class description.
doerwalter Jun 2, 2017
f0f6100
In the attribute description refer to the method that uses the attrib…
doerwalter Jun 2, 2017
3b4529d
Complete all tests for HTMLCalendar class attributes
oz123 Jun 5, 2017
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 80 additions & 1 deletion Doc/library/calendar.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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
Expand Down
23 changes: 16 additions & 7 deletions Doc/whatsnew/3.7.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
--------
Expand All @@ -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
----------

Expand Down
36 changes: 30 additions & 6 deletions Lib/calendar.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -402,7 +421,8 @@ def formatweekday(self, day):
"""
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):
"""
Expand All @@ -419,15 +439,17 @@ def formatmonthname(self, theyear, themonth, withyear=True):
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):
"""
Return a formatted month as a table.
"""
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')
Expand All @@ -447,9 +469,11 @@ def formatyear(self, theyear, width=3):
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))
Expand Down
Loading