Skip to content

Instantly share code, notes, and snippets.

@lxneng
Created August 14, 2012 04:45
Show Gist options
  • Save lxneng/3346315 to your computer and use it in GitHub Desktop.
Save lxneng/3346315 to your computer and use it in GitHub Desktop.
strapdownjs
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Sanitise a (unicode) string so it is suitable as a URL path
component.</title>
</head>
<body>
<xmp theme="United">
```py
# coding=utf-8
import re
import unicodedata
def cropText(text, length):
"""Crop a text at a given length. If the text is shorter than the
given length it is returned unchanged.
:param unicode text: text to crop
:param int length: maximum length of returned string
"""
if not text:
return text
if len(text) <= length:
return text
return text[:length] + u'…'
def NormalizeTitleToPath(title):
"""Sanitise a (unicode) string so it is suitable as a URL path
component."""
path = title.strip().lower()
# Normalize the unicode form
def norm(c):
category = unicodedata.category(c)
# Return normalized version of all letter and number forms
if category in ['Lu', 'Ll', 'Lt', 'Lm', 'Lo', 'Nd', 'Nl', 'No']:
return unicodedata.normalize("NFKD", c)
# Reduce all separators to a simple space
if category in ['Zs', 'Zl', 'Zp']:
return u' '
# List of allowed non-letter characters
if c in '+-':
return c
# Everything else is ignored
return u''
path = u''.join([norm(part) for part in path])
path = re.sub(r' +', u'-', path)
return path
```
</xmp>
<script src="http://strapdownjs.com/v/0.1/strapdown.js"></script>
</body>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment