Back to index

python3.2  3.2.2
Public Member Functions | Public Attributes | Private Member Functions | Private Attributes | Static Private Attributes
difflib.HtmlDiff Class Reference
Inheritance diagram for difflib.HtmlDiff:
Inheritance graph
[legend]
Collaboration diagram for difflib.HtmlDiff:
Collaboration graph
[legend]

List of all members.

Public Member Functions

def __init__
def make_file
def make_table

Public Attributes

_PyObject_HEAD_EXTRA Py_ssize_t ob_refcnt
struct _typeobjectob_type

Private Member Functions

def _tab_newline_replace
def _split_line
def _line_wrapper
def _collect_lines
def _format_line
def _make_prefix
def _convert_flags

Private Attributes

 _tabsize
 _wrapcolumn
 _linejunk
 _charjunk
 _prefix

Static Private Attributes

 _file_template = _file_template
 _styles = _styles
 _table_template = _table_template
 _legend = _legend
int _default_prefix = 0

Detailed Description

For producing HTML side by side comparison with change highlights.

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 comparison
of text with inter-line and intra-line change highlights.  The table can
be generated in either full or contextual difference mode.

The following methods are provided for HTML generation:

make_table -- generates HTML for 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.

Definition at line 1677 of file difflib.py.


Constructor & Destructor Documentation

def difflib.HtmlDiff.__init__ (   self,
  tabsize = 8,
  wrapcolumn = None,
  linejunk = None,
  charjunk = IS_CHARACTER_JUNK 
)
HtmlDiff instance initializer

Arguments:
tabsize -- tab stop spacing, defaults to 8.
wrapcolumn -- column number where lines are broken and wrapped,
    defaults to None where lines are not wrapped.
linejunk,charjunk -- keyword arguments passed into ndiff() (used to by
    HtmlDiff() to generate the side by side HTML differences).  See
    ndiff() documentation for argument default values and descriptions.

Definition at line 1700 of file difflib.py.

01700 
01701                  charjunk=IS_CHARACTER_JUNK):
01702         """HtmlDiff instance initializer
01703 
01704         Arguments:
01705         tabsize -- tab stop spacing, defaults to 8.
01706         wrapcolumn -- column number where lines are broken and wrapped,
01707             defaults to None where lines are not wrapped.
01708         linejunk,charjunk -- keyword arguments passed into ndiff() (used to by
01709             HtmlDiff() to generate the side by side HTML differences).  See
01710             ndiff() documentation for argument default values and descriptions.
01711         """
01712         self._tabsize = tabsize
01713         self._wrapcolumn = wrapcolumn
01714         self._linejunk = linejunk
01715         self._charjunk = charjunk

Here is the caller graph for this function:


Member Function Documentation

def difflib.HtmlDiff._collect_lines (   self,
  diffs 
) [private]
Collects mdiff output into separate lists

Before storing the mdiff from/to data into a list, it is converted
into a single line of text with HTML markup.

Definition at line 1846 of file difflib.py.

01846 
01847     def _collect_lines(self,diffs):
01848         """Collects mdiff output into separate lists
01849 
01850         Before storing the mdiff from/to data into a list, it is converted
01851         into a single line of text with HTML markup.
01852         """
01853 
01854         fromlist,tolist,flaglist = [],[],[]
01855         # pull from/to data and flags from mdiff style iterator
01856         for fromdata,todata,flag in diffs:
01857             try:
01858                 # store HTML markup of the lines into the lists
01859                 fromlist.append(self._format_line(0,flag,*fromdata))
01860                 tolist.append(self._format_line(1,flag,*todata))
01861             except TypeError:
01862                 # exceptions occur for lines where context separators go
01863                 fromlist.append(None)
01864                 tolist.append(None)
01865             flaglist.append(flag)
01866         return fromlist,tolist,flaglist

Here is the call graph for this function:

Here is the caller graph for this function:

def difflib.HtmlDiff._convert_flags (   self,
  fromlist,
  tolist,
  flaglist,
  context,
  numlines 
) [private]
Makes list of "next" links

Definition at line 1901 of file difflib.py.

01901 
01902     def _convert_flags(self,fromlist,tolist,flaglist,context,numlines):
01903         """Makes list of "next" links"""
01904 
01905         # all anchor names will be generated using the unique "to" prefix
01906         toprefix = self._prefix[1]
01907 
01908         # process change flags, generating middle column of next anchors/links
01909         next_id = ['']*len(flaglist)
01910         next_href = ['']*len(flaglist)
01911         num_chg, in_change = 0, False
01912         last = 0
01913         for i,flag in enumerate(flaglist):
01914             if flag:
01915                 if not in_change:
01916                     in_change = True
01917                     last = i
01918                     # at the beginning of a change, drop an anchor a few lines
01919                     # (the context lines) before the change for the previous
01920                     # link
01921                     i = max([0,i-numlines])
01922                     next_id[i] = ' id="difflib_chg_%s_%d"' % (toprefix,num_chg)
01923                     # at the beginning of a change, drop a link to the next
01924                     # change
01925                     num_chg += 1
01926                     next_href[last] = '<a href="#difflib_chg_%s_%d">n</a>' % (
01927                          toprefix,num_chg)
01928             else:
01929                 in_change = False
01930         # check for cases where there is no content to avoid exceptions
01931         if not flaglist:
01932             flaglist = [False]
01933             next_id = ['']
01934             next_href = ['']
01935             last = 0
01936             if context:
01937                 fromlist = ['<td></td><td>&nbsp;No Differences Found&nbsp;</td>']
01938                 tolist = fromlist
01939             else:
01940                 fromlist = tolist = ['<td></td><td>&nbsp;Empty File&nbsp;</td>']
01941         # if not a change on first line, drop a link
01942         if not flaglist[0]:
01943             next_href[0] = '<a href="#difflib_chg_%s_0">f</a>' % toprefix
01944         # redo the last link to link to the top
01945         next_href[last] = '<a href="#difflib_chg_%s_top">t</a>' % (toprefix)
01946 
01947         return fromlist,tolist,flaglist,next_href,next_id

Here is the call graph for this function:

Here is the caller graph for this function:

def difflib.HtmlDiff._format_line (   self,
  side,
  flag,
  linenum,
  text 
) [private]
Returns HTML markup of "from" / "to" text lines

side -- 0 or 1 indicating "from" or "to" text
flag -- indicates if difference on line
linenum -- line number (used for line number column)
text -- line text to be marked up

Definition at line 1867 of file difflib.py.

01867 
01868     def _format_line(self,side,flag,linenum,text):
01869         """Returns HTML markup of "from" / "to" text lines
01870 
01871         side -- 0 or 1 indicating "from" or "to" text
01872         flag -- indicates if difference on line
01873         linenum -- line number (used for line number column)
01874         text -- line text to be marked up
01875         """
01876         try:
01877             linenum = '%d' % linenum
01878             id = ' id="%s%s"' % (self._prefix[side],linenum)
01879         except TypeError:
01880             # handle blank lines where linenum is '>' or ''
01881             id = ''
01882         # replace those things that would get confused with HTML symbols
01883         text=text.replace("&","&amp;").replace(">","&gt;").replace("<","&lt;")
01884 
01885         # make space non-breakable so they don't get compressed or line wrapped
01886         text = text.replace(' ','&nbsp;').rstrip()
01887 
01888         return '<td class="diff_header"%s>%s</td><td nowrap="nowrap">%s</td>' \
01889                % (id,linenum,text)

Here is the call graph for this function:

Here is the caller graph for this function:

def difflib.HtmlDiff._line_wrapper (   self,
  diffs 
) [private]
Returns iterator that splits (wraps) mdiff text lines

Definition at line 1818 of file difflib.py.

