mirror of
https://github.com/python/cpython.git
synced 2025-12-08 06:10:17 +00:00
gh-134580: Modernizing difflib.HtmlDiff for HTML Output (#134581)
Co-authored-by: Hugo van Kemenade <1324225+hugovk@users.noreply.github.com>
This commit is contained in:
parent
a4d37f88b6
commit
7ca6d79fa3
5 changed files with 84 additions and 30 deletions
|
|
@ -89,6 +89,13 @@ New modules
|
||||||
Improved modules
|
Improved modules
|
||||||
================
|
================
|
||||||
|
|
||||||
|
difflib
|
||||||
|
-------
|
||||||
|
|
||||||
|
* Improved the styling of HTML diff pages generated by the :class:`difflib.HtmlDiff`
|
||||||
|
class, and migrated the output to the HTML5 standard.
|
||||||
|
(Contributed by Jiahao Li in :gh:`134580`.)
|
||||||
|
|
||||||
ssl
|
ssl
|
||||||
---
|
---
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1615,16 +1615,13 @@ def _line_pair_iterator():
|
||||||
|
|
||||||
|
|
||||||
_file_template = """
|
_file_template = """
|
||||||
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
|
<!DOCTYPE html>
|
||||||
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
|
<html lang="en">
|
||||||
|
|
||||||
<html>
|
|
||||||
|
|
||||||
<head>
|
<head>
|
||||||
<meta http-equiv="Content-Type"
|
<meta charset="%(charset)s">
|
||||||
content="text/html; charset=%(charset)s" />
|
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||||||
<title></title>
|
<title>Diff comparison</title>
|
||||||
<style type="text/css">%(styles)s
|
<style>%(styles)s
|
||||||
</style>
|
</style>
|
||||||
</head>
|
</head>
|
||||||
|
|
||||||
|
|
@ -1636,13 +1633,36 @@ def _line_pair_iterator():
|
||||||
|
|
||||||
_styles = """
|
_styles = """
|
||||||
:root {color-scheme: light dark}
|
:root {color-scheme: light dark}
|
||||||
table.diff {font-family: Menlo, Consolas, Monaco, Liberation Mono, Lucida Console, monospace; border:medium}
|
table.diff {
|
||||||
.diff_header {background-color:#e0e0e0}
|
font-family: Menlo, Consolas, Monaco, Liberation Mono, Lucida Console, monospace;
|
||||||
td.diff_header {text-align:right}
|
border: medium;
|
||||||
.diff_next {background-color:#c0c0c0}
|
}
|
||||||
|
.diff_header {
|
||||||
|
background-color: #e0e0e0;
|
||||||
|
font-weight: bold;
|
||||||
|
}
|
||||||
|
td.diff_header {
|
||||||
|
text-align: right;
|
||||||
|
padding: 0 8px;
|
||||||
|
}
|
||||||
|
.diff_next {
|
||||||
|
background-color: #c0c0c0;
|
||||||
|
padding: 4px 0;
|
||||||
|
}
|
||||||
.diff_add {background-color:palegreen}
|
.diff_add {background-color:palegreen}
|
||||||
.diff_chg {background-color:#ffff77}
|
.diff_chg {background-color:#ffff77}
|
||||||
.diff_sub {background-color:#ffaaaa}
|
.diff_sub {background-color:#ffaaaa}
|
||||||
|
table.diff[summary="Legends"] {
|
||||||
|
margin-top: 20px;
|
||||||
|
border: 1px solid #ccc;
|
||||||
|
}
|
||||||
|
table.diff[summary="Legends"] th {
|
||||||
|
background-color: #e0e0e0;
|
||||||
|
padding: 4px 8px;
|
||||||
|
}
|
||||||
|
table.diff[summary="Legends"] td {
|
||||||
|
padding: 4px 8px;
|
||||||
|
}
|
||||||
|
|
||||||
@media (prefers-color-scheme: dark) {
|
@media (prefers-color-scheme: dark) {
|
||||||
.diff_header {background-color:#666}
|
.diff_header {background-color:#666}
|
||||||
|
|
@ -1650,6 +1670,8 @@ def _line_pair_iterator():
|
||||||
.diff_add {background-color:darkgreen}
|
.diff_add {background-color:darkgreen}
|
||||||
.diff_chg {background-color:#847415}
|
.diff_chg {background-color:#847415}
|
||||||
.diff_sub {background-color:darkred}
|
.diff_sub {background-color:darkred}
|
||||||
|
table.diff[summary="Legends"] {border-color:#555}
|
||||||
|
table.diff[summary="Legends"] th{background-color:#666}
|
||||||
}"""
|
}"""
|
||||||
|
|
||||||
_table_template = """
|
_table_template = """
|
||||||
|
|
@ -1692,7 +1714,7 @@ class HtmlDiff(object):
|
||||||
make_table -- generates HTML for a single side by side table
|
make_table -- generates HTML for a single side by side table
|
||||||
make_file -- generates complete HTML file with a single side by side table
|
make_file -- generates complete HTML file with a single side by side table
|
||||||
|
|
||||||
See tools/scripts/diff.py for an example usage of this class.
|
See Doc/includes/diff.py for an example usage of this class.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
_file_template = _file_template
|
_file_template = _file_template
|
||||||
|
|
|
||||||
|
|
@ -255,21 +255,21 @@ def test_make_file_default_charset(self):
|
||||||
html_diff = difflib.HtmlDiff()
|
html_diff = difflib.HtmlDiff()
|
||||||
output = html_diff.make_file(patch914575_from1.splitlines(),
|
output = html_diff.make_file(patch914575_from1.splitlines(),
|
||||||
patch914575_to1.splitlines())
|
patch914575_to1.splitlines())
|
||||||
self.assertIn('content="text/html; charset=utf-8"', output)
|
self.assertIn('charset="utf-8"', output)
|
||||||
|
|
||||||
def test_make_file_iso88591_charset(self):
|
def test_make_file_iso88591_charset(self):
|
||||||
html_diff = difflib.HtmlDiff()
|
html_diff = difflib.HtmlDiff()
|
||||||
output = html_diff.make_file(patch914575_from1.splitlines(),
|
output = html_diff.make_file(patch914575_from1.splitlines(),
|
||||||
patch914575_to1.splitlines(),
|
patch914575_to1.splitlines(),
|
||||||
charset='iso-8859-1')
|
charset='iso-8859-1')
|
||||||
self.assertIn('content="text/html; charset=iso-8859-1"', output)
|
self.assertIn('charset="iso-8859-1"', output)
|
||||||
|
|
||||||
def test_make_file_usascii_charset_with_nonascii_input(self):
|
def test_make_file_usascii_charset_with_nonascii_input(self):
|
||||||
html_diff = difflib.HtmlDiff()
|
html_diff = difflib.HtmlDiff()
|
||||||
output = html_diff.make_file(patch914575_nonascii_from1.splitlines(),
|
output = html_diff.make_file(patch914575_nonascii_from1.splitlines(),
|
||||||
patch914575_nonascii_to1.splitlines(),
|
patch914575_nonascii_to1.splitlines(),
|
||||||
charset='us-ascii')
|
charset='us-ascii')
|
||||||
self.assertIn('content="text/html; charset=us-ascii"', output)
|
self.assertIn('charset="us-ascii"', output)
|
||||||
self.assertIn('ımplıcıt', output)
|
self.assertIn('ımplıcıt', output)
|
||||||
|
|
||||||
class TestDiffer(unittest.TestCase):
|
class TestDiffer(unittest.TestCase):
|
||||||
|
|
|
||||||
|
|
@ -1,22 +1,42 @@
|
||||||
|
|
||||||
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
|
<!DOCTYPE html>
|
||||||
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
|
<html lang="en">
|
||||||
|
|
||||||
<html>
|
|
||||||
|
|
||||||
<head>
|
<head>
|
||||||
<meta http-equiv="Content-Type"
|
<meta charset="utf-8">
|
||||||
content="text/html; charset=utf-8" />
|
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||||||
<title></title>
|
<title>Diff comparison</title>
|
||||||
<style type="text/css">
|
<style>
|
||||||
:root {color-scheme: light dark}
|
:root {color-scheme: light dark}
|
||||||
table.diff {font-family: Menlo, Consolas, Monaco, Liberation Mono, Lucida Console, monospace; border:medium}
|
table.diff {
|
||||||
.diff_header {background-color:#e0e0e0}
|
font-family: Menlo, Consolas, Monaco, Liberation Mono, Lucida Console, monospace;
|
||||||
td.diff_header {text-align:right}
|
border: medium;
|
||||||
.diff_next {background-color:#c0c0c0}
|
}
|
||||||
|
.diff_header {
|
||||||
|
background-color: #e0e0e0;
|
||||||
|
font-weight: bold;
|
||||||
|
}
|
||||||
|
td.diff_header {
|
||||||
|
text-align: right;
|
||||||
|
padding: 0 8px;
|
||||||
|
}
|
||||||
|
.diff_next {
|
||||||
|
background-color: #c0c0c0;
|
||||||
|
padding: 4px 0;
|
||||||
|
}
|
||||||
.diff_add {background-color:palegreen}
|
.diff_add {background-color:palegreen}
|
||||||
.diff_chg {background-color:#ffff77}
|
.diff_chg {background-color:#ffff77}
|
||||||
.diff_sub {background-color:#ffaaaa}
|
.diff_sub {background-color:#ffaaaa}
|
||||||
|
table.diff[summary="Legends"] {
|
||||||
|
margin-top: 20px;
|
||||||
|
border: 1px solid #ccc;
|
||||||
|
}
|
||||||
|
table.diff[summary="Legends"] th {
|
||||||
|
background-color: #e0e0e0;
|
||||||
|
padding: 4px 8px;
|
||||||
|
}
|
||||||
|
table.diff[summary="Legends"] td {
|
||||||
|
padding: 4px 8px;
|
||||||
|
}
|
||||||
|
|
||||||
@media (prefers-color-scheme: dark) {
|
@media (prefers-color-scheme: dark) {
|
||||||
.diff_header {background-color:#666}
|
.diff_header {background-color:#666}
|
||||||
|
|
@ -24,6 +44,8 @@
|
||||||
.diff_add {background-color:darkgreen}
|
.diff_add {background-color:darkgreen}
|
||||||
.diff_chg {background-color:#847415}
|
.diff_chg {background-color:#847415}
|
||||||
.diff_sub {background-color:darkred}
|
.diff_sub {background-color:darkred}
|
||||||
|
table.diff[summary="Legends"] {border-color:#555}
|
||||||
|
table.diff[summary="Legends"] th{background-color:#666}
|
||||||
}
|
}
|
||||||
</style>
|
</style>
|
||||||
</head>
|
</head>
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,3 @@
|
||||||
|
Improved the styling of HTML diff pages generated by the
|
||||||
|
:class:`difflib.HtmlDiff` class, and migrated the output to the HTML5
|
||||||
|
standard.
|
||||||
Loading…
Add table
Add a link
Reference in a new issue