[ckan-changes] commit/ckan: amercader: [lib] Add back munge_title_to_name function, used in some extensions

Bitbucket commits-noreply at bitbucket.org
Tue Oct 25 17:45:02 UTC 2011


1 new commit in ckan:


https://bitbucket.org/okfn/ckan/changeset/db8c9f64bb3d/
changeset:   db8c9f64bb3d
branch:      release-v1.5
user:        amercader
date:        2011-10-25 19:44:39
summary:     [lib] Add back munge_title_to_name function, used in some extensions
affected #:  1 file

diff -r 945e2a6ce74fc1aaed058346da5b9127162d6d89 -r db8c9f64bb3d5df069a0ff53d6cef43370cbda7d ckan/lib/munge.py
--- a/ckan/lib/munge.py
+++ b/ckan/lib/munge.py
@@ -19,6 +19,33 @@
     name = re.sub('[^a-zA-Z0-9-_]', '', name).lower()
     return name
 
+def munge_title_to_name(name):
+    '''Munge a title into a name.
+    '''
+    # remove foreign accents
+    if isinstance(name, unicode):
+        name = substitute_ascii_equivalents(name)
+    # convert spaces and separators
+    name = re.sub('[ .:/]', '-', name)
+    # take out not-allowed characters
+    name = re.sub('[^a-zA-Z0-9-_]', '', name).lower()
+    # remove doubles
+    name = re.sub('--', '-', name)
+    # remove leading or trailing hyphens
+    name = name.strip('-')
+    # if longer than max_length, keep last word if a year
+    max_length = model.PACKAGE_NAME_MAX_LENGTH - 5
+    # (make length less than max, in case we need a few for '_' chars
+    # to de-clash names.)
+    if len(name) > max_length:
+        year_match = re.match('.*?[_-]((?:\d{2,4}[-/])?\d{2,4})$', name)
+        if year_match:
+            year = year_match.groups()[0]
+            name = '%s-%s' % (name[:(max_length-len(year)-1)], year)
+        else:
+            name = name[:max_length]
+    return name
+
 def substitute_ascii_equivalents(text_unicode):
     # Method taken from: http://code.activestate.com/recipes/251871/
     """This takes a UNICODE string and replaces Latin-1 characters with

Repository URL: https://bitbucket.org/okfn/ckan/

--

This is a commit notification from bitbucket.org. You are receiving
this because you have the service enabled, addressing the recipient of
this email.




More information about the ckan-changes mailing list