Giter Club home page Giter Club logo

awesome-slugify's Introduction

awesome-slugify

image

Python flexible slugify function

Install

pip install awesome-slugify

Usage

from slugify import slugify

slugify('Any text')  # 'Any-text'

Custom slugify

from slugify import slugify, Slugify, UniqueSlugify

slugify('Any text', to_lower=True)  # 'any-text'

custom_slugify = Slugify(to_lower=True)
custom_slugify('Any text')          # 'any-text'

custom_slugify.separator = '_'
custom_slugify('Any text')          # 'any_text'

custom_slugify = UniqueSlugify()
custom_slugify('Any text')          # 'any-text'
custom_slugify('Any text')          # 'any-text-1'

slugify function optional args

to_lower              # if True convert text to lowercase
max_length            # output string max length
separator             # separator string
capitalize            # if True upper first letter

Slugify class args

pretranslate = None               # function or dict for replace before translation
translate = unidecode.unidecode   # function for slugifying or None
safe_chars = ''                   # additional safe chars
stop_words = ()                   # remove these words from slug

to_lower = False                  # default to_lower value
max_length = None                 # default max_length value
separator = '-'                   # default separator value
capitalize = False                # default capitalize value

UniqueSlugify class args

# all Slugify class args +
uids = []                         # initial unique ids

Predefined slugify functions

Some slugify functions is predefined this way:

from slugify import Slugify, CYRILLIC, GERMAN, GREEK

slugify = Slugify()
slugify_unicode = Slugify(translate=None)

slugify_url = Slugify()
slugify_url.to_lower = True
slugify_url.stop_words = ('a', 'an', 'the')
slugify_url.max_length = 200

slugify_filename = Slugify()
slugify_filename.separator = '_'
slugify_filename.safe_chars = '-.'
slugify_filename.max_length = 255

slugify_ru = Slugify(pretranslate=CYRILLIC)
slugify_de = Slugify(pretranslate=GERMAN)
slugify_el = Slugify(pretranslate=GREEK)

Examples

from slugify import Slugify, UniqueSlugify, slugify, slugify_unicode
from slugify import slugify_url, slugify_filename
from slugify import slugify_ru, slugify_de

slugify('one kožušček')                       # one-kozuscek
slugify('one two three', separator='.')       # one.two.three
slugify('one two three four', max_length=12)  # one-two-four   (12 chars)
slugify('one TWO', to_lower=True)             # one-two
slugify('one TWO', capitalize=True)           # One-TWO

slugify_filename(u'Дrаft №2.txt')             # Draft_2.txt
slugify_url(u'Дrаft №2.txt')                  # draft-2-txt

my_slugify = Slugify()
my_slugify.separator = '.'
my_slugify.pretranslate = {'я': 'i', '♥': 'love'}
my_slugify('Я ♥ борщ')                        # I.love.borshch  (custom translate)

slugify('Я ♥ борщ')                           # Ia-borshch  (standard translation)
slugify_ru('Я ♥ борщ')                        # Ya-borsch   (alternative russian translation)
slugify_unicode('Я ♥ борщ')                   # Я-борщ      (sanitize only)

slugify_de('ÜBER Über slugify')               # UEBER-Ueber-slugify

slugify_unique = UniqueSlugify(separator='_')
slugify_unique('one TWO')                     # One_TWO
slugify_unique('one TWO')                     # One_TWO_1

slugify_unique = UniqueSlugify(uids=['cellar-door'])
slugify_unique('cellar door')                 # cellar-door-1

Custom Unique Slugify Checker

from slugify import UniqueSlugify

def my_unique_check(text, uids):
    if text in uids:
        return False
    return not SomeDBClass.objects.filter(slug_field=text).exists()

custom_slugify_unique = UniqueSlugify(unique_check=my_unique_check)

# Checks the database for a matching document
custom_slugify_unique('te occidere possunt')

Running UnitTests

$ virtualenv venv
$ venv/bin/pip install -r requirements.txt
$ venv/bin/nosetests slugify

