Attachment 'diff2.py'

Download

   1 """
   2     MoinMoin - Diff Parser
   3 
   4     Copyright (c) 2002 by Fabien Niñoles <fabien@tzone.org>
   5     Copyright (c) 2000, 2001, 2002 by Jürgen Hermann <jh@web.de>
   6     All rights reserved, see COPYING for details.
   7 
   8     $Id: $
   9 """
  10 
  11 # Imports
  12 import cgi, string, re, sys
  13 
  14 
  15 #############################################################################
  16 ### Plain Text Parser
  17 #############################################################################
  18 
  19 # Currently, only work with diff -u output... other format
  20 # should be easy to add.
  21 
  22 _LINE_RE = [
  23         (re.compile('^(diff .*?)$', re.M), 'green'), # command line
  24         (re.compile('^(--- .*?)$', re.M), 'green'), # old file
  25         (re.compile('^(\+\+\+ .*?)$', re.M), 'green'), # new file
  26     (re.compile('^(@@ .*?)$', re.M), 'magenta'), # lines numbers
  27     (re.compile('^(- .*?)$', re.M), 'red'), # removed line
  28     (re.compile('^(\+ .*?)$', re.M), 'blue'), # added line
  29 ]
  30 
  31 class _colorized_match:
  32     """Englobe the match with <font color="color">match</font>
  33     """
  34     def __init__(self, color):
  35         self.color = color
  36 
  37     def __call__(self, match):
  38         return '<font color="%s">%s</font>' % (self.color, match.group(0))
  39 
  40 class Parser:
  41     """ Colorized diff output
  42     """
  43 
  44     def __init__(self, raw, request, **kw):
  45         self.raw = raw
  46         self.out = kw.get('out', sys.stdout)
  47 
  48     def format(self, form):
  49         """ Send the text.
  50         """
  51 
  52         text = '<font face="LucidaTypewriter,Courier New">'
  53         color_me = _colorized_match('grey')
  54         #!!! send each line via the usual formatter calls
  55         text = cgi.escape(self.raw)
  56         # text = color_me(text)
  57         text = string.expandtabs(text)
  58         for (regex, color) in _LINE_RE:
  59             color_me.color = color
  60             text = regex.sub(color_me, text)
  61         print >>self.out, '<pre class="code">' + text + '</font></pre>' 

Attached Files

To refer to attachments on a page, use attachment:filename, as shown below in the list of files. Do NOT use the URL of the [get] link, since this is subject to change and can break easily.
  • [get | view] (2003-12-07 18:15:55, 1.8 KB) [[attachment:diff2.py]]
  • [get | view] (2003-12-07 18:15:55, 8.2 KB) [[attachment:linuxmagazin.py]]
 All files | Selected Files: delete move to page copy to page

You are not allowed to attach a file to this page.