comparison rhodecode/lib/helpers.py @ 2165:dc2584ba5fbc

merged beta into default branch
author Marcin Kuzminski <marcin@python-works.com>
date Wed, 28 Mar 2012 19:54:16 +0200
parents 82a88013a3fd 04d3fae0e4c0
children a437a986d399
comparison
equal deleted inserted replaced
2097:8fd6650bb436 2165:dc2584ba5fbc
37 from webhelpers.html.tags import _set_input_attrs, _set_id_attr, \ 37 from webhelpers.html.tags import _set_input_attrs, _set_id_attr, \
38 convert_boolean_attrs, NotGiven, _make_safe_id_component 38 convert_boolean_attrs, NotGiven, _make_safe_id_component
39 39
40 from rhodecode.lib.annotate import annotate_highlight 40 from rhodecode.lib.annotate import annotate_highlight
41 from rhodecode.lib.utils import repo_name_slug 41 from rhodecode.lib.utils import repo_name_slug
42 from rhodecode.lib import str2bool, safe_unicode, safe_str, get_changeset_safe 42 from rhodecode.lib.utils2 import str2bool, safe_unicode, safe_str, \
43 get_changeset_safe
43 from rhodecode.lib.markup_renderer import MarkupRenderer 44 from rhodecode.lib.markup_renderer import MarkupRenderer
44 45
45 log = logging.getLogger(__name__) 46 log = logging.getLogger(__name__)
47
48
49 def shorter(text, size=20):
50 postfix = '...'
51 if len(text) > size:
52 return text[:size - len(postfix)] + postfix
53 return text
46 54
47 55
48 def _reset(name, value=None, id=NotGiven, type="reset", **attrs): 56 def _reset(name, value=None, id=NotGiven, type="reset", **attrs):
49 """ 57 """
50 Reset button 58 Reset button
65 73
66 :param raw_id: 74 :param raw_id:
67 :param path: 75 :param path:
68 """ 76 """
69 77
70 return 'C-%s-%s' % (short_id(raw_id), md5(path).hexdigest()[:12]) 78 return 'C-%s-%s' % (short_id(raw_id), md5(safe_str(path)).hexdigest()[:12])
71 79
72 80
73 def get_token(): 81 def get_token():
74 """Return the current authentication token, creating one if one doesn't 82 """Return the current authentication token, creating one if one doesn't
75 already exist. 83 already exist.
84 session[token_key] = token 92 session[token_key] = token
85 if hasattr(session, 'save'): 93 if hasattr(session, 'save'):
86 session.save() 94 session.save()
87 return session[token_key] 95 return session[token_key]
88 96
97
89 class _GetError(object): 98 class _GetError(object):
90 """Get error from form_errors, and represent it as span wrapped error 99 """Get error from form_errors, and represent it as span wrapped error
91 message 100 message
92 101
93 :param field_name: field to fetch errors for 102 :param field_name: field to fetch errors for
99 if form_errors and form_errors.has_key(field_name): 108 if form_errors and form_errors.has_key(field_name):
100 return literal(tmpl % form_errors.get(field_name)) 109 return literal(tmpl % form_errors.get(field_name))
101 110
102 get_error = _GetError() 111 get_error = _GetError()
103 112
113
104 class _ToolTip(object): 114 class _ToolTip(object):
105 115
106 def __call__(self, tooltip_title, trim_at=50): 116 def __call__(self, tooltip_title, trim_at=50):
107 """Special function just to wrap our text into nice formatted 117 """Special function just to wrap our text into nice formatted
108 autowrapped text 118 autowrapped text
109 119
110 :param tooltip_title: 120 :param tooltip_title:
111 """ 121 """
112 return escape(tooltip_title) 122 return escape(tooltip_title)
113 tooltip = _ToolTip() 123 tooltip = _ToolTip()
124
114 125
115 class _FilesBreadCrumbs(object): 126 class _FilesBreadCrumbs(object):
116 127
117 def __call__(self, repo_name, rev, paths): 128 def __call__(self, repo_name, rev, paths):
118 if isinstance(paths, str): 129 if isinstance(paths, str):
134 145
135 return literal('/'.join(url_l)) 146 return literal('/'.join(url_l))
136 147
137 files_breadcrumbs = _FilesBreadCrumbs() 148 files_breadcrumbs = _FilesBreadCrumbs()
138 149
150
139 class CodeHtmlFormatter(HtmlFormatter): 151 class CodeHtmlFormatter(HtmlFormatter):
140 """My code Html Formatter for source codes 152 """
153 My code Html Formatter for source codes
141 """ 154 """
142 155
143 def wrap(self, source, outfile): 156 def wrap(self, source, outfile):
144 return self._wrap_div(self._wrap_pre(self._wrap_code(source))) 157 return self._wrap_div(self._wrap_pre(self._wrap_code(source)))
145 158
317 330
318 #============================================================================== 331 #==============================================================================
319 # SCM FILTERS available via h. 332 # SCM FILTERS available via h.
320 #============================================================================== 333 #==============================================================================
321 from rhodecode.lib.vcs.utils import author_name, author_email 334 from rhodecode.lib.vcs.utils import author_name, author_email
322 from rhodecode.lib import credentials_filter, age as _age 335 from rhodecode.lib.utils2 import credentials_filter, age as _age
323 from rhodecode.model.db import User 336 from rhodecode.model.db import User
324 337
325 age = lambda x: _age(x) 338 age = lambda x: _age(x)
326 capitalize = lambda x: x.capitalize() 339 capitalize = lambda x: x.capitalize()
327 email = author_email 340 email = author_email
757 770
758 a_v = a if a > 0 else '' 771 a_v = a if a > 0 else ''
759 d_v = d if d > 0 else '' 772 d_v = d if d > 0 else ''
760 773
761 def cgen(l_type): 774 def cgen(l_type):
762 mapping = {'tr': 'top-right-rounded-corner', 775 mapping = {'tr': 'top-right-rounded-corner-mid',
763 'tl': 'top-left-rounded-corner', 776 'tl': 'top-left-rounded-corner-mid',
764 'br': 'bottom-right-rounded-corner', 777 'br': 'bottom-right-rounded-corner-mid',
765 'bl': 'bottom-left-rounded-corner'} 778 'bl': 'bottom-left-rounded-corner-mid'}
766 map_getter = lambda x: mapping[x] 779 map_getter = lambda x: mapping[x]
767 780
768 if l_type == 'a' and d_v: 781 if l_type == 'a' and d_v:
769 #case when added and deleted are present 782 #case when added and deleted are present
770 return ' '.join(map(map_getter, ['tl', 'bl'])) 783 return ' '.join(map(map_getter, ['tl', 'bl']))
799 812
800 return literal(url_pat.sub(url_func, text_)) 813 return literal(url_pat.sub(url_func, text_))
801 814
802 815
803 def urlify_changesets(text_, repository): 816 def urlify_changesets(text_, repository):
817 """
818 Extract revision ids from changeset and make link from them
819
820 :param text_:
821 :param repository:
822 """
804 import re 823 import re
805 URL_PAT = re.compile(r'([0-9a-fA-F]{12,})') 824 URL_PAT = re.compile(r'([0-9a-fA-F]{12,})')
806 825
807 def url_func(match_obj): 826 def url_func(match_obj):
808 rev = match_obj.groups()[0] 827 rev = match_obj.groups()[0]
837 :param link_: changeset link 856 :param link_: changeset link
838 """ 857 """
839 import re 858 import re
840 import traceback 859 import traceback
841 860
842 # urlify changesets 861 def escaper(string):
843 text_ = urlify_changesets(text_, repository) 862 return string.replace('<', '&lt;').replace('>', '&gt;')
844 863
845 def linkify_others(t, l): 864 def linkify_others(t, l):
846 urls = re.compile(r'(\<a.*?\<\/a\>)',) 865 urls = re.compile(r'(\<a.*?\<\/a\>)',)
847 links = [] 866 links = []
848 for e in urls.split(t): 867 for e in urls.split(t):
850 links.append('<a class="message-link" href="%s">%s</a>' % (l, e)) 869 links.append('<a class="message-link" href="%s">%s</a>' % (l, e))
851 else: 870 else:
852 links.append(e) 871 links.append(e)
853 872
854 return ''.join(links) 873 return ''.join(links)
874
875
876 # urlify changesets - extrac revisions and make link out of them
877 text_ = urlify_changesets(escaper(text_), repository)
878
855 try: 879 try:
856 conf = config['app_conf'] 880 conf = config['app_conf']
857 881
858 URL_PAT = re.compile(r'%s' % conf.get('issue_pat')) 882 URL_PAT = re.compile(r'%s' % conf.get('issue_pat'))
859 883