Attachment 'gobby-0.0.3.py'

Download

   1 # -*- coding: iso-8859-1 -*-
   2 
   3 import re
   4 from MoinMoin import config, wikiutil
   5 
   6 """
   7     This is a MoinMoin parser plugin that can display, nicely colored,
   8     the Gobby 3.0 or Sobby saved session, preserving the coloring.
   9 
  10     Note: The session file parser is extremely primitive and hacky, and
  11     probably doesn't include some special cases, as it was written in 5
  12     minutes while just looking at a sample session file.
  13 
  14     Also, the code was previously a standalone filter, and was later 
  15     wrapped in all the MoinMoin plugin stuff -- so it has its own function
  16     for quoting html, and generally is very dirty.
  17 
  18     But at least it's some start.
  19 
  20     This is public domain.
  21 """
  22 
  23 
  24 
  25 Dependencies = []
  26 
  27 class Parser:
  28     # Enable caching
  29     caching = 1
  30     Dependencies = []
  31 
  32     user_re = re.compile(r'^\s*user\s+colour="(?P<color>[^"]*)"\s*id="(?P<id>\d+)"\s*name="(?P<name>[^"]*)"\s*$')
  33     doc_re = re.compile(r'^\s*document\s+(?:encoding="(?:[^"]*)"\s+)?id="(?P<id>\d+)"\s*owner="(?P<owner>\d+)"\s*(?:suffix="[0-9]+"\s+)?title="(?P<title>[^"]*)"\s*$')
  34     part_re = re.compile(r'^\s*chunk\s+author="(?P<author>\d+)"\s*content="(?P<content>.*)"$')
  35 
  36     def __init__(self, raw, request, **kw):
  37         """Create a minimal Parser object with required attributes."""
  38         self.request = request
  39         self.form = request.form
  40         self.raw = raw
  41         
  42     def format(self, formatter):
  43         self.parse_gobby(self.raw)
  44 
  45     def quote_html(self, text):
  46         return text.replace('&', '&amp;').replace('<', '&lt;').replace('>', '&gt;')
  47 
  48     def unquote(self, text):
  49         return text.replace(r'\"', '"')
  50 
  51 
  52     def parse_gobby(self, text):
  53         user_colors = {}
  54         user_names = {}
  55         docs = {}
  56         cur_doc = None
  57         cur_line = None
  58         for line in text.split('\n'):
  59             stripped = line.strip()
  60             if stripped=='line':
  61                 cur_line = []
  62                 cur_doc.append(cur_line)
  63             else:
  64                 m = self.user_re.match(line)
  65                 if m:
  66                     name = m.group('name')
  67                     user_id = int(m.group('id'))
  68                     color = m.group('color')
  69                     user_colors[user_id]=color
  70                     user_names[user_id]=name
  71                 else:
  72                     m = self.doc_re.match(line)
  73                     if m:
  74                         title = m.group('title')
  75                         owner = int(m.group('owner'))
  76                         doc_id = int(m.group('id'))
  77                         docs[title] = []
  78                         cur_doc = docs[title]
  79                         cur_line = []
  80                         cur_doc.append(cur_line)
  81                     else:
  82                         m = self.part_re.match(line)
  83                         if m:
  84                             author = int(m.group('author'))
  85                             content = self.unquote(m.group('content'))
  86                             cur_line.append( (author, content) )
  87         result = '''<style type="text/css"><!--'''
  88 
  89         for user,color in user_colors.iteritems():
  90             result += '.user%d { background-color: #%s }' % (user, color)
  91 
  92         result += '''--></style>'''
  93         result += '<h1>Users</h1>'
  94         result += '<ul>'
  95         for user,name in user_names.iteritems():
  96             result += '<li><span class="user%d">%s</span></li>' % (user, self.quote_html(name))
  97         result += '</ul>'
  98 
  99         result += '<h1>Documents</h1>'
 100         self.request.write(self.request.formatter.rawHTML(result))
 101         for title,body in docs.iteritems():
 102             html = ''
 103             for line in body:
 104                 part_html = ''
 105                 for part in line:
 106                     part_html += '<span class="user%d">%s</span>' % (part[0], self.quote_html(part[1]))
 107                 part_html = part_html.replace("\\n", "<br />")
 108                 html += part_html
 109             self.request.write(self.request.formatter.rawHTML('<h2>%s</h2>' % self.quote_html(title)))
 110             self.request.write(self.request.formatter.rawHTML(html))

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-08-22 21:57:25, 3.8 KB) [[attachment:gobby-0.0.1.py]]
  • [get | view] (2006-11-22 03:00:31, 4.0 KB) [[attachment:gobby-0.0.3.py]]
 All files | Selected Files: delete move to page copy to page

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