01818 
01819     def _line_wrapper(self,diffs):
01820         """Returns iterator that splits (wraps) mdiff text lines"""
01821 
01822         # pull from/to data and flags from mdiff iterator
01823         for fromdata,todata,flag in diffs:
01824             # check for context separators and pass them through
01825             if flag is None:
01826                 yield fromdata,todata,flag
01827                 continue
01828             (fromline,fromtext),(toline,totext) = fromdata,todata
01829             # for each from/to line split it at the wrap column to form
01830             # list of text lines.
01831             fromlist,tolist = [],[]
01832             self._split_line(fromlist,fromline,fromtext)
01833             self._split_line(tolist,toline,totext)
01834             # yield from/to line in pairs inserting blank lines as
01835             # necessary when one side has more wrapped lines
01836             while fromlist or tolist:
01837                 if fromlist:
01838                     fromdata = fromlist.pop(0)
01839                 else:
01840                     fromdata = ('',' ')
01841                 if tolist:
01842                     todata = tolist.pop(0)
01843                 else:
01844                     todata = ('',' ')
01845                 yield fromdata,todata,flag

Here is the call graph for this function:

Here is the caller graph for this function:

def difflib.HtmlDiff._make_prefix (   self) [private]
Create unique anchor prefixes

Definition at line 1890 of file difflib.py.

01890 
01891     def _make_prefix(self):
01892         """Create unique anchor prefixes"""
01893 
01894         # Generate a unique anchor prefix so multiple tables
01895         # can exist on the same HTML page without conflicts.
01896         fromprefix = "from%d_" % HtmlDiff._default_prefix
01897         toprefix = "to%d_" % HtmlDiff._default_prefix
01898         HtmlDiff._default_prefix += 1
01899         # store prefixes so line format method has access
01900         self._prefix = [fromprefix,toprefix]

Here is the caller graph for this function:

def difflib.HtmlDiff._split_line (   self,
  data_list,
  line_num,
  text 
) [private]
Builds list of text lines by splitting text lines at wrap point

This function will determine if the input text line needs to be
wrapped (split) into separate lines.  If so, the first wrap point
will be determined and the first line appended to the output
text line list.  This function is used recursively to handle
the second part of the split line to further split it.

Definition at line 1763 of file difflib.py.

01763 
01764     def _split_line(self,data_list,line_num,text):
01765         """Builds list of text lines by splitting text lines at wrap point
01766 
01767         This function will determine if the input text line needs to be
01768         wrapped (split) into separate lines.  If so, the first wrap point
01769         will be determined and the first line appended to the output
01770         text line list.  This function is used recursively to handle
01771         the second part of the split line to further split it.
01772         """
01773         # if blank line or context separator, just add it to the output list
01774         if not line_num:
01775             data_list.append((line_num,text))
01776             return
01777 
01778         # if line text doesn't need wrapping, just add it to the output list
01779         size = len(text)
01780         max = self._wrapcolumn
01781         if (size <= max) or ((size -(text.count('\0')*3)) <= max):
01782             data_list.append((line_num,text))
01783             return
01784 
01785         # scan text looking for the wrap point, keeping track if the wrap
01786         # point is inside markers
01787         i = 0
01788         n = 0
01789         mark = ''
01790         while n < max and i < size:
01791             if text[i] == '\0':
01792                 i += 1
01793                 mark = text[i]
01794                 i += 1
01795             elif text[i] == '\1':
01796                 i += 1
01797                 mark = ''
01798             else:
01799                 i += 1
01800                 n += 1
01801 
01802         # wrap point is inside text, break it up into separate lines
01803         line1 = text[:i]
01804         line2 = text[i:]
01805 
01806         # if wrap point is inside markers, place end marker at end of first
01807         # line and start marker at beginning of second line because each
01808         # line will have its own table tag markup around it.
01809         if mark:
01810             line1 = line1 + '\1'
01811             line2 = '\0' + mark + line2
01812 
01813         # tack on first line onto the output list
01814         data_list.append((line_num,line1))
01815 
01816         # use this routine again to wrap the remaining text
01817         self._split_line(data_list,'>',line2)

Here is the call graph for this function:

Here is the caller graph for this function:

