bpo-30095: Customizing HTMLCalendar by oz123 · Pull Request #1439 · python/cpython (original) (raw)

oz123

as discussed here http://bugs.python.org/issue30095

Unfortunately, this whole module isn't pep8 compatible, I didn't touch where I am not supposed to touch, so pep8 won't passed here.

@oz123

The HTMLCalendar has now multiple class attributes for styling the calendar. In order to retain backwards compatibility style names are not changed.

The new attributes are:

weekday_head_styles month_head_styles = ("month",) year_head_styles = ("year",) year_styles = ("year",)

This are added to the previously existing styles, which where scattered around the code hard coded or as class attributes:

month style day styles noday style

The relevant methods have been modified to use these attributes. If you want a calendar where elements has multiple CSS attributes you can now do the following:

my_styles = HTMLCalendar.cssclasses[:] my_stelys = " ".join((s, "black text-centered")) for s in my_styles]

class MyCalendar(HTMLCalendar): cssclasses = styles month_head_styles = "month-title text-bold"

This implementation simply replaces hard coded string with class attributes (which are also single strings albite the attribute names which are in plural).

@oz123

@oz123

@oz123

@mention-bot

@oz123

Fix typos, and more class attributes documentation

@oz123

@doerwalter, I updated the documentation. I would be happy to hear your comments now.

csabella

@@ -170,6 +170,67 @@ it's the base calendar for all computations.
sheet should be used. *encoding* specifies the encoding to be used for the
output (defaulting to the system default encoding).
:class:`HTMLCalendar` has the following attribute you can override to customize the style of your calender:

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

attribute -> attributes
Try not to use 'you' or 'your'.

csabella

.. attribute:: cssclasses
Is a list of CSS styles used for each weekday. The default style list is::

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure this should start with 'is'. The calendar attributes on this page all start with a subject, such as 'An array'.

csabella

cssclasses = ["mon text-bold", "tue", "wed", "thu", "fri", "sat", "sun red"]
Note, that the length of this list must be 7 items.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this needs a comma after Note.

csabella

.. attribute:: noday_classes
The CSS class for a week day occuring in previous or the coming month.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

csabella

Is a list of CSS styles used for each weekday. The default style list is
the same as ``HTMLCalendar.cssclassess``.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

csabella

.. attribute:: month_head_classes
A space separted list of styles for the month head, for example::

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

separted -> separated

csabella

.. attribute:: month_classes
a space separted list of styles for the whole table.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

csabella

.. attribute:: year_classes
a space separted list of styles for the table when formatting the year
as a table of tables (see: ``HTMLCalendar.formatyear``).

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

csabella

.. attribute:: year_head_classes
a space separted list of styles for the table head when formatting the year as a table of tables (see: ``HTMLCalendar.formatyear``).

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@csabella

When I looked at the html page for the docs, the indentation was off compared to the section before your changes (I used 'make html' to be able to view it). I'm also wondering about the name of 'cssclasses'. Since the year and month ones have year and month in the name, maybe this should too?

@oz123

@csabella Thanks for the thorough review. I am embarrassed there are so many spelling errors...

I would prefer to rename the cssclasses attribute to weekday_classes, but I was afraid somebody is already using it with that name.
If there is no objection to that, I would rename that attribute. I will correct the spelling errors as soon as possible.

@csabella

@oz123 Please don't be embarrassed about it. I probably should have just asked you to spellcheck it instead of commenting on each one. Sorry about that.

As far as the attribute name, maybe @doerwalter could help resolve that. I just found it inconsistent when reading the doc, so I wanted to ask about it.

@oz123

@oz123

@doerwalter what do you think about renaming the attribute HTMLCalendar.cssclassesto HTMLCalendar.weekday_styles`?

@csabella

@oz123 I looked at the html again and still had the issue with the odd indentation. Looking at the source, I noticed you used 4 spaces (or tab) to indent. The docs use 3 spaces (and no tabs) as the standard with an 80 character limit.

Here's the reference in the dev guide for formatting the docs:
https://docs.python.org/devguide/documenting.html#style-guide

@oz123

@csabella fixed the white space. Can you check now?

@oz123

csabella

@@ -170,6 +170,64 @@ it's the base calendar for all computations.
sheet should be used. *encoding* specifies the encoding to be used for the
output (defaulting to the system default encoding).
:class:`HTMLCalendar` has the following attributes you can override to
customize the style of your calender:

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

calender -> calendar

csabella

cssclasses = ["mon text-bold", "tue", "wed", "thu", "fri", "sat", "sun red"]
Note that the length of this list must be 7 items.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Other places in this page use 'seven' instead of '7'.

csabella

.. attribute:: noday_classes
The CSS class for a week day occurring in previous or the coming month.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@doerwalter

Also you might want to add an appropriate description of the new features to Doc/whatsnew/3.7.rst.

Oz N Tiram added 5 commits

June 1, 2017 06:34

@oz123

@doerwalter I added the changes you suggested.
I really hope these additions to HTMLCalendar will be useful for others too.
Thank you for all the support and suggestions until now.

doerwalter

@@ -185,37 +185,42 @@ it's the base calendar for all computations.
Note that the length of this list must be seven items.
.. versionadded:: 3.7

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The .. versionadded:: 3.7 should go inside the .. attribute:: block (after the description), i.e.

    .. attribute:: cssclass_noday
  
        The CSS class for a weekday occurring in the previous or coming month.

        .. versionadded:: 3.7

Note that you can create the documentation yourself to check the result via make html in the Doc directory (you need Sphinx for that)

doerwalter

.. attribute:: cssclass_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. The default value is ``"month"``.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could add a reference to formatmonthname here, i.e.:

The month's head CSS class (see :meth:`formatmonthname`). The default value is ``"month"``.

doerwalter

.. attribute:: cssclass_month_head
The month's head CSS class. The default value is ``"month"``.
.. versionadded:: 3.7
.. attribute:: cssclass_month
The CSS class for the whole month's table. The default value is ``"month"``.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could add a reference to formatmonth here, i.e.:

The CSS class for the whole month's table (see :meth:`formatmonth`). The default value is ``"month"``.

doerwalter

customisation the CSS classes in the produced HTML calendar.
(Contributed by Oz Tiram in :issue:`30095`.)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMHO this should be something like this:

calendar
--------

CSS classes used by :class:`~calendar.HTMLCalendar` can now be customized by
subclassing :class:`~calendar.HTMLCalendar` and overwriting certain class
attributes. (Contributed by Oz Tiram in :issue:`30095`.)

doerwalter

def test_formatweek(self):
weeks = self.cal.monthdays2calendar(2017,5)
self.assertIn('class="wed text-nowrap"', self.cal.formatweek(weeks[0]))
def test_format_year(self):

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's a newline missing here.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@doerwalter

I pushed a small documentation fix to your branch.

@oz123

@doerwalter WOW! I didn't know you can push to a pull request! That is cool.

@doerwalter

@doerwalter

@doerwalter

@doerwalter

I've renamed cssclass_weekday_head to cssclasses_weekday_head, since this is a list of names.

I've noticed that there's no test that checks for this configuration. Could you add one? And if there are other tests missing add those too.

@oz123

@doerwalter I think the change you introduce is good. I also added tests to all the class attributes. Hopefully, this is the last iteration.

@doerwalter

OK, your patch is in.

Congratulations and thanks for your contribution to Python.