##// END OF EJS Templates
helpers: refactor and optimize urlify_issues...
Mads Kiilerich -
r6199:39a59e69 default
parent child Browse files
Show More
@@ -1352,63 +1352,65 b' def linkify_others(t, l):'
1352 return ''.join(links)
1352 return ''.join(links)
1353
1353
1354
1354
1355 def _urlify_issues_replace_f(repo_name, ISSUE_SERVER_LNK, ISSUE_PREFIX):
1355 # Global variable that will hold the actual urlify_issues function body.
1356 def urlify_issues_replace(match_obj):
1356 # Will be set on first use when the global configuration has been read.
1357 pref = ''
1357 _urlify_issues_f = None
1358 if match_obj.group().startswith(' '):
1359 pref = ' '
1360
1361 issue_id = ''.join(match_obj.groups())
1362 issue_url = ISSUE_SERVER_LNK.replace('{id}', issue_id)
1363 issue_url = issue_url.replace('{repo}', repo_name)
1364 issue_url = issue_url.replace('{repo_name}', repo_name.split(URL_SEP)[-1])
1365
1366 return (
1367 '%(pref)s<a class="%(cls)s" href="%(url)s">'
1368 '%(issue-prefix)s%(id-repr)s'
1369 '</a>'
1370 ) % {
1371 'pref': pref,
1372 'cls': 'issue-tracker-link',
1373 'url': issue_url,
1374 'id-repr': issue_id,
1375 'issue-prefix': ISSUE_PREFIX,
1376 'serv': ISSUE_SERVER_LNK,
1377 }
1378 return urlify_issues_replace
1379
1358
1380
1359
1381 def urlify_issues(newtext, repo_name):
1360 def urlify_issues(newtext, repo_name):
1382 from kallithea import CONFIG as conf
1361 """Urlify issue references according to .ini configuration"""
1362 global _urlify_issues_f
1363 if _urlify_issues_f is None:
1364 from kallithea import CONFIG
1365 assert CONFIG['sqlalchemy.url'] # make sure config has been loaded
1366
1367 # Build chain of urlify functions, starting with not doing any transformation
1368 tmp_urlify_issues_f = lambda s: s
1383
1369
1384 # allow multiple issue servers to be used
1370 issue_pat_re = re.compile(r'issue_pat(.*)')
1385 valid_indices = [
1371 for k in CONFIG.keys():
1386 x.group(1)
1372 # Find all issue_pat* settings that also have corresponding server_link and prefix configuration
1387 for x in map(lambda x: re.match(r'issue_pat(.*)', x), conf.keys())
1373 m = issue_pat_re.match(k)
1388 if x and 'issue_server_link%s' % x.group(1) in conf
1374 if m is None:
1389 and 'issue_prefix%s' % x.group(1) in conf
1375 continue
1390 ]
1376 suffix = m.group(1)
1391
1377 issue_pat = CONFIG.get(k)
1392 if valid_indices:
1378 issue_server_link = CONFIG.get('issue_server_link%s' % suffix)
1393 log.debug('found issue server suffixes `%s` during valuation of: %s',
1379 issue_prefix = CONFIG.get('issue_prefix%s' % suffix)
1394 ','.join(valid_indices), newtext)
1380 if issue_pat and issue_server_link and issue_prefix:
1381 log.debug('issue pattern %r: %r -> %r %r', suffix, issue_pat, issue_server_link, issue_prefix)
1382 else:
1383 log.error('skipping incomplete issue pattern %r: %r -> %r %r', suffix, issue_pat, issue_server_link, issue_prefix)
1384 continue
1395
1385
1396 for pattern_index in valid_indices:
1386 # Wrap tmp_urlify_issues_f with substitution of this pattern, while making sure all loop variables (and compiled regexpes) are bound
1397 ISSUE_PATTERN = conf.get('issue_pat%s' % pattern_index)
1387 issue_re = re.compile(issue_pat)
1398 ISSUE_SERVER_LNK = conf.get('issue_server_link%s' % pattern_index)
1388 def issues_replace(match_obj,
1399 ISSUE_PREFIX = conf.get('issue_prefix%s' % pattern_index)
1389 issue_server_link=issue_server_link, issue_prefix=issue_prefix):
1390 leadingspace = ' ' if match_obj.group().startswith(' ') else ''
1391 issue_id = ''.join(match_obj.groups())
1392 issue_url = issue_server_link.replace('{id}', issue_id)
1393 issue_url = issue_url.replace('{repo}', repo_name)
1394 issue_url = issue_url.replace('{repo_name}', repo_name.split(URL_SEP)[-1])
1395 return (
1396 '%(leadingspace)s<a class="issue-tracker-link" href="%(url)s">'
1397 '%(issue-prefix)s%(id-repr)s'
1398 '</a>'
1399 ) % {
1400 'leadingspace': leadingspace,
1401 'url': issue_url,
1402 'id-repr': issue_id,
1403 'issue-prefix': issue_prefix,
1404 'serv': issue_server_link,
1405 }
1406 tmp_urlify_issues_f = (lambda s,
1407 issue_re=issue_re, issues_replace=issues_replace, chain_f=tmp_urlify_issues_f:
1408 issue_re.sub(issues_replace, chain_f(s)))
1400
1409
1401 log.debug('pattern suffix `%s` PAT:%s SERVER_LINK:%s PREFIX:%s',
1410 # Set tmp function globally - atomically
1402 pattern_index, ISSUE_PATTERN, ISSUE_SERVER_LNK,
1411 _urlify_issues_f = tmp_urlify_issues_f
1403 ISSUE_PREFIX)
1404
1405 URL_PAT = re.compile(ISSUE_PATTERN)
1406
1412
1407 urlify_issues_replace = _urlify_issues_replace_f(repo_name, ISSUE_SERVER_LNK, ISSUE_PREFIX)
1413 return _urlify_issues_f(newtext)
1408 newtext = URL_PAT.sub(urlify_issues_replace, newtext)
1409 log.debug('processed prefix:`%s` => %s', pattern_index, newtext)
1410
1411 return newtext
1412
1414
1413
1415
1414 def render_w_mentions(source, repo_name=None):
1416 def render_w_mentions(source, repo_name=None):
@@ -92,6 +92,10 b' class TestFilesController(TestController'
92 response.mustcontain("""@ r%s:%s""" % (r[0], r[1][:12]))
92 response.mustcontain("""@ r%s:%s""" % (r[0], r[1][:12]))
93
93
94 def test_file_source(self):
94 def test_file_source(self):
95 # Force the global cache to be populated now when we know the right .ini has been loaded.
96 # (Without this, the test would fail.)
97 import kallithea.lib.helpers
98 kallithea.lib.helpers._urlify_issues_f = None
95 self.log_user()
99 self.log_user()
96 response = self.app.get(url(controller='files', action='index',
100 response = self.app.get(url(controller='files', action='index',
97 repo_name=HG_REPO,
101 repo_name=HG_REPO,
General Comments 0
You need to be logged in to leave comments. Login now