1 | # Author: Melnychuk Taras |
---|
2 | # Contact: fenix@quintagroup.com |
---|
3 | # Date: $Date: 2006-08-11 |
---|
4 | # Copyright: quintagroup.com |
---|
5 | |
---|
6 | import re |
---|
7 | from Products.CMFCore.utils import getToolByName |
---|
8 | from Products.PortalTransforms.interfaces import itransform |
---|
9 | |
---|
10 | from Products.qPloneResolveUID.config import * |
---|
11 | |
---|
12 | class ruid_to_url: |
---|
13 | """Transform which replaces resolve uid into urls""" |
---|
14 | |
---|
15 | __implements__ = itransform |
---|
16 | |
---|
17 | __name__ = "ruid_to_url" |
---|
18 | inputs = ('text/html',) |
---|
19 | output = 'text/html' |
---|
20 | |
---|
21 | def __init__(self, name=None): |
---|
22 | if name: |
---|
23 | self.__name__ = name |
---|
24 | self.tag_regexp = re.compile(TAG_PATTERN ,re.I|re.S) |
---|
25 | self.ruid_regexp = re.compile(UID_PATTERN ,re.I|re.S) |
---|
26 | |
---|
27 | def name(self): |
---|
28 | return self.__name__ |
---|
29 | |
---|
30 | def find_ruid(self, data): |
---|
31 | tags_ruid = [] |
---|
32 | unique_ruid = [] |
---|
33 | for m in self.tag_regexp.finditer(data): |
---|
34 | ruid = re.search(self.ruid_regexp, m.group(0)) |
---|
35 | if ruid: |
---|
36 | tags_ruid.append({m.group(0):ruid.group('uid')}) |
---|
37 | [unique_ruid.append(tu.values()[0]) for tu in tags_ruid if tu.values()[0] not in unique_ruid] |
---|
38 | return tags_ruid, unique_ruid |
---|
39 | |
---|
40 | def mapRUID_URL(self, unique_ruid, portal): |
---|
41 | ruid_url = {} |
---|
42 | rc = getToolByName(portal, 'reference_catalog') |
---|
43 | pu = getToolByName(portal, 'portal_url') |
---|
44 | for uid in unique_ruid: |
---|
45 | try: |
---|
46 | obj = rc.lookupObject(uid) |
---|
47 | ruid_url[uid] = pu.getRelativeUrl(obj) |
---|
48 | except: |
---|
49 | ruid_url[uid] = uid |
---|
50 | return ruid_url |
---|
51 | |
---|
52 | def convert(self, orig, data, **kwargs): |
---|
53 | text = orig |
---|
54 | tags_ruid, unique_ruid = self.find_ruid(text) |
---|
55 | if unique_ruid: |
---|
56 | ruid_url = self.mapRUID_URL(unique_ruid, kwargs['context']) |
---|
57 | for tag_ruid in tags_ruid: |
---|
58 | t, uid = tag_ruid.items()[0] |
---|
59 | text = text.replace(t, t.replace('resolveuid/'+uid, ruid_url[uid])) |
---|
60 | |
---|
61 | data.setData(text) |
---|
62 | return data |
---|
63 | |
---|
64 | |
---|
65 | def register(): |
---|
66 | return ruid_to_url() |
---|
67 | |
---|