src/pyams_utils/url.py
changeset 171 d6c77b2b5c4b
parent 127 0fcad7725bdc
child 189 fb23265528fb
--- a/src/pyams_utils/url.py	Thu Apr 12 11:21:59 2018 +0200
+++ b/src/pyams_utils/url.py	Fri Apr 13 12:23:18 2018 +0200
@@ -16,16 +16,27 @@
 # import standard library
 
 # import interfaces
-from persistent.interfaces import IPersistent
 from pyams_utils.interfaces.tales import ITALESExtension
 
 # import packages
 from pyams_utils.adapter import ContextRequestViewAdapter, adapter_config
+from pyams_utils.unicode import translate_string
 from pyramid.encode import url_quote, urlencode
 from pyramid.url import resource_url, QUERY_SAFE
 from zope.interface import Interface
 
 
+def generate_url(title):
+    """Generate an SEO-friendly content URL from it's title
+
+    The original title is translated to remove accents, converted to lowercase, and words
+    shorter than three characters are removed; terms are joined by hyphens.
+    """
+    return '-'.join(filter(lambda x: len(x) > 2,
+                           translate_string(title, escape_slashes=True, force_lower=True, spaces='-',
+                                            remove_punctuation=True, keep_chars='-').split('-')))
+
+
 def absolute_url(context, request, view_name=None, query=None):
     """Get resource absolute_url