def difflib.HtmlDiff._tab_newline_replace (   self,
  fromlines,
  tolines 
) [private]
Returns from/to line lists with tabs expanded and newlines removed.

Instead of tab characters being replaced by the number of spaces
needed to fill in to the next tab stop, this function will fill
the space with tab characters.  This is done so that the difference
algorithms can identify changes in a file when tabs are replaced by
spaces and vice versa.  At the end of the HTML generation, the tab
characters will be replaced with a nonbreakable space.

Definition at line 1740 of file difflib.py.

01740 
01741     def _tab_newline_replace(self,fromlines,tolines):
01742         """Returns from/to line lists with tabs expanded and newlines removed.
01743 
01744         Instead of tab characters being replaced by the number of spaces
01745         needed to fill in to the next tab stop, this function will fill
01746         the space with tab characters.  This is done so that the difference
01747         algorithms can identify changes in a file when tabs are replaced by
01748         spaces and vice versa.  At the end of the HTML generation, the tab
01749         characters will be replaced with a nonbreakable space.
01750         """
01751         def expand_tabs(line):
01752             # hide real spaces
01753             line = line.replace(' ','\0')
01754             # expand tabs into spaces
01755             line = line.expandtabs(self._tabsize)
01756             # replace spaces from expanded tabs back into tab characters
01757             # (we'll replace them with markup after we do differencing)
01758             line = line.replace(' ','\t')
01759             return line.replace('\0',' ').rstrip('\n')
01760         fromlines = [expand_tabs(line) for line in fromlines]
01761         tolines = [expand_tabs(line) for line in tolines]
01762         return fromlines,tolines

Here is the caller graph for this function:

def difflib.HtmlDiff.make_file (   self,
  fromlines,
  tolines,
  fromdesc = '',
  todesc = '',
  context = False,
  numlines = 5 
)
Returns HTML file of side by side comparison with change highlights

Arguments:
fromlines -- list of "from" lines
tolines -- list of "to" lines
fromdesc -- "from" file column header string
todesc -- "to" file column header string
context -- set to True for contextual differences (defaults to False
    which shows full differences).
numlines -- number of context lines.  When context is set True,
    controls number of lines displayed before and after the change.
    When context is False, controls the number of lines to place
    the "next" link anchors before the next change (so click of
    "next" link jumps to just before the change).

Definition at line 1717 of file difflib.py.

01717 
01718                   numlines=5):
01719         """Returns HTML file of side by side comparison with change highlights
01720 
01721         Arguments:
01722         fromlines -- list of "from" lines
01723         tolines -- list of "to" lines
01724         fromdesc -- "from" file column header string
01725         todesc -- "to" file column header string
01726         context -- set to True for contextual differences (defaults to False
01727             which shows full differences).
01728         numlines -- number of context lines.  When context is set True,
01729             controls number of lines displayed before and after the change.
01730             When context is False, controls the number of lines to place
01731             the "next" link anchors before the next change (so click of
01732             "next" link jumps to just before the change).
01733         """
01734 
01735         return self._file_template % dict(
01736             styles = self._styles,
01737             legend = self._legend,
01738             table = self.make_table(fromlines,tolines,fromdesc,todesc,
01739                                     context=context,numlines=numlines))

Here is the call graph for this function:

def difflib.HtmlDiff.make_table (   self,
  fromlines,
  tolines,
  fromdesc = '',
  todesc = '',
  context = False,
  numlines = 5 
)
Returns HTML table of side by side comparison with change highlights

Arguments:
fromlines -- list of "from" lines
tolines -- list of "to" lines
fromdesc -- "from" file column header string
todesc -- "to" file column header string
context -- set to True for contextual differences (defaults to False
    which shows full differences).
numlines -- number of context lines.  When context is set True,
    controls number of lines displayed before and after the change.
    When context is False, controls the number of lines to place
    the "next" link anchors before the next change (so click of
    "next" link jumps to just before the change).

Definition at line 1949 of file difflib.py.

