diff --git a/rhodecode/lib/utils.py b/rhodecode/lib/utils.py --- a/rhodecode/lib/utils.py +++ b/rhodecode/lib/utils.py @@ -29,15 +29,17 @@ import os import logging import datetime import traceback +import paste +import beaker + +from paste.script.command import Command, BadCommand from UserDict import DictMixin from mercurial import ui, config, hg from mercurial.error import RepoError -import paste -import beaker -from paste.script.command import Command, BadCommand +from webhelpers.text import collapse, remove_formatting, strip_tags from vcs.backends.base import BaseChangeset from vcs.utils.lazy import LazyProperty @@ -51,6 +53,38 @@ from rhodecode.model.user import UserMod log = logging.getLogger(__name__) +def recursive_replace(str, replace=' '): + """Recursive replace of given sign to just one instance + + :param str: given string + :param replace: char to find and replace multiple instances + + Examples:: + >>> recursive_replace("Mighty---Mighty-Bo--sstones",'-') + 'Mighty-Mighty-Bo-sstones' + """ + + if str.find(replace * 2) == -1: + return str + else: + str = str.replace(replace * 2, replace) + return recursive_replace(str, replace) + +def repo_name_slug(value): + """Return slug of name of repository + This function is called on each creation/modification + of repository to prevent bad names in repo + """ + + slug = remove_formatting(value) + slug = strip_tags(slug) + + for c in """=[]\;'"<>,/~!@#$%^&*()+{}|: """: + slug = slug.replace(c, '-') + slug = recursive_replace(slug, '-') + slug = collapse(slug, '-') + return slug + def get_repo_slug(request): return request.environ['pylons.routes_dict'].get('repo_name')