diff --git a/Doc/whatsnew/3.15.rst b/Doc/whatsnew/3.15.rst index 9f01b52f1aff3b..79a99fd126b4e0 100644 --- a/Doc/whatsnew/3.15.rst +++ b/Doc/whatsnew/3.15.rst @@ -214,6 +214,13 @@ New modules Improved modules ================ +calendar +-------- + +* The calendar pages generated by the :class:`calendar.HTMLCalendar` class now + use the HTML5 standard. + (Contributed by Jiahao Li in :gh:`137634`.) + dbm --- diff --git a/Lib/calendar.py b/Lib/calendar.py index 45bb265a65602c..65e322a70dd4d1 100644 --- a/Lib/calendar.py +++ b/Lib/calendar.py @@ -490,30 +490,29 @@ def formatday(self, day, weekday): """ if day == 0: # day outside month - return ' ' % self.cssclass_noday + return f' ' else: - return '%d' % (self.cssclasses[weekday], day) + return f'{day}' def formatweek(self, theweek): """ Return a complete week as a table row. """ s = ''.join(self.formatday(d, wd) for (d, wd) in theweek) - return '%s' % s + return f'{s}' def formatweekday(self, day): """ Return a weekday name as a table header. """ - return '%s' % ( - self.cssclasses_weekday_head[day], day_abbr[day]) + return f'{day_abbr[day]}' def formatweekheader(self): """ Return a header for a week as a table row. """ s = ''.join(self.formatweekday(i) for i in self.iterweekdays()) - return '%s' % s + return f'{s}' def formatmonthname(self, theyear, themonth, withyear=True): """ @@ -521,11 +520,10 @@ def formatmonthname(self, theyear, themonth, withyear=True): """ _validate_month(themonth) if withyear: - s = '%s %s' % (standalone_month_name[themonth], theyear) + s = f'{standalone_month_name[themonth]} {theyear}' else: s = standalone_month_name[themonth] - return '%s' % ( - self.cssclass_month_head, s) + return f'{s}' def formatmonth(self, theyear, themonth, withyear=True): """ @@ -533,8 +531,7 @@ def formatmonth(self, theyear, themonth, withyear=True): """ v = [] a = v.append - a('' % ( - self.cssclass_month)) + a(f'
') a('\n') a(self.formatmonthname(theyear, themonth, withyear=withyear)) a('\n') @@ -554,11 +551,9 @@ def formatyear(self, theyear, width=3): v = [] a = v.append width = max(width, 1) - a('
' % - self.cssclass_year) + a(f'
') a('\n') - a('' % ( - width, self.cssclass_year_head, theyear)) + a(f'') for i in range(JANUARY, JANUARY+12, width): # months in this row months = range(i, min(i+width, 13)) @@ -579,14 +574,14 @@ def formatyearpage(self, theyear, width=3, css='calendar.css', encoding=None): encoding = 'utf-8' v = [] a = v.append - a('\n' % encoding) - a('\n') - a('\n') + a('\n') + a('\n') a('\n') - a('\n' % encoding) + a(f'\n') + a('\n') + a(f'Calendar for {theyear}\n') if css is not None: - a('\n' % css) - a('Calendar for %d\n' % theyear) + a(f'\n') a('\n') a('\n') a(self.formatyear(theyear, width)) diff --git a/Lib/test/test_calendar.py b/Lib/test/test_calendar.py index 589a21baf7bd61..3f6b7922cd5641 100644 --- a/Lib/test/test_calendar.py +++ b/Lib/test/test_calendar.py @@ -114,17 +114,17 @@ default_format = dict(year="year", month="month", encoding="ascii") result_2004_html = """\ - - - + + - - + +Calendar for 2004 + -
%s
{theyear}
-
2004
+
+
2004
@@ -133,7 +133,7 @@
January
MonTueWedThuFriSatSun
   1234
19202122232425
262728293031 
-
+
@@ -142,7 +142,7 @@
February
MonTueWedThuFriSatSun
      1
16171819202122
23242526272829
-
+
@@ -151,7 +151,7 @@
March
MonTueWedThuFriSatSun
1234567
22232425262728
293031    
-
+
@@ -160,7 +160,7 @@
April
MonTueWedThuFriSatSun
   1234
19202122232425
2627282930  
-
+
@@ -170,7 +170,7 @@
May
MonTueWedThuFriSatSun
     12
24252627282930
31      
-
+
@@ -179,7 +179,7 @@
June
MonTueWedThuFriSatSun
 123456
21222324252627
282930    
-
+
@@ -188,7 +188,7 @@
July
MonTueWedThuFriSatSun
   1234
19202122232425
262728293031 
-
+
@@ -198,7 +198,7 @@
August
MonTueWedThuFriSatSun
      1
23242526272829
3031     
-
+
@@ -207,7 +207,7 @@
September
MonTueWedThuFriSatSun
  12345
20212223242526
27282930   
-
+
@@ -216,7 +216,7 @@
October
MonTueWedThuFriSatSun
    123
18192021222324
25262728293031
-
+
@@ -225,7 +225,7 @@
November
MonTueWedThuFriSatSun
1234567
22232425262728
2930     
-
+
@@ -1132,7 +1132,7 @@ def test_option_type(self): output = run('--type', 'text', '2004') self.assertEqual(output, conv(result_2004_text)) output = run('--type', 'html', '2004') - self.assertStartsWith(output, b'') self.assertIn(b'Calendar for 2004', output) def test_html_output_current_year(self): @@ -1152,8 +1152,7 @@ def test_html_output_year_css(self): self.assertFailure('-t', 'html', '--css') for run in self.runners: output = run('-t', 'html', '--css', 'custom.css', '2004') - self.assertIn(b'', output) + self.assertIn(b'', output) class MiscTestCase(unittest.TestCase): @@ -1207,7 +1206,7 @@ def test_formatweek_head(self): def test_format_year(self): self.assertIn( - ('
December
MonTueWedThuFriSatSun
  12345
' % + ('
' % self.cal.cssclass_year), self.cal.formatyear(2017)) def test_format_year_head(self): diff --git a/Misc/NEWS.d/next/Library/2025-08-11-14-18-32.gh-issue-137634.M7iBG6.rst b/Misc/NEWS.d/next/Library/2025-08-11-14-18-32.gh-issue-137634.M7iBG6.rst new file mode 100644 index 00000000000000..e07561c050cd77 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2025-08-11-14-18-32.gh-issue-137634.M7iBG6.rst @@ -0,0 +1,2 @@ +The calendar pages generated by the :class:`calendar.HTMLCalendar` class now +use the HTML5 standard.