comparison roundup/cgi/templating.py @ 3991:13161539e5bd

improved URL matching
author Richard Jones <richard@users.sourceforge.net>
date Mon, 18 Aug 2008 06:03:06 +0000
parents eee76dd4a09f
children e335ce40d6c8
comparison
equal deleted inserted replaced
3990:0728808fdf5c 3991:13161539e5bd
1243 self._classname, self._name, self._nodeid): 1243 self._classname, self._name, self._nodeid):
1244 return 1 1244 return 1
1245 return self.is_edit_ok() 1245 return self.is_edit_ok()
1246 1246
1247 class StringHTMLProperty(HTMLProperty): 1247 class StringHTMLProperty(HTMLProperty):
1248 hyper_re = re.compile(r'((?P<url>\w{3,6}://\S+)|' 1248 hyper_re = re.compile(r'''(
1249 r'(?P<email>[-+=%/\w\.]+@[\w\.\-]+)|' 1249 (?P<url>
1250 r'(?P<item>(?P<class>[A-Za-z_]+)(\s*)(?P<id>\d+)))') 1250 ((ht|f)tp(s?)://|www\.)? # prefix
1251 ([\w]+:\w+@)? # username/password
1252 (([\w\-]+\.)+([\w]{2,5})) # hostname
1253 (:[\d]{1,5})? # port
1254 (/[\w\-$.+!*(),;:@&=?/~\\#%]*)? # path etc.
1255 )|
1256 (?P<email>[-+=%/\w\.]+@[\w\.\-]+)|
1257 (?P<item>(?P<class>[A-Za-z_]+)(\s*)(?P<id>\d+))
1258 )''', re.X | re.I)
1259 protocol_re = re.compile('^(ht|f)tp(s?)://', re.I)
1260
1251 def _hyper_repl_item(self,match,replacement): 1261 def _hyper_repl_item(self,match,replacement):
1252 item = match.group('item') 1262 item = match.group('item')
1253 cls = match.group('class').lower() 1263 cls = match.group('class').lower()
1254 id = match.group('id') 1264 id = match.group('id')
1255 try: 1265 try:
1261 except KeyError: 1271 except KeyError:
1262 return item 1272 return item
1263 1273
1264 def _hyper_repl(self, match): 1274 def _hyper_repl(self, match):
1265 if match.group('url'): 1275 if match.group('url'):
1266 s = match.group('url') 1276 u = s = match.group('url')
1267 return '<a href="%s">%s</a>'%(s, s) 1277 if not self.protocol_re.search(s):
1278 u = 'http://' + s
1279 # catch an escaped ">" at the end of the URL
1280 if s.endswith('&gt;'):
1281 u = s = s[:-4]
1282 e = '&gt;'
1283 else:
1284 e = ''
1285 return '<a href="%s">%s</a>%s'%(u, s, e)
1268 elif match.group('email'): 1286 elif match.group('email'):
1269 s = match.group('email') 1287 s = match.group('email')
1270 return '<a href="mailto:%s">%s</a>'%(s, s) 1288 return '<a href="mailto:%s">%s</a>'%(s, s)
1271 else: 1289 else:
1272 return self._hyper_repl_item(match, 1290 return self._hyper_repl_item(match,

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