awesome-slugify's People

Contributors

facciocose avatar gthole avatar jmcarp avatar jonafato avatar jpadilla avatar sgeulette avatar srault95 avatar voronind avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

awesome-slugify's Issues

The @ sign is ignored

slugify('This wonderful event @ the cool place')
u'This-wonderful-event-the-cool-place'

I suggest to slugify the @ to an 'at'
u'This-wonderful-event-at-the-cool-place'

Kindest of Regards;

Add a LICENSE

In order for others to contribute or use this work, it would be handy to have a LICENSE file declaring, under which conditions it can be included.

You can, for example, use Choose A License service to guide you with your choice ;-)

Python 2.6 supprt

Seems I can't run it at all in python 2.6

Python 2.6.6 (r266:84292, Dec  5 2011, 09:38:23)
[GCC 4.2.1 (Based on Apple Inc. build 5658) (LLVM build 2336.1.00)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import slugify
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/peterbe/virtualenvs/airmozilla/lib/python2.6/site-packages/slugify/__init__.py", line 1, in <module>
    from .main import slugify, slugify_unicode, slugify_ru, get_slugify
  File "/Users/peterbe/virtualenvs/airmozilla/lib/python2.6/site-packages/slugify/main.py", line 100, in <module>
    slugify_ru = get_slugify(pretranslate=ALT_CYRILLIC)
  File "/Users/peterbe/virtualenvs/airmozilla/lib/python2.6/site-packages/slugify/main.py", line 78, in get_slugify
    pretranslate = get_pretranslate(pretranslate)
  File "/Users/peterbe/virtualenvs/airmozilla/lib/python2.6/site-packages/slugify/main.py", line 35, in get_pretranslate
    PRETRANSLATE = u'({})'.format('|'.join(map(re.escape, translate_dict)))
ValueError: zero length field name in format
>>>

Fix the import path for get_slugify

At the moment, I can't do:

from slugify import get_slugify

I get an import error because of how slugify.__init__.py is defined. Instead I have to do the following:

from slugify.main import get_slugify

I would like to either submit a patch to slugify.__init__.py or correct the documentation. @dimka665, what approach would you prefer?

Fix DeprecationWarnings in newer Pythons (3.6+)

I see these warnings each time I run:

...python3.6/site-packages/slugify/main.py:65
  ...python3.6/site-packages/slugify/main.py:65: DeprecationWarning: invalid escape sequence \p
    '''

...python3.6/site-packages/slugify/main.py:98
  ...python3.6/site-packages/slugify/main.py:98: DeprecationWarning: invalid escape sequence \L
    PRETRANSLATE = re.compile(u'(\L<options>)', options=convert_dict)

...python3.6/site-packages/slugify/main.py:140
  ...python3.6/site-packages/slugify/main.py:140: DeprecationWarning: invalid escape sequence \p
    unwanted_chars_re = u'[^\p{{AlNum}}{safe_chars}]+'.format(safe_chars=re.escape(self._safe_chars or ''))

...python3.6/site-packages/slugify/main.py:144
  ...python3.6/site-packages/slugify/main.py:144: DeprecationWarning: invalid escape sequence \p
    unwanted_chars_and_words_re = unwanted_chars_re + u'|(?<!\p{AlNum})(?:\L<stop_words>)(?!\p{AlNum})'

Perhaps this is the problem:
https://stackoverflow.com/questions/50504500/deprecationwarning-invalid-escape-sequence-what-to-use-instead-of-d

not working in python 3

Please make this library compatible with Python 3. Right now it doesn't work because of at least several syntax errors in some string literals, and an iteration over a changing dict issue.

Exception StopIteration on empty strings '' with max_length or separator

>>> from slugify import slugify
>>> slugify('', max_length=40, separator='_')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python2.7/dist-packages/slugify/main.py", line 108, in slugify
    text = join_words(words, separator, max_length)
  File "/usr/lib/python2.7/dist-packages/slugify/main.py", line 85, in join_words
    text = next(words)    # text = words.pop(0)
StopIteration

Incorrect Japanese transliteration for っ

っ is U+3063 HIRAGANA LETTER SMALL TU, which is different than つ U+3064 HIRAGANA LETTER TU in that the phonetic transliteration of it is a glottal stop; the English equivalent is doubling the consonant-sound of the next mora.

For example, ほっこり should be transliterated as 'hokkori', but awesome-slugify incorrectly renders it 'hotsukori' (as if the っ were a つ):

>>> slugify('ほっこり')
'hotsukori'

See also https://translate.google.com/?sl=ja&tl=en&text=%E3%81%BB%E3%81%A3%E3%81%93%E3%82%8A&op=translate for the use of this character (and https://translate.google.com/?sl=ja&tl=en&text=%E3%81%BB%E3%81%A4%E3%81%93%E3%82%8A%0A%0A&op=translate to see what the large tsu does instead).

Fix for import on Python 2.7.9

awesome-slugify 1.6.5

Traceback (most recent call last):
  File "py_parsers.py", line 12, in <module>
    from slugify import slugify
  File "/usr/local/lib/python2.7/dist-packages/slugify/__init__.py", line 2, in <module>
    from slugify.main import Slugify, UniqueSlugify
ImportError: No module named main

add UNWANTED_CHARS to get_slugify

Hi,

I use this module for slugify filename
Example:

  • With this module: ừ-ắ_.txt -> u.a..txt
  • But I want: u-a_.txt (keep some special char like -_.)

When I look into your codes, I see that I must change UNWANTED_CHARS to make this work
So I think, you should add UNWANTED_CHARS to input of get_slugify
It's my opinion

Thank you

Unidecode 1.0.22

Hello,

Unidecode was updated today. I was wondering if there are plans to relax the Unidecode dependency for awesome-slugify

Carlos

UniqueSlugify will exceed max_length if it adds digits to make slug unique

Python 3.6.4 (default, Mar  9 2018, 23:15:03)
[GCC 4.2.1 Compatible Apple LLVM 9.0.0 (clang-900.0.39.2)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from slugify import UniqueSlugify
>>> s = UniqueSlugify(to_lower=True, max_length=3)
>>> s("Hello World")
'hel'
>>> s("Hello World")
'hel-1'

I would expect something like hel, h-1, etc.

I like to conver : to - but - to _ but not : to _

{':': '-'}
I like to conver : to - but - to _ but not : to _
Or equivalent
I like to conver (: -> - ) but (- -> _) but not (: -> _)

I have a problem for undesirable conversion.
I think (separator="_") works after translate and pretranslate and if works in pretranslate -> separator -> translate problem is solved.
Note: i don't know why translate need a funtion and don't work with a dict.

Separator convert ":" to a "" (pre) and (translate) don`t prevent this.
(safe_chars=":") prevent ":" -> "
" conversion but then conversion ":" -> "-" is made and conversion "-" -> "" is made.
Eventualy undesirable conversion ":" -> "
" is made.
I can prevent this adding "-" to safe_chars=, but i like to convert "-" -> "", But Not ":" -> ""

I think separator should ignore pre or translate.keys()

I hope i'm sucesfull explaining.

Thanks for awesome-slugify, is awesome :) and with interesting features.

Add Travis CI

It'd be awesome to have this repo setup with Travis CI to automatically run tests. Tests will run on Pull Requests as well letting you know if they passed tests and are safe to merge. Having their little badge in the README really gives confidence to other developers.

Something like this should work:

language: python

python:
  - '2.7'
  - '3.3'

install:
  - pip install -r requirements.txt

script:
  - python slugify/tests.py

Fix for Python 2.7.10

Python 2.7.10 (default, Jul 14 2015, 19:46:27)
[GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.39)] on darwin
Type "help", "copyright", "credits" or "license" for more information.

import slugify
Traceback (most recent call last):
File "", line 1, in
File "/Library/Python/2.7/site-packages/slugify/init.py", line 2, in
from slugify.main import Slugify, UniqueSlugify
ImportError: No module named main

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.