comparison roundup/cgi/templating.py @ 4396:172489ea9e07

allow trackers to override the classes used to render properties... ...in templating per issue2550659 (thanks Ezio Melotti)
author Richard Jones <richard@users.sourceforge.net>
date Sat, 24 Jul 2010 09:44:58 +0000
parents d5239335fae3
children 66603a9051f9
comparison
equal deleted inserted replaced
4395:b9eb72f20b4d 4396:172489ea9e07
1341 (?P<email>[-+=%/\w\.]+@[\w\.\-]+)| 1341 (?P<email>[-+=%/\w\.]+@[\w\.\-]+)|
1342 (?P<item>(?P<class>[A-Za-z_]+)(\s*)(?P<id>\d+)) 1342 (?P<item>(?P<class>[A-Za-z_]+)(\s*)(?P<id>\d+))
1343 )''', re.X | re.I) 1343 )''', re.X | re.I)
1344 protocol_re = re.compile('^(ht|f)tp(s?)://', re.I) 1344 protocol_re = re.compile('^(ht|f)tp(s?)://', re.I)
1345 1345
1346 def _hyper_repl_item(self,match,replacement): 1346
1347
1348 def _hyper_repl(self, match):
1349 if match.group('url'):
1350 return self._hyper_repl_url(match, '<a href="%s">%s</a>%s')
1351 elif match.group('email'):
1352 return self._hyper_repl_email(match, '<a href="mailto:%s">%s</a>')
1353 elif len(match.group('id')) < 10:
1354 return self._hyper_repl_item(match,
1355 '<a href="%(cls)s%(id)s">%(item)s</a>')
1356 else:
1357 # just return the matched text
1358 return match.group(0)
1359
1360 def _hyper_repl_url(self, match, replacement):
1361 u = s = match.group('url')
1362 if not self.protocol_re.search(s):
1363 u = 'http://' + s
1364 if s.endswith('&gt;'):
1365 # catch an escaped ">" at the end of the URL
1366 u = s = s[:-4]
1367 e = '&gt;'
1368 elif s.count('(') != s.count(')'):
1369 # don't include extraneous ')' in the link
1370 pos = s.rfind(')')
1371 e = s[pos:]
1372 u = s = s[:pos]
1373 else:
1374 e = ''
1375 return replacement % (u, s, e)
1376
1377 def _hyper_repl_email(self, match, replacement):
1378 s = match.group('email')
1379 return replacement % (s, s)
1380
1381 def _hyper_repl_item(self, match, replacement):
1347 item = match.group('item') 1382 item = match.group('item')
1348 cls = match.group('class').lower() 1383 cls = match.group('class').lower()
1349 id = match.group('id') 1384 id = match.group('id')
1350 try: 1385 try:
1351 # make sure cls is a valid tracker classname 1386 # make sure cls is a valid tracker classname
1354 return item 1389 return item
1355 return replacement % locals() 1390 return replacement % locals()
1356 except KeyError: 1391 except KeyError:
1357 return item 1392 return item
1358 1393
1359 def _hyper_repl(self, match):
1360 if match.group('url'):
1361 u = s = match.group('url')
1362 if not self.protocol_re.search(s):
1363 u = 'http://' + s
1364 if s.endswith('&gt;'):
1365 # catch an escaped ">" at the end of the URL
1366 u = s = s[:-4]
1367 e = '&gt;'
1368 elif s.count('(') != s.count(')'):
1369 # don't include extraneous ')' in the link
1370 pos = s.rfind(')')
1371 e = s[pos:]
1372 u = s = s[:pos]
1373 else:
1374 e = ''
1375 return '<a href="%s">%s</a>%s' % (u, s, e)
1376 elif match.group('email'):
1377 s = match.group('email')
1378 return '<a href="mailto:%s">%s</a>' % (s, s)
1379 elif len(match.group('id')) < 10:
1380 return self._hyper_repl_item(match,
1381 '<a href="%(cls)s%(id)s">%(item)s</a>')
1382 else:
1383 # just return the matched text
1384 return match.group(0)
1385 1394
1386 def _hyper_repl_rst(self, match): 1395 def _hyper_repl_rst(self, match):
1387 if match.group('url'): 1396 if match.group('url'):
1388 s = match.group('url') 1397 s = match.group('url')
1389 return '`%s <%s>`_'%(s, s) 1398 return '`%s <%s>`_'%(s, s)
2279 l.append('<option %svalue="%s">%s</option>'%(s, optionid, 2288 l.append('<option %svalue="%s">%s</option>'%(s, optionid,
2280 lab)) 2289 lab))
2281 l.append('</select>') 2290 l.append('</select>')
2282 return '\n'.join(l) 2291 return '\n'.join(l)
2283 2292
2293
2284 # set the propclasses for HTMLItem 2294 # set the propclasses for HTMLItem
2285 propclasses = ( 2295 propclasses = [
2286 (hyperdb.String, StringHTMLProperty), 2296 (hyperdb.String, StringHTMLProperty),
2287 (hyperdb.Number, NumberHTMLProperty), 2297 (hyperdb.Number, NumberHTMLProperty),
2288 (hyperdb.Boolean, BooleanHTMLProperty), 2298 (hyperdb.Boolean, BooleanHTMLProperty),
2289 (hyperdb.Date, DateHTMLProperty), 2299 (hyperdb.Date, DateHTMLProperty),
2290 (hyperdb.Interval, IntervalHTMLProperty), 2300 (hyperdb.Interval, IntervalHTMLProperty),
2291 (hyperdb.Password, PasswordHTMLProperty), 2301 (hyperdb.Password, PasswordHTMLProperty),
2292 (hyperdb.Link, LinkHTMLProperty), 2302 (hyperdb.Link, LinkHTMLProperty),
2293 (hyperdb.Multilink, MultilinkHTMLProperty), 2303 (hyperdb.Multilink, MultilinkHTMLProperty),
2294 ) 2304 ]
2305
2306 def register_propclass(prop, cls):
2307 for index,propclass in enumerate(propclasses):
2308 p, c = propclass
2309 if prop == p:
2310 propclasses[index] = (prop, cls)
2311 break
2312 else:
2313 propclasses.append((prop, cls))
2314
2295 2315
2296 def make_sort_function(db, classname, sort_on=None): 2316 def make_sort_function(db, classname, sort_on=None):
2297 """Make a sort function for a given class 2317 """Make a sort function for a given class
2298 """ 2318 """
2299 linkcl = db.getclass(classname) 2319 linkcl = db.getclass(classname)

Roundup Issue Tracker: http://roundup-tracker.org/