slugs.py 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. """
  2. Slugs.
  3. Additional slug outputs.
  4. MIT license.
  5. Copyright (c) 2014 - 2017 Isaac Muse <isaacmuse@gmail.com>
  6. Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
  7. documentation files (the "Software"), to deal in the Software without restriction, including without limitation
  8. the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software,
  9. and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
  10. The above copyright notice and this permission notice shall be included in all copies or substantial portions
  11. of the Software.
  12. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
  13. TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  14. THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
  15. CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  16. DEALINGS IN THE SOFTWARE.
  17. """
  18. import re
  19. import unicodedata
  20. from urllib.parse import quote
  21. RE_TAGS = re.compile(r'</?[^>]*>', re.UNICODE)
  22. RE_INVALID_SLUG_CHAR = re.compile(r'[^\w\- ]', re.UNICODE)
  23. RE_SEP = re.compile(r' ', re.UNICODE)
  24. RE_ASCII_LETTERS = re.compile(r'[A-Z]', re.UNICODE)
  25. NO_CASED = 0
  26. UNICODE_CASED = 1
  27. CASED = 2
  28. def uslugify(text, sep, cased=NO_CASED, percent_encode=False):
  29. """Unicode slugify (`utf-8`)."""
  30. # Normalize, Strip html tags, strip leading and trailing whitespace, and lower
  31. slug = RE_TAGS.sub('', unicodedata.normalize('NFC', text)).strip()
  32. if cased == NO_CASED:
  33. slug = slug.lower()
  34. elif cased == UNICODE_CASED:
  35. def lower(m):
  36. """Lowercase character."""
  37. return m.group(0).lower()
  38. slug = RE_ASCII_LETTERS.sub(lower, slug)
  39. # Remove non word characters, non spaces, and non dashes, and convert spaces to dashes.
  40. slug = RE_SEP.sub(sep, RE_INVALID_SLUG_CHAR.sub('', slug))
  41. return quote(slug.encode('utf-8')) if percent_encode else slug
  42. def uslugify_encoded(text, sep):
  43. """Unicode slugify (percent encoded)."""
  44. return uslugify(text, sep, percent_encode=True)
  45. def uslugify_cased(text, sep):
  46. """Unicode slugify cased (keep case) (`utf-8`)."""
  47. return uslugify(text, sep, cased=CASED)
  48. def uslugify_cased_encoded(text, sep):
  49. """Unicode slugify cased (keep case) (percent encoded)."""
  50. return uslugify(text, sep, cased=CASED, percent_encode=True)
  51. def gfm(text, sep):
  52. """Unicode slugify cased (cased Unicode only) (`utf-8`)."""
  53. return uslugify(text, sep, cased=UNICODE_CASED)
  54. def gfm_encoded(text, sep):
  55. """Unicode slugify cased (cased Unicode only) (percent encoded)."""
  56. return uslugify(text, sep, cased=UNICODE_CASED, percent_encode=True)