METADATA 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. Metadata-Version: 2.1
  2. Name: python-slugify
  3. Version: 8.0.4
  4. Summary: A Python slugify application that also handles Unicode
  5. Home-page: https://github.com/un33k/python-slugify
  6. Author: Val Neekman
  7. Author-email: info@neekware.com
  8. License: MIT
  9. Classifier: Development Status :: 5 - Production/Stable
  10. Classifier: Intended Audience :: Developers
  11. Classifier: Natural Language :: English
  12. Classifier: License :: OSI Approved :: MIT License
  13. Classifier: Programming Language :: Python
  14. Classifier: Programming Language :: Python :: 3
  15. Classifier: Programming Language :: Python :: 3.7
  16. Classifier: Programming Language :: Python :: 3.8
  17. Classifier: Programming Language :: Python :: 3.9
  18. Classifier: Programming Language :: Python :: 3.10
  19. Classifier: Programming Language :: Python :: 3.11
  20. Classifier: Programming Language :: Python :: 3.12
  21. Requires-Python: >=3.7
  22. Description-Content-Type: text/markdown
  23. License-File: LICENSE
  24. Requires-Dist: text-unidecode (>=1.3)
  25. Provides-Extra: unidecode
  26. Requires-Dist: Unidecode (>=1.1.1) ; extra == 'unidecode'
  27. # Python Slugify
  28. **A Python slugify application that handles unicode**.
  29. [![status-image]][status-link]
  30. [![version-image]][version-link]
  31. [![coverage-image]][coverage-link]
  32. # Overview
  33. **Best attempt** to create slugs from unicode strings while keeping it **DRY**.
  34. # Notice
  35. This module, by default installs and uses [text-unidecode](https://github.com/kmike/text-unidecode) _(GPL & Perl Artistic)_ for its decoding needs.
  36. However, there is an alternative decoding package called [Unidecode](https://github.com/avian2/unidecode) _(GPL)_. It can be installed as `python-slugify[unidecode]` for those who prefer it. `Unidecode` is believed to be more [advanced](https://github.com/un33k/python-slugify/wiki/Python-Slugify-Wiki#notes-on-unidecode).
  37. ### `Official` Support Matrix
  38. | Python | Slugify |
  39. | -------------- | ------------------ |
  40. | `>= 2.7 < 3.6` | `< 5.0.0` |
  41. | `>= 3.6 < 3.7` | `>= 5.0.0 < 7.0.0` |
  42. | `>= 3.7` | `>= 7.0.0` |
  43. # How to install
  44. easy_install python-slugify |OR| easy_install python-slugify[unidecode]
  45. -- OR --
  46. pip install python-slugify |OR| pip install python-slugify[unidecode]
  47. # Options
  48. ```python
  49. def slugify(
  50. text: str,
  51. entities: bool = True,
  52. decimal: bool = True,
  53. hexadecimal: bool = True,
  54. max_length: int = 0,
  55. word_boundary: bool = False,
  56. separator: str = DEFAULT_SEPARATOR,
  57. save_order: bool = False,
  58. stopwords: Iterable[str] = (),
  59. regex_pattern: str | None = None,
  60. lowercase: bool = True,
  61. replacements: Iterable[Iterable[str]] = (),
  62. allow_unicode: bool = False,
  63. ) -> str:
  64. """
  65. Make a slug from the given text.
  66. :param text (str): initial text
  67. :param entities (bool): converts html entities to unicode (foo &amp; bar -> foo-bar)
  68. :param decimal (bool): converts html decimal to unicode (&#381; -> Ž -> z)
  69. :param hexadecimal (bool): converts html hexadecimal to unicode (&#x17D; -> Ž -> z)
  70. :param max_length (int): output string length
  71. :param word_boundary (bool): truncates to end of full words (length may be shorter than max_length)
  72. :param save_order (bool): if parameter is True and max_length > 0 return whole words in the initial order
  73. :param separator (str): separator between words
  74. :param stopwords (iterable): words to discount
  75. :param regex_pattern (str): regex pattern for disallowed characters
  76. :param lowercase (bool): activate case sensitivity by setting it to False
  77. :param replacements (iterable): list of replacement rules e.g. [['|', 'or'], ['%', 'percent']]
  78. :param allow_unicode (bool): allow unicode characters
  79. :return (str): slugify text
  80. """
  81. ```
  82. # How to use
  83. ```python
  84. from slugify import slugify
  85. txt = "This is a test ---"
  86. r = slugify(txt)
  87. self.assertEqual(r, "this-is-a-test")
  88. txt = '影師嗎'
  89. r = slugify(txt)
  90. self.assertEqual(r, "ying-shi-ma")
  91. txt = '影師嗎'
  92. r = slugify(txt, allow_unicode=True)
  93. self.assertEqual(r, "影師嗎")
  94. txt = 'C\'est déjà l\'été.'
  95. r = slugify(txt)
  96. self.assertEqual(r, "c-est-deja-l-ete")
  97. txt = 'Nín hǎo. Wǒ shì zhōng guó rén'
  98. r = slugify(txt)
  99. self.assertEqual(r, "nin-hao-wo-shi-zhong-guo-ren")
  100. txt = 'Компьютер'
  101. r = slugify(txt)
  102. self.assertEqual(r, "kompiuter")
  103. txt = 'jaja---lol-méméméoo--a'
  104. r = slugify(txt, max_length=9)
  105. self.assertEqual(r, "jaja-lol")
  106. txt = 'jaja---lol-méméméoo--a'
  107. r = slugify(txt, max_length=15, word_boundary=True)
  108. self.assertEqual(r, "jaja-lol-a")
  109. txt = 'jaja---lol-méméméoo--a'
  110. r = slugify(txt, max_length=20, word_boundary=True, separator=".")
  111. self.assertEqual(r, "jaja.lol.mememeoo.a")
  112. txt = 'one two three four five'
  113. r = slugify(txt, max_length=13, word_boundary=True, save_order=True)
  114. self.assertEqual(r, "one-two-three")
  115. txt = 'the quick brown fox jumps over the lazy dog'
  116. r = slugify(txt, stopwords=['the'])
  117. self.assertEqual(r, 'quick-brown-fox-jumps-over-lazy-dog')
  118. txt = 'the quick brown fox jumps over the lazy dog in a hurry'
  119. r = slugify(txt, stopwords=['the', 'in', 'a', 'hurry'])
  120. self.assertEqual(r, 'quick-brown-fox-jumps-over-lazy-dog')
  121. txt = 'thIs Has a stopword Stopword'
  122. r = slugify(txt, stopwords=['Stopword'], lowercase=False)
  123. self.assertEqual(r, 'thIs-Has-a-stopword')
  124. txt = "___This is a test___"
  125. regex_pattern = r'[^-a-z0-9_]+'
  126. r = slugify(txt, regex_pattern=regex_pattern)
  127. self.assertEqual(r, "___this-is-a-test___")
  128. txt = "___This is a test___"
  129. regex_pattern = r'[^-a-z0-9_]+'
  130. r = slugify(txt, separator='_', regex_pattern=regex_pattern)
  131. self.assertNotEqual(r, "_this_is_a_test_")
  132. txt = '10 | 20 %'
  133. r = slugify(txt, replacements=[['|', 'or'], ['%', 'percent']])
  134. self.assertEqual(r, "10-or-20-percent")
  135. txt = 'ÜBER Über German Umlaut'
  136. r = slugify(txt, replacements=[['Ü', 'UE'], ['ü', 'ue']])
  137. self.assertEqual(r, "ueber-ueber-german-umlaut")
  138. txt = 'i love 🦄'
  139. r = slugify(txt, allow_unicode=True)
  140. self.assertEqual(r, "i-love")
  141. txt = 'i love 🦄'
  142. r = slugify(txt, allow_unicode=True, regex_pattern=r'[^🦄]+')
  143. self.assertEqual(r, "🦄")
  144. ```
  145. For more examples, have a look at the [test.py](test.py) file.
  146. # Command Line Options
  147. With the package, a command line tool called `slugify` is also installed.
  148. It allows convenient command line access to all the features the `slugify` function supports. Call it with `-h` for help.
  149. The command can take its input directly on the command line or from STDIN (when the `--stdin` flag is passed):
  150. ```
  151. $ echo "Taking input from STDIN" | slugify --stdin
  152. taking-input-from-stdin
  153. ```
  154. ```
  155. $ slugify taking input from the command line
  156. taking-input-from-the-command-line
  157. ```
  158. Please note that when a multi-valued option such as `--stopwords` or `--replacements` is passed, you need to use `--` as separator before you start with the input:
  159. ```
  160. $ slugify --stopwords the in a hurry -- the quick brown fox jumps over the lazy dog in a hurry
  161. quick-brown-fox-jumps-over-lazy-dog
  162. ```
  163. # Running the tests
  164. To run the tests against the current environment:
  165. python test.py
  166. # Contribution
  167. Please read the ([wiki](https://github.com/un33k/python-slugify/wiki/Python-Slugify-Wiki)) page prior to raising any PRs.
  168. # License
  169. Released under a ([MIT](LICENSE)) license.
  170. ### Notes on GPL dependencies
  171. Though the dependencies may be GPL licensed, `python-slugify` itself is not considered a derivative work and will remain under the MIT license.
  172. If you wish to avoid installation of any GPL licensed packages, please note that the default dependency `text-unidecode` explicitly lets you choose to use the [Artistic License](https://opensource.org/license/artistic-perl-1-0-2/) instead. Use without concern.
  173. # Version
  174. X.Y.Z Version
  175. `MAJOR` version -- when you make incompatible API changes,
  176. `MINOR` version -- when you add functionality in a backwards-compatible manner, and
  177. `PATCH` version -- when you make backwards-compatible bug fixes.
  178. [status-image]: https://github.com/un33k/python-slugify/actions/workflows/ci.yml/badge.svg
  179. [status-link]: https://github.com/un33k/python-slugify/actions/workflows/ci.yml
  180. [version-image]: https://img.shields.io/pypi/v/python-slugify.svg
  181. [version-link]: https://pypi.python.org/pypi/python-slugify
  182. [coverage-image]: https://coveralls.io/repos/un33k/python-slugify/badge.svg
  183. [coverage-link]: https://coveralls.io/r/un33k/python-slugify
  184. [download-image]: https://img.shields.io/pypi/dm/python-slugify.svg
  185. [download-link]: https://pypi.python.org/pypi/python-slugify
  186. # Sponsors
  187. [Neekware Inc.](http://neekware.com)