01949 
01950                    numlines=5):
01951         """Returns HTML table of side by side comparison with change highlights
01952 
01953         Arguments:
01954         fromlines -- list of "from" lines
01955         tolines -- list of "to" lines
01956         fromdesc -- "from" file column header string
01957         todesc -- "to" file column header string
01958         context -- set to True for contextual differences (defaults to False
01959             which shows full differences).
01960         numlines -- number of context lines.  When context is set True,
01961             controls number of lines displayed before and after the change.
01962             When context is False, controls the number of lines to place
01963             the "next" link anchors before the next change (so click of
01964             "next" link jumps to just before the change).
01965         """
01966 
01967         # make unique anchor prefixes so that multiple tables may exist
01968         # on the same page without conflict.
01969         self._make_prefix()
01970 
01971         # change tabs to spaces before it gets more difficult after we insert
01972         # markkup
01973         fromlines,tolines = self._tab_newline_replace(fromlines,tolines)
01974 
01975         # create diffs iterator which generates side by side from/to data
01976         if context:
01977             context_lines = numlines
01978         else:
01979             context_lines = None
01980         diffs = _mdiff(fromlines,tolines,context_lines,linejunk=self._linejunk,
01981                       charjunk=self._charjunk)
01982 
01983         # set up iterator to wrap lines that exceed desired width
01984         if self._wrapcolumn:
01985             diffs = self._line_wrapper(diffs)
01986 
01987         # collect up from/to lines and flags into lists (also format the lines)
01988         fromlist,tolist,flaglist = self._collect_lines(diffs)
01989 
01990         # process change flags, generating middle column of next anchors/links
01991         fromlist,tolist,flaglist,next_href,next_id = self._convert_flags(
01992             fromlist,tolist,flaglist,context,numlines)
01993 
01994         s = []
01995         fmt = '            <tr><td class="diff_next"%s>%s</td>%s' + \
01996               '<td class="diff_next">%s</td>%s</tr>\n'
01997         for i in range(len(flaglist)):
01998             if flaglist[i] is None:
01999                 # mdiff yields None on separator lines skip the bogus ones
02000                 # generated for the first line
02001                 if i > 0:
02002                     s.append('        </tbody>        \n        <tbody>\n')
02003             else:
02004                 s.append( fmt % (next_id[i],next_href[i],fromlist[i],
02005                                            next_href[i],tolist[i]))
02006         if fromdesc or todesc:
02007             header_row = '<thead><tr>%s%s%s%s</tr></thead>' % (
02008                 '<th class="diff_next"><br /></th>',
02009                 '<th colspan="2" class="diff_header">%s</th>' % fromdesc,
02010                 '<th class="diff_next"><br /></th>',
02011                 '<th colspan="2" class="diff_header">%s</th>' % todesc)
02012         else:
02013             header_row = ''
02014 
02015         table = self._table_template % dict(
02016             data_rows=''.join(s),
02017             header_row=header_row,
02018             prefix=self._prefix[1])
02019 
02020         return table.replace('\0+','<span class="diff_add">'). \
02021                      replace('\0-','<span class="diff_sub">'). \
02022                      replace('\0^','<span class="diff_chg">'). \
02023                      replace('\1','</span>'). \
02024                      replace('\t','&nbsp;')

Here is the call graph for this function:

Here is the caller graph for this function:


Member Data Documentation

Definition at line 1714 of file difflib.py.

int difflib.HtmlDiff._default_prefix = 0 [static, private]

Definition at line 1697 of file difflib.py.

Definition at line 1693 of file difflib.py.

difflib.HtmlDiff._legend = _legend [static, private]

Definition at line 1696 of file difflib.py.

Definition at line 1713 of file difflib.py.

Definition at line 1899 of file difflib.py.

difflib.HtmlDiff._styles = _styles [static, private]

Definition at line 1694 of file difflib.py.

Definition at line 1695 of file difflib.py.

Definition at line 1711 of file difflib.py.

Definition at line 1712 of file difflib.py.

Definition at line 107 of file object.h.

struct _typeobject* _object::ob_type [inherited]

Definition at line 108 of file object.h.


The documentation for this class was generated from the following file: