Skip to content

Commit 7ca6d79

Browse files
Wulian233hugovk
andauthored
gh-134580: Modernizing difflib.HtmlDiff for HTML Output (#134581)
Co-authored-by: Hugo van Kemenade <1324225+hugovk@users.noreply.github.com>
1 parent a4d37f8 commit 7ca6d79

File tree

5 files changed

+84
-30
lines changed

5 files changed

+84
-30
lines changed

Doc/whatsnew/3.15.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,13 @@ New modules
8989
Improved modules
9090
================
9191

92+
difflib
93+
-------
94+
95+
* Improved the styling of HTML diff pages generated by the :class:`difflib.HtmlDiff`
96+
class, and migrated the output to the HTML5 standard.
97+
(Contributed by Jiahao Li in :gh:`134580`.)
98+
9299
ssl
93100
---
94101

Lib/difflib.py

Lines changed: 36 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1615,16 +1615,13 @@ def _line_pair_iterator():
16151615

16161616

16171617
_file_template = """
1618-
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
1619-
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
1620-
1621-
<html>
1622-
1618+
<!DOCTYPE html>
1619+
<html lang="en">
16231620
<head>
1624-
<meta http-equiv="Content-Type"
1625-
content="text/html; charset=%(charset)s" />
1626-
<title></title>
1627-
<style type="text/css">%(styles)s
1621+
<meta charset="%(charset)s">
1622+
<meta name="viewport" content="width=device-width, initial-scale=1">
1623+
<title>Diff comparison</title>
1624+
<style>%(styles)s
16281625
</style>
16291626
</head>
16301627
@@ -1636,20 +1633,45 @@ def _line_pair_iterator():
16361633

16371634
_styles = """
16381635
:root {color-scheme: light dark}
1639-
table.diff {font-family: Menlo, Consolas, Monaco, Liberation Mono, Lucida Console, monospace; border:medium}
1640-
.diff_header {background-color:#e0e0e0}
1641-
td.diff_header {text-align:right}
1642-
.diff_next {background-color:#c0c0c0}
1636+
table.diff {
1637+
font-family: Menlo, Consolas, Monaco, Liberation Mono, Lucida Console, monospace;
1638+
border: medium;
1639+
}
1640+
.diff_header {
1641+
background-color: #e0e0e0;
1642+
font-weight: bold;
1643+
}
1644+
td.diff_header {
1645+
text-align: right;
1646+
padding: 0 8px;
1647+
}
1648+
.diff_next {
1649+
background-color: #c0c0c0;
1650+
padding: 4px 0;
1651+
}
16431652
.diff_add {background-color:palegreen}
16441653
.diff_chg {background-color:#ffff77}
16451654
.diff_sub {background-color:#ffaaaa}
1655+
table.diff[summary="Legends"] {
1656+
margin-top: 20px;
1657+
border: 1px solid #ccc;
1658+
}
1659+
table.diff[summary="Legends"] th {
1660+
background-color: #e0e0e0;
1661+
padding: 4px 8px;
1662+
}
1663+
table.diff[summary="Legends"] td {
1664+
padding: 4px 8px;
1665+
}
16461666
16471667
@media (prefers-color-scheme: dark) {
16481668
.diff_header {background-color:#666}
16491669
.diff_next {background-color:#393939}
16501670
.diff_add {background-color:darkgreen}
16511671
.diff_chg {background-color:#847415}
16521672
.diff_sub {background-color:darkred}
1673+
table.diff[summary="Legends"] {border-color:#555}
1674+
table.diff[summary="Legends"] th{background-color:#666}
16531675
}"""
16541676

16551677
_table_template = """
@@ -1692,7 +1714,7 @@ class HtmlDiff(object):
16921714
make_table -- generates HTML for a single side by side table
16931715
make_file -- generates complete HTML file with a single side by side table
16941716
1695-
See tools/scripts/diff.py for an example usage of this class.
1717+
See Doc/includes/diff.py for an example usage of this class.
16961718
"""
16971719

16981720
_file_template = _file_template

Lib/test/test_difflib.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -255,21 +255,21 @@ def test_make_file_default_charset(self):
255255
html_diff = difflib.HtmlDiff()
256256
output = html_diff.make_file(patch914575_from1.splitlines(),
257257
patch914575_to1.splitlines())
258-
self.assertIn('content="text/html; charset=utf-8"', output)
258+
self.assertIn('charset="utf-8"', output)
259259

260260
def test_make_file_iso88591_charset(self):
261261
html_diff = difflib.HtmlDiff()
262262
output = html_diff.make_file(patch914575_from1.splitlines(),
263263
patch914575_to1.splitlines(),
264264
charset='iso-8859-1')
265-
self.assertIn('content="text/html; charset=iso-8859-1"', output)
265+
self.assertIn('charset="iso-8859-1"', output)
266266

267267
def test_make_file_usascii_charset_with_nonascii_input(self):
268268
html_diff = difflib.HtmlDiff()
269269
output = html_diff.make_file(patch914575_nonascii_from1.splitlines(),
270270
patch914575_nonascii_to1.splitlines(),
271271
charset='us-ascii')
272-
self.assertIn('content="text/html; charset=us-ascii"', output)
272+
self.assertIn('charset="us-ascii"', output)
273273
self.assertIn('&#305;mpl&#305;c&#305;t', output)
274274

275275
class TestDiffer(unittest.TestCase):

Lib/test/test_difflib_expect.html

Lines changed: 35 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,51 @@
11

2-
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
3-
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
4-
5-
<html>
6-
2+
<!DOCTYPE html>
3+
<html lang="en">
74
<head>
8-
<meta http-equiv="Content-Type"
9-
content="text/html; charset=utf-8" />
10-
<title></title>
11-
<style type="text/css">
5+
<meta charset="utf-8">
6+
<meta name="viewport" content="width=device-width, initial-scale=1">
7+
<title>Diff comparison</title>
8+
<style>
129
:root {color-scheme: light dark}
13-
table.diff {font-family: Menlo, Consolas, Monaco, Liberation Mono, Lucida Console, monospace; border:medium}
14-
.diff_header {background-color:#e0e0e0}
15-
td.diff_header {text-align:right}
16-
.diff_next {background-color:#c0c0c0}
10+
table.diff {
11+
font-family: Menlo, Consolas, Monaco, Liberation Mono, Lucida Console, monospace;
12+
border: medium;
13+
}
14+
.diff_header {
15+
background-color: #e0e0e0;
16+
font-weight: bold;
17+
}
18+
td.diff_header {
19+
text-align: right;
20+
padding: 0 8px;
21+
}
22+
.diff_next {
23+
background-color: #c0c0c0;
24+
padding: 4px 0;
25+
}
1726
.diff_add {background-color:palegreen}
1827
.diff_chg {background-color:#ffff77}
1928
.diff_sub {background-color:#ffaaaa}
29+
table.diff[summary="Legends"] {
30+
margin-top: 20px;
31+
border: 1px solid #ccc;
32+
}
33+
table.diff[summary="Legends"] th {
34+
background-color: #e0e0e0;
35+
padding: 4px 8px;
36+
}
37+
table.diff[summary="Legends"] td {
38+
padding: 4px 8px;
39+
}
2040

2141
@media (prefers-color-scheme: dark) {
2242
.diff_header {background-color:#666}
2343
.diff_next {background-color:#393939}
2444
.diff_add {background-color:darkgreen}
2545
.diff_chg {background-color:#847415}
2646
.diff_sub {background-color:darkred}
47+
table.diff[summary="Legends"] {border-color:#555}
48+
table.diff[summary="Legends"] th{background-color:#666}
2749
}
2850
</style>
2951
</head>
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Improved the styling of HTML diff pages generated by the
2+
:class:`difflib.HtmlDiff` class, and migrated the output to the HTML5
3+
standard.

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy