initial commit where I steal code from amo
This commit is contained in:
Коммит
a082e5e847
|
@ -0,0 +1,7 @@
|
|||
*.pyo
|
||||
*.pyc
|
||||
pip-log.txt
|
||||
.coverage
|
||||
dist
|
||||
*.egg-info
|
||||
.noseids
|
|
@ -0,0 +1,28 @@
|
|||
Copyright (c) 2011, Mozilla Foundation
|
||||
All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
modification, are permitted provided that the following conditions are met:
|
||||
|
||||
1. Redistributions of source code must retain the above copyright notice,
|
||||
this list of conditions and the following disclaimer.
|
||||
|
||||
2. Redistributions in binary form must reproduce the above copyright
|
||||
notice, this list of conditions and the following disclaimer in the
|
||||
documentation and/or other materials provided with the distribution.
|
||||
|
||||
3. Neither the name of unicode-slugify nor the names of its contributors
|
||||
may be used to endorse or promote products derived from this software
|
||||
without specific prior written permission.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
||||
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
|
||||
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
||||
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
||||
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
|
||||
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||||
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
include LICENSE
|
||||
include README.rst
|
||||
|
|
@ -0,0 +1,13 @@
|
|||
# Unicode Slugify
|
||||
|
||||
Unicode Slugify is a slugifier that generates unicode slugs. It was originally
|
||||
used in the Firefox Add-ons web site to generate slugs for add-ons and add-on
|
||||
collections. Many of these add-ons and collections had unicode characters and
|
||||
required more than simple transliteration.
|
||||
|
||||
## Usage
|
||||
|
||||
>>> import slugify
|
||||
|
||||
>>> slugify.slugify(u'Bän...g (bang)')
|
||||
u'bäng-bang'
|
|
@ -0,0 +1,5 @@
|
|||
# Django is needed for smart_unicode
|
||||
django
|
||||
|
||||
# These are the requirements to run the test suite.
|
||||
nose
|
|
@ -0,0 +1,29 @@
|
|||
from setuptools import setup
|
||||
|
||||
setup(
|
||||
name='slugify',
|
||||
version='0.1',
|
||||
description='A slug generator that turns strings into unicode slugs.',
|
||||
long_description=open('README.rst').read(),
|
||||
author='Jeff Balogh, Dave Dash',
|
||||
author_email='jbalogh@mozilla.com, dd@mozilla.com',
|
||||
url='http://github.com/mozilla/unicode-slugify',
|
||||
license='BSD',
|
||||
packages=['slugify'],
|
||||
include_package_data=True,
|
||||
package_data = { '': ['README.rst'] },
|
||||
zip_safe=False,
|
||||
install_requires=['django'],
|
||||
classifiers=[
|
||||
'Development Status :: 4 - Beta',
|
||||
'Environment :: Web Environment',
|
||||
'Environment :: Web Environment :: Mozilla',
|
||||
'Intended Audience :: Developers',
|
||||
'License :: OSI Approved :: BSD License',
|
||||
'Operating System :: OS Independent',
|
||||
'Programming Language :: Python',
|
||||
'Topic :: Software Development :: Libraries :: Python Modules',
|
||||
]
|
||||
)
|
||||
|
||||
|
|
@ -0,0 +1,23 @@
|
|||
import re
|
||||
import unicodedata
|
||||
|
||||
from django.utils.encoding import smart_unicode
|
||||
|
||||
# Extra characters outside of alphanumerics that we'll allow.
|
||||
SLUG_OK = '-_~'
|
||||
|
||||
|
||||
def slugify(s, ok=SLUG_OK, lower=True, spaces=False):
|
||||
# L and N signify letter/number.
|
||||
# http://www.unicode.org/reports/tr44/tr44-4.html#GC_Values_Table
|
||||
rv = []
|
||||
for c in smart_unicode(s):
|
||||
cat = unicodedata.category(c)[0]
|
||||
if cat in 'LN' or c in ok:
|
||||
rv.append(c)
|
||||
if cat == 'Z': # space
|
||||
rv.append(' ')
|
||||
new = ''.join(rv).strip()
|
||||
if not spaces:
|
||||
new = re.sub('[-\s]+', '-', new)
|
||||
return new.lower() if lower else new
|
|
@ -0,0 +1,30 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
from nose.tools import eq_
|
||||
|
||||
from slugify import slugify
|
||||
|
||||
|
||||
u = u'Ελληνικά'
|
||||
|
||||
|
||||
def test_slugify():
|
||||
x = '-'.join([u, u])
|
||||
y = ' - '.join([u, u])
|
||||
|
||||
def check(x, y):
|
||||
eq_(slugify(x), y)
|
||||
s = [('xx x - "#$@ x', 'xx-x-x'),
|
||||
(u'Bän...g (bang)', u'bäng-bang'),
|
||||
(u, u.lower()),
|
||||
(x, x.lower()),
|
||||
(y, x.lower()),
|
||||
(' a ', 'a'),
|
||||
('tags/', 'tags'),
|
||||
('holy_wars', 'holy_wars'),
|
||||
# I don't really care what slugify returns. Just don't crash.
|
||||
(u'x𘍿', u'x'),
|
||||
(u'ϧ𘒬𘓣', u'\u03e7'),
|
||||
(u'¿x', u'x'),
|
||||
]
|
||||
for val, expected in s:
|
||||
yield check, val, expected
|
Загрузка…
Ссылка в новой задаче