diff options
author | Val Neekman <val@neekware.com> | 2015-04-11 22:03:34 -0400 |
---|---|---|
committer | Val Neekman <val@neekware.com> | 2015-04-11 22:03:34 -0400 |
commit | f90ecf1905c9d06f32e676a2dcaff311f82df868 (patch) | |
tree | adb49e182792a29fb3381425262beee8484c91ec | |
parent | f64f1a3e84a73d54def5f89130c07a7b0814424e (diff) | |
download | python-slugify-f90ecf1905c9d06f32e676a2dcaff311f82df868.tar.gz |
remove 2to3 depenancy, add save word order
-rw-r--r-- | .travis.yml | 6 | ||||
-rw-r--r-- | CHANGELOG.md | 8 | ||||
-rw-r--r-- | README.md | 24 | ||||
-rw-r--r-- | slugify/__init__.py | 7 | ||||
-rw-r--r-- | slugify/slugify.py | 32 | ||||
-rw-r--r-- | test.py | 3 |
6 files changed, 53 insertions, 27 deletions
diff --git a/.travis.yml b/.travis.yml index d51b361..d6ee375 100644 --- a/.travis.yml +++ b/.travis.yml @@ -6,10 +6,8 @@ python: - "3.3" - "3.4" - pypy + install: - pip install -q -r requirements.txt --use-mirrors -before_script: - - if [[ $TRAVIS_PYTHON_VERSION == '3.2' ]]; then 2to3 --no-diffs --write --nobackups slugify; fi - - if [[ $TRAVIS_PYTHON_VERSION == '3.3' ]]; then 2to3 --no-diffs --write --nobackups slugify; fi - - if [[ $TRAVIS_PYTHON_VERSION == '3.4' ]]; then 2to3 --no-diffs --write --nobackups slugify; fi + script: python test.py diff --git a/CHANGELOG.md b/CHANGELOG.md index b2bd1af..966cb5d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,11 @@ +## 0.1.1 + +Enhancement: + + - Added more test + - Added option to save word order + - Removed 2to3 dependency + ## 0.1.0 Enhancement: @@ -89,6 +89,30 @@ How to use txt = 'jaja---lol-méméméoo--a' r = slugify(txt, max_length=20, word_boundary=True, separator="ZZZZZZ") self.assertEqual(r, "jajaZZZZZZlolZZZZZZmememeooZZZZZZa") + + txt = "___This is a test ---" + r = slugify(txt) + self.assertEqual(r, "this-is-a-test") + + txt = "___This is a test___" + r = slugify(txt) + self.assertEqual(r, "this-is-a-test") + + txt = 'one two three four five' + r = slugify(txt, max_length=13, word_boundary=True, save_order=True) + self.assertEqual(r, "one-two-three") + + txt = 'one two three four five' + r = slugify(txt, max_length=13, word_boundary=True, save_order=False) + self.assertEqual(r, "one-two-three") + + txt = 'one two three four five' + r = slugify(txt, max_length=12, word_boundary=True, save_order=False) + self.assertEqual(r, "one-two-four") + + txt = 'one two three four five' + r = slugify(txt, max_length=12, word_boundary=True, save_order=True) + self.assertEqual(r, "one-two") ``` Running the tests diff --git a/slugify/__init__.py b/slugify/__init__.py index a0b2249..08f721a 100644 --- a/slugify/__init__.py +++ b/slugify/__init__.py @@ -1,5 +1,6 @@ -# -*- coding: utf-8 -*- +from .slugify import * -__version__ = '0.1.0' -from slugify import * +__author__ = 'Val Neekman @ Neekware Inc. [@vneekman]' +__description__ = 'A Python slugify application that also handles Unicode' +__version__ = '0.1.1' diff --git a/slugify/slugify.py b/slugify/slugify.py index aae4622..2cc7db2 100644 --- a/slugify/slugify.py +++ b/slugify/slugify.py @@ -1,14 +1,18 @@ -# -*- coding: utf-8 -*- - -__all__ = ['slugify'] - import re import unicodedata import types import sys -from htmlentitydefs import name2codepoint +try: + from htmlentitydefs import name2codepoint +except ImportError: + from html.entities import name2codepoint + from unidecode import unidecode + +__all__ = ['slugify'] + + # character entity reference CHAR_ENTITY_REXP = re.compile('&(%s);' % '|'.join(name2codepoint)) @@ -78,17 +82,9 @@ def slugify(text, entities=True, decimal=True, hexadecimal=True, max_length=0, w :return (str): """ - # text to unicode - if not isinstance(text, types.UnicodeType): - text = unicode(text, 'utf-8', 'ignore') - - # decode unicode ( 影師嗎 = Ying Shi Ma) + # decode unicode text = unidecode(text) - # text back to unicode - if not isinstance(text, types.UnicodeType): - text = unicode(text, 'utf-8', 'ignore') - # character entity reference if entities: text = CHAR_ENTITY_REXP.sub(lambda m: unichr(name2codepoint[m.group(1)]), text) @@ -131,7 +127,7 @@ def slugify(text, entities=True, decimal=True, hexadecimal=True, max_length=0, w def main(): if len(sys.argv) < 2: - print "Usage %s TEXT TO SLUGIFY" % sys.argv[0] - return - text = ' '.join(sys.argv[1:]) - print slugify(text)
\ No newline at end of file + print("Usage %s TEXT TO SLUGIFY" % sys.argv[0]) + else: + text = ' '.join(sys.argv[1:]) + print(slugify(text)) @@ -96,6 +96,5 @@ class TestSequenceFunctions(unittest.TestCase): r = slugify(txt, max_length=12, word_boundary=True, save_order=True) self.assertEqual(r, "one-two") - if __name__ == '__main__': - unittest.main()
\ No newline at end of file + unittest.main() |