# HG changeset patch # User Mads Kiilerich # Date 1473115878 -7200 # Node ID ca830f9d01a8d38dd7486cb64c7bfd0266b4a557 # Parent 4e2e6371f79a1b48c8b00c8db007184054aac5d2 helpers: inline url markup in urlify_text We inline it so we eventually can match all patterns in the same regexp and thus avoid problems with parsing of formatted html. Inlining it will also make repo_name and other parameters easily available. diff -r 4e2e6371f79a -r ca830f9d01a8 kallithea/lib/helpers.py --- a/kallithea/lib/helpers.py Tue Sep 06 00:51:18 2016 +0200 +++ b/kallithea/lib/helpers.py Tue Sep 06 00:51:18 2016 +0200 @@ -1268,16 +1268,12 @@ return literal('
%s%s
' % (width, d_a, d_d)) -def _urlify_text_replace(match_obj): - url_full = match_obj.group(1) - return '%(url)s' % {'url': url_full} - +_URLIFY_RE = re.compile(r''' +# URL markup +(?P%s) +''' % (url_re.pattern), + re.VERBOSE | re.MULTILINE | re.IGNORECASE) -def _urlify_text(s): - """ - Extract urls from text and make html links out of them - """ - return url_re.sub(_urlify_text_replace, s) def urlify_text(s, repo_name=None, link_=None, truncate=None, stylize=False, truncatef=truncate): @@ -1289,6 +1285,19 @@ Issues are linked to given issue-server. If link_ is provided, all text not already linking somewhere will link there. """ + + def _replace(match_obj): + url = match_obj.group('url') + if url is not None: + return '%(url)s' % {'url': url} + return match_obj.group(0) + + def _urlify(s): + """ + Extract urls from text and make html links out of them + """ + return _URLIFY_RE.sub(_replace, s) + if truncate is None: s = s.rstrip() else: @@ -1298,7 +1307,7 @@ s = urlify_changesets(s, repo_name) if stylize: s = desc_stylize(s) - s = _urlify_text(s) + s = _urlify(s) if repo_name is not None: s = urlify_issues(s, repo_name, link_) s = MENTIONS_REGEX.sub(_mentions_replace, s) diff -r 4e2e6371f79a -r ca830f9d01a8 kallithea/lib/markup_renderer.py --- a/kallithea/lib/markup_renderer.py Tue Sep 06 00:51:18 2016 +0200 +++ b/kallithea/lib/markup_renderer.py Tue Sep 06 00:51:18 2016 +0200 @@ -35,8 +35,9 @@ log = logging.getLogger(__name__) -url_re = re.compile(r'''(\bhttps?://(?:[\da-zA-Z0-9@:.-]+)''' - r'''(?:[/a-zA-Z0-9_=@#~&+%.,:;?!*()-]*[/a-zA-Z0-9_=@#~])?)''') +url_re = re.compile(r'''\bhttps?://(?:[\da-zA-Z0-9@:.-]+)''' + r'''(?:[/a-zA-Z0-9_=@#~&+%.,:;?!*()-]*[/a-zA-Z0-9_=@#~])?''') + class MarkupRenderer(object): RESTRUCTUREDTEXT_DISALLOWED_DIRECTIVES = ['include', 'meta', 'raw'] @@ -132,7 +133,7 @@ source = newline.join(source.splitlines()) def url_func(match_obj): - url_full = match_obj.groups()[0] + url_full = match_obj.group(0) return '%(url)s' % ({'url': url_full}) source = url_re.sub(url_func, source) return '
' + source.replace("\n", '
')