Attachment 'gobby-0.0.1.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+id="(?P<id>\d+)"\s*owner="(?P<owner>\d+)"\s*title="(?P<title>[^"]*)"\s*$')
34 part_re = re.compile(r'^\s*part\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 result = self.parse_gobby(self.raw)
44 self.request.write(self.request.formatter.rawHTML(result))
45
46
47 def quote_html(self, text):
48 return text.replace('&', '&').replace('<', '<').replace('>', '>')
49
50 def unquote(self, text):
51 return text.replace(r'\"', '"')
52
53 def parse_gobby(self, text):
54 user_colors = {}
55 user_names = {}
56 docs = {}
57 cur_doc = None
58 cur_line = None
59 for line in text.split('\n'):
60 stripped = line.strip()
61 if stripped=='line':
62 cur_line = []
63 cur_doc.append(cur_line)
64 else:
65 m = self.user_re.match(line)
66 if m:
67 name = m.group('name')
68 user_id = int(m.group('id'))
69 color = m.group('color')
70 user_colors[user_id]=color
71 user_names[user_id]=name
72 else:
73 m = self.doc_re.match(line)
74 if m:
75 title = m.group('title')
76 owner = int(m.group('owner'))
77 doc_id = int(m.group('id'))
78 docs[title] = []
79 cur_doc = docs[title]
80 cur_line = []
81 cur_doc.append(cur_line)
82 else:
83 m = self.part_re.match(line)
84 if m:
85 author = int(m.group('author'))
86 content = self.unquote(m.group('content'))
87 cur_line.append( (author, content) )
88 result= '''<style type="text/css"><!--'''
89
90 for user,color in user_colors.iteritems():
91 result+= '.user%d { background-color: #%s }' % (user, color)
92
93 result+= '''--></style>'''
94 result+= '<h1>Users</h1>'
95 result+= '<ul>'
96 for user,name in user_names.iteritems():
97 result+= '<li><span class="user%d">%s</span></li>' % (user, self.quote_html(name))
98 result+= '</ul>'
99
100 result+= '<h1>Documents</h1>'
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 html += '%s<br>' % part_html
108 result+= '<h2>%s</h2>' % self.quote_html(title)
109 result+= '<pre>%s</pre>' % html
110 return result
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.You are not allowed to attach a file to this page.