Attachment 'diff.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     Small changes for MoinMoin 1.5 by Emilio Lopes, 2006-03-01.
   9 
  10     $Id: $
  11 """
  12 
  13 # Imports
  14 import cgi, string, re, sys
  15 
  16 
  17 #############################################################################
  18 ### Plain Text Parser
  19 #############################################################################
  20 
  21 # Currently, only work with diff -u output... other format
  22 # should be easy to add.
  23 
  24 _LINE_RE = [
  25         (re.compile('^(diff .*?)$', re.M), 'green'), # command line
  26         (re.compile('^(--- .*?)$', re.M), 'green'), # old file
  27         (re.compile('^(\+\+\+ .*?)$', re.M), 'green'), # new file
  28         (re.compile('^(@@ .*?)$', re.M), 'magenta'), # lines numbers
  29         (re.compile('^(-.*?)$', re.M), 'red'), # removed line
  30         (re.compile('^(\+.*?)$', re.M), 'blue'), # added line
  31 ]
  32 
  33 class _colorized_match:
  34     """Englobe the match with <font color="color">match</font>
  35     """
  36     def __init__(self, color):
  37         self.color = color
  38 
  39     def __call__(self, match):
  40         return '<font color="%s">%s</font>' % (self.color, match.group(0))
  41 
  42 class Parser:
  43     """ Colorized diff output
  44     """
  45 
  46     def __init__(self, raw, request, **kw):
  47         self.raw = raw
  48         self.request = request
  49         self.out = kw.get('out', sys.stdout)
  50 
  51     def format(self, form):
  52         """ Send the text.
  53         """
  54 
  55         text = '<font face="LucidaTypewriter,Courier New">'
  56         color_me = _colorized_match('grey')
  57         #!!! send each line via the usual formatter calls
  58         text = cgi.escape(self.raw)
  59         # text = color_me(text)
  60         text = string.expandtabs(text)
  61         for (regex, color) in _LINE_RE:
  62             color_me.color = color
  63             text = regex.sub(color_me, text)
  64 
  65         self.request.write('<pre class="code">' + text + '</pre></font>')

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] (2006-03-01 17:11:49, 2.0 KB) [[attachment:diff.py]]
 All files | Selected Files: delete move to page copy to page

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