changeset 5661:b97ba9b23796

helpers: introduce render_w_mentions Eventually this function should support and auto detect multiple formats and is thus not named for a specific format. But for now it is plain text only. This kind of markup can quite easily and safely support additional magic markup. It is much harder to do that on top of a richer markup format; it must essentially be done in a single pass, with both all the various regexps and the rst formatting done in a single pass.
author Mads Kiilerich <madski@unity3d.com>
date Fri, 04 Jul 2014 14:12:07 +0200
parents 072175b07608
children b60e58094fb7
files kallithea/lib/helpers.py
diffstat 1 files changed, 21 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/kallithea/lib/helpers.py	Wed Jan 20 01:47:11 2016 +0100
+++ b/kallithea/lib/helpers.py	Fri Jul 04 14:12:07 2014 +0200
@@ -55,7 +55,7 @@
 from kallithea.lib.utils import repo_name_slug, get_custom_lexer
 from kallithea.lib.utils2 import str2bool, safe_unicode, safe_str, \
     get_changeset_safe, datetime_to_time, time_to_datetime, AttributeDict, \
-    safe_int
+    safe_int, MENTIONS_REGEX
 from kallithea.lib.markup_renderer import MarkupRenderer, url_re
 from kallithea.lib.vcs.exceptions import ChangesetDoesNotExistError
 from kallithea.lib.vcs.backends.base import BaseChangeset, EmptyChangeset
@@ -1397,6 +1397,26 @@
     return literal('<div class="rst-block">%s</div>' %
                    MarkupRenderer.rst_with_mentions(source))
 
+
+def mentions_replace(match_obj):
+    return '<b>@%s</b>' % match_obj.group(1)
+
+
+def render_w_mentions(source):
+    """
+    Render plain text with @mention highlighting.
+    """
+    s = source.rstrip()
+    s = safe_unicode(s)
+    s = '\n'.join(s.splitlines())
+    s = html_escape(s)
+    # this sequence of html-ifications seems to be safe and non-conflicting
+    # if the issues regexp is sane
+    s = _urlify_text(s)
+    s = MENTIONS_REGEX.sub(mentions_replace, s)
+    return literal('<code style="white-space:pre-wrap">%s</code>' % s)
+
+
 def short_ref(ref_type, ref_name):
     if ref_type == 'rev':
         return short_id(ref_name)