Kaydet (Commit) e064b41f authored tarafından Martin v. Löwis's avatar Martin v. Löwis

Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.

üst 87fa785f
......@@ -52,6 +52,79 @@
characters.
\end{classdesc*}
\begin{classdesc*}{HtmlDiff}
This class can be used to create an HTML table (or a complete HTML file
containing the table) showing a side by side, line by line comparision
of text with inter-line and intra-line change highlights. The table can
be generated in either full or contextual difference mode.
The constructor for this class is:
\begin{funcdesc}{__init__}{
\optional{, tabsize
\optional{, wrapcolumn
\optional{, linejunk
\optional{, charjunk}}}}}
Initializes instance of \class{HtmlDiff}.
\var{tabsize} is an optional keyword argument to specify tab stop spacing
and defaults to \code{8}.
\var{wrapcolumn} is an optional keyword to specify column number where
lines are broken and wrapped, defaults to \code{None} where lines are not
wrapped.
\var{linejunk} and \var{charjunk} are optional keyword arguments passed
into \code{ndiff()} (used to by \class{HtmlDiff} to generate the
side by side HTML differences). See \code{ndiff()} documentation for
argument default values and descriptions.
\end{funcdesc}
The following methods are public:
\begin{funcdesc}{make_file}{fromlines, tolines
\optional{, fromdesc
\optional{, todesc
\optional{, context
\optional{, numlines}}}}}
Compares \var{fromlines} and \var{tolines} (lists of strings) and returns
a string which is a complete HTML file containing a table showing line by
line differences with inter-line and intra-line changes highlighted.
\var{fromdesc} and \var{todesc} are optional keyword arguments to specify
from/to file column header strings (both default to an empty string).
\var{context} and \var{numlines} are both optional keyword arguments.
Set \var{context} to \code{True} when contextual differences are to be
shown, else the default is \code{False} to show the full files.
\var{numlines} defaults to \code{5}. When \var{context} is \code{True}
\var{numlines} controls the number of context lines which surround the
difference highlights. When \var{context} is \code{False} \var{numlines}
controls the number of lines which are shown before a difference
highlight when using the "next" hyperlinks (setting to zero would cause
the "next" hyperlinks to place the next difference highlight at the top of
the browser without any leading context).
\end{funcdesc}
\begin{funcdesc}{make_table}{fromlines, tolines
\optional{, fromdesc
\optional{, todesc
\optional{, context}}}}
Compares \var{fromlines} and \var{tolines} (lists of strings) and returns
a string which is a complete HTML table showing line by line differences
with inter-line and intra-line changes highlighted.
The arguments of this method are a subset of those for the
\code{make_file} method. Refer to the \code{make_file} method
documentation.
\end{funcdesc}
\file{Tools/scripts/ndiff.py} is a command-line front-end to this class
and contains a good example of its use.
\end{classdesc*}
\begin{funcdesc}{context_diff}{a, b\optional{, fromfile\optional{, tofile
\optional{, fromfiledate\optional{, tofiledate\optional{, n
\optional{, lineterm}}}}}}}
......
This diff is collapsed.
import difflib
from test import test_support
from test.test_support import run_unittest, findfile
import unittest
import doctest
......@@ -19,6 +19,130 @@ class TestSFbugs(unittest.TestCase):
diff_gen = difflib.unified_diff([], [])
self.assertRaises(StopIteration, diff_gen.next)
patch914575_from1 = """
1. Beautiful is beTTer than ugly.
2. Explicit is better than implicit.
3. Simple is better than complex.
4. Complex is better than complicated.
"""
patch914575_to1 = """
1. Beautiful is better than ugly.
3. Simple is better than complex.
4. Complicated is better than complex.
5. Flat is better than nested.
"""
patch914575_from2 = """
\t\tLine 1: preceeded by from:[tt] to:[ssss]
\t\tLine 2: preceeded by from:[sstt] to:[sssst]
\t \tLine 3: preceeded by from:[sstst] to:[ssssss]
Line 4: \thas from:[sst] to:[sss] after :
Line 5: has from:[t] to:[ss] at end\t
"""
patch914575_to2 = """
Line 1: preceeded by from:[tt] to:[ssss]
\tLine 2: preceeded by from:[sstt] to:[sssst]
Line 3: preceeded by from:[sstst] to:[ssssss]
Line 4: has from:[sst] to:[sss] after :
Line 5: has from:[t] to:[ss] at end
"""
patch914575_from3 = """line 0
1234567890123456789012345689012345
line 1
line 2
line 3
line 4 changed
line 5 changed
line 6 changed
line 7
line 8 subtracted
line 9
1234567890123456789012345689012345
short line
just fits in!!
just fits in two lines yup!!
the end"""
patch914575_to3 = """line 0
1234567890123456789012345689012345
line 1
line 2 added
line 3
line 4 chanGEd
line 5a chanGed
line 6a changEd
line 7
line 8
line 9
1234567890
another long line that needs to be wrapped
just fitS in!!
just fits in two lineS yup!!
the end"""
class TestSFpatches(unittest.TestCase):
def test_html_diff(self):
# Check SF patch 914575 for generating HTML differences
f1a = ((patch914575_from1 + '123\n'*10)*3)
t1a = (patch914575_to1 + '123\n'*10)*3
f1b = '456\n'*10 + f1a
t1b = '456\n'*10 + t1a
f1a = f1a.splitlines()
t1a = t1a.splitlines()
f1b = f1b.splitlines()
t1b = t1b.splitlines()
f2 = patch914575_from2.splitlines()
t2 = patch914575_to2.splitlines()
f3 = patch914575_from3
t3 = patch914575_to3
i = difflib.HtmlDiff()
j = difflib.HtmlDiff(tabsize=2)
k = difflib.HtmlDiff(wrapcolumn=14)
full = i.make_file(f1a,t1a,'from','to',context=False,numlines=5)
tables = '\n'.join(
[
'<h2>Context (first diff within numlines=5(default))</h2>',
i.make_table(f1a,t1a,'from','to',context=True),
'<h2>Context (first diff after numlines=5(default))</h2>',
i.make_table(f1b,t1b,'from','to',context=True),
'<h2>Context (numlines=6)</h2>',
i.make_table(f1a,t1a,'from','to',context=True,numlines=6),
'<h2>Context (numlines=0)</h2>',
i.make_table(f1a,t1a,'from','to',context=True,numlines=0),
'<h2>Same Context</h2>',
i.make_table(f1a,f1a,'from','to',context=True),
'<h2>Same Full</h2>',
i.make_table(f1a,f1a,'from','to',context=False),
'<h2>Empty Context</h2>',
i.make_table([],[],'from','to',context=True),
'<h2>Empty Full</h2>',
i.make_table([],[],'from','to',context=False),
'<h2>tabsize=2</h2>',
j.make_table(f2,t2),
'<h2>tabsize=default</h2>',
i.make_table(f2,t2),
'<h2>Context (wrapcolumn=14,numlines=0)</h2>',
k.make_table(f3.splitlines(),t3.splitlines(),context=True,numlines=0),
'<h2>wrapcolumn=14,splitlines()</h2>',
k.make_table(f3.splitlines(),t3.splitlines()),
'<h2>wrapcolumn=14,splitlines(True)</h2>',
k.make_table(f3.splitlines(True),t3.splitlines(True)),
])
actual = full.replace('</body>','\n%s\n</body>' % tables)
# temporarily uncomment next three lines to baseline this test
#f = open('test_difflib_expect.html','w')
#f.write(actual)
#f.close()
expect = open(findfile('test_difflib_expect.html')).read()
self.assertEqual(actual,expect)
Doctests = doctest.DocTestSuite(difflib)
test_support.run_unittest(TestSFbugs, Doctests)
run_unittest(TestSFpatches, TestSFbugs, Doctests)
This source diff could not be displayed because it is too large. You can view the blob instead.
......@@ -201,6 +201,7 @@ Raymund Galvin
Nitin Ganatra
Fred Gansevles
Lars Marius Garshol
Dan Gass
Andrew Gaul
Stephen M. Gava
Harry Henry Gebel
......
......@@ -47,6 +47,8 @@ Core and builtins
Extension modules
-----------------
- difflib now supports HTML side-by-side diff.
- os.urandom has been added for systems that support sources of random
data.
......@@ -63,6 +65,8 @@ Extension modules
Library
-------
- difflib and diff.py can now generate HTML.
- bdist_rpm now includes version and release in the BuildRoot, and
replaces - by ``_`` in version and release.
......
""" Command line interface to difflib.py providing diffs in three formats:
""" Command line interface to difflib.py providing diffs in four formats:
* ndiff: lists every line and highlights interline changes.
* context: highlights clusters of changes in a before/after format
* context: highlights clusters of changes in a before/after format.
* unified: highlights clusters of changes in an inline format.
* html: generates side by side comparison with change highlights.
"""
......@@ -14,6 +15,7 @@ def main():
parser = optparse.OptionParser(usage)
parser.add_option("-c", action="store_true", default=False, help='Produce a context format diff (default)')
parser.add_option("-u", action="store_true", default=False, help='Produce a unified format diff')
parser.add_option("-m", action="store_true", default=False, help='Produce HTML side by side diff (can use -c and -l in conjunction)')
parser.add_option("-n", action="store_true", default=False, help='Produce a ndiff format diff')
parser.add_option("-l", "--lines", type="int", default=3, help='Set number of context lines (default 3)')
(options, args) = parser.parse_args()
......@@ -36,6 +38,8 @@ def main():
diff = difflib.unified_diff(fromlines, tolines, fromfile, tofile, fromdate, todate, n=n)
elif options.n:
diff = difflib.ndiff(fromlines, tolines)
elif options.m:
diff = difflib.HtmlDiff().make_file(fromlines,tolines,fromfile,tofile,context=options.c,numlines=n)
else:
diff = difflib.context_diff(fromlines, tolines, fromfile, tofile, fromdate, todate, n=n)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment