METADATA 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. Metadata-Version: 2.1
  2. Name: linkify-it-py
  3. Version: 2.0.3
  4. Summary: Links recognition library with FULL unicode support.
  5. Author: tsutsu3
  6. License: MIT
  7. Project-URL: Homepage, https://github.com/tsutsu3/linkify-it-py
  8. Keywords: linkify,linkifier,autolink,autolinker
  9. Classifier: Development Status :: 5 - Production/Stable
  10. Classifier: Programming Language :: Python :: 3
  11. Classifier: Programming Language :: Python :: 3.7
  12. Classifier: Programming Language :: Python :: 3.8
  13. Classifier: Programming Language :: Python :: 3.9
  14. Classifier: Programming Language :: Python :: 3.10
  15. Classifier: Programming Language :: Python :: 3.11
  16. Classifier: License :: OSI Approved :: MIT License
  17. Classifier: Operating System :: OS Independent
  18. Classifier: Intended Audience :: Developers
  19. Classifier: Topic :: Software Development :: Libraries :: Python Modules
  20. Requires-Python: >=3.7
  21. Description-Content-Type: text/markdown
  22. License-File: LICENSE
  23. Requires-Dist: uc-micro-py
  24. Provides-Extra: benchmark
  25. Requires-Dist: pytest ; extra == 'benchmark'
  26. Requires-Dist: pytest-benchmark ; extra == 'benchmark'
  27. Provides-Extra: dev
  28. Requires-Dist: pre-commit ; extra == 'dev'
  29. Requires-Dist: isort ; extra == 'dev'
  30. Requires-Dist: flake8 ; extra == 'dev'
  31. Requires-Dist: black ; extra == 'dev'
  32. Requires-Dist: pyproject-flake8 ; extra == 'dev'
  33. Provides-Extra: doc
  34. Requires-Dist: sphinx ; extra == 'doc'
  35. Requires-Dist: sphinx-book-theme ; extra == 'doc'
  36. Requires-Dist: myst-parser ; extra == 'doc'
  37. Provides-Extra: test
  38. Requires-Dist: pytest ; extra == 'test'
  39. Requires-Dist: coverage ; extra == 'test'
  40. Requires-Dist: pytest-cov ; extra == 'test'
  41. # linkify-it-py
  42. [![CI](https://github.com/tsutsu3/linkify-it-py/workflows/CI/badge.svg?branch=main)](https://github.com/tsutsu3/linkify-it-py/actions)
  43. [![pypi](https://img.shields.io/pypi/v/linkify-it-py)](https://pypi.org/project/linkify-it-py/)
  44. [![Anaconda-Server Badge](https://anaconda.org/conda-forge/linkify-it-py/badges/version.svg)](https://anaconda.org/conda-forge/linkify-it-py)
  45. [![Documentation Status](https://readthedocs.org/projects/linkify-it-py/badge/?version=latest)](https://linkify-it-py.readthedocs.io/en/latest/?badge=latest)
  46. [![codecov](https://codecov.io/gh/tsutsu3/linkify-it-py/branch/main/graph/badge.svg)](https://codecov.io/gh/tsutsu3/linkify-it-py)
  47. [![Maintainability](https://api.codeclimate.com/v1/badges/6341fd3ec5f05fde392f/maintainability)](https://codeclimate.com/github/tsutsu3/linkify-it-py/maintainability)
  48. This is Python port of [linkify-it](https://github.com/markdown-it/linkify-it).
  49. > Links recognition library with FULL unicode support.
  50. > Focused on high quality link patterns detection in plain text.
  51. __[Demo](https://linkify-it-py-demo.vercel.app/)__
  52. __[Javascript Demo](http://markdown-it.github.io/linkify-it/)__
  53. Why it's awesome:
  54. - Full unicode support, _with astral characters_!
  55. - International domains support.
  56. - Allows rules extension & custom normalizers.
  57. ## Install
  58. ```bash
  59. pip install linkify-it-py
  60. ```
  61. or
  62. ```bash
  63. conda install -c conda-forge linkify-it-py
  64. ```
  65. ## Usage examples
  66. ### Example 1. Simple use
  67. ```python
  68. from linkify_it import LinkifyIt
  69. linkify = LinkifyIt()
  70. print(linkify.test("Site github.com!"))
  71. # => True
  72. print(linkify.match("Site github.com!"))
  73. # => [linkify_it.main.Match({
  74. # 'schema': '',
  75. # 'index': 5,
  76. # 'last_index': 15,
  77. # 'raw': 'github.com',
  78. # 'text': 'github.com',
  79. # 'url': 'http://github.com'
  80. # }]
  81. ```
  82. ### Example 2. With options
  83. ```python
  84. from linkify_it import LinkifyIt
  85. from linkify_it.tlds import TLDS
  86. # Reload full tlds list & add unofficial `.onion` domain.
  87. linkify = (
  88. LinkifyIt()
  89. .tlds(TLDS) # Reload with full tlds list
  90. .tlds("onion", True) # Add unofficial `.onion` domain
  91. .add("git:", "http:") # Add `git:` protocol as "alias"
  92. .add("ftp:", None) # Disable `ftp:` protocol
  93. .set({"fuzzy_ip": True}) # Enable IPs in fuzzy links (without schema)
  94. )
  95. print(linkify.test("Site tamanegi.onion!"))
  96. # => True
  97. print(linkify.match("Site tamanegi.onion!"))
  98. # => [linkify_it.main.Match({
  99. # 'schema': '',
  100. # 'index': 5,
  101. # 'last_index': 19,
  102. # 'raw': 'tamanegi.onion',
  103. # 'text': 'tamanegi.onion',
  104. # 'url': 'http://tamanegi.onion'
  105. # }]
  106. ```
  107. ### Example 3. Add twitter mentions handler
  108. ```python
  109. from linkify_it import LinkifyIt
  110. linkify = LinkifyIt()
  111. def validate(obj, text, pos):
  112. tail = text[pos:]
  113. if not obj.re.get("twitter"):
  114. obj.re["twitter"] = re.compile(
  115. "^([a-zA-Z0-9_]){1,15}(?!_)(?=$|" + obj.re["src_ZPCc"] + ")"
  116. )
  117. if obj.re["twitter"].search(tail):
  118. if pos > 2 and tail[pos - 2] == "@":
  119. return False
  120. return len(obj.re["twitter"].search(tail).group())
  121. return 0
  122. def normalize(obj, match):
  123. match.url = "https://twitter.com/" + re.sub(r"^@", "", match.url)
  124. linkify.add("@", {"validate": validate, "normalize": normalize})
  125. ```
  126. ## API
  127. [API documentation](https://linkify-it-py.readthedocs.io/en/latest/)
  128. ### LinkifyIt(schemas, options)
  129. Creates new linkifier instance with optional additional schemas.
  130. By default understands:
  131. - `http(s)://...` , `ftp://...`, `mailto:...` & `//...` links
  132. - "fuzzy" links and emails (google.com, foo@bar.com).
  133. `schemas` is an dict, where each key/value describes protocol/rule:
  134. - __key__ - link prefix (usually, protocol name with `:` at the end, `skype:`
  135. for example). `linkify-it-py` makes sure that prefix is not preceded with
  136. alphanumeric char.
  137. - __value__ - rule to check tail after link prefix
  138. - _str_
  139. - just alias to existing rule
  140. - _dict_
  141. - _validate_ - either a `re.Pattern` (start with `^`, and don't include the
  142. link prefix itself), or a validator `function` which, given arguments
  143. _self_, _text_ and _pos_, returns the length of a match in _text_
  144. starting at index _pos_. _pos_ is the index right after the link prefix.
  145. _self_ can be used to access the linkify object to cache data.
  146. - _normalize_ - optional function to normalize text & url of matched result
  147. (for example, for twitter mentions).
  148. `options`:
  149. - __fuzzy_link__ - recognize URL-s without `http(s)://` head. Default `True`.
  150. - __fuzzy_ip__ - allow IPs in fuzzy links above. Can conflict with some texts
  151. like version numbers. Default `False`.
  152. - __fuzzy_email__ - recognize emails without `mailto:` prefix. Default `True`.
  153. - __---__ - set `True` to terminate link with `---` (if it's considered as long dash).
  154. ### .test(text)
  155. Searches linkifiable pattern and returns `True` on success or `False` on fail.
  156. ### .pretest(text)
  157. Quick check if link MAY BE can exist. Can be used to optimize more expensive
  158. `.test()` calls. Return `False` if link can not be found, `True` - if `.test()`
  159. call needed to know exactly.
  160. ### .test_schema_at(text, name, position)
  161. Similar to `.test()` but checks only specific protocol tail exactly at given
  162. position. Returns length of found pattern (0 on fail).
  163. ### .match(text)
  164. Returns `list` of found link matches or null if nothing found.
  165. Each match has:
  166. - __schema__ - link schema, can be empty for fuzzy links, or `//` for
  167. protocol-neutral links.
  168. - __index__ - offset of matched text
  169. - __last_index__ - index of next char after mathch end
  170. - __raw__ - matched text
  171. - __text__ - normalized text
  172. - __url__ - link, generated from matched text
  173. ### .matchAtStart(text)
  174. Checks if a match exists at the start of the string. Returns `Match`
  175. (see docs for `match(text)`) or null if no URL is at the start.
  176. Doesn't work with fuzzy links.
  177. ### .tlds(list_tlds, keep_old=False)
  178. Load (or merge) new tlds list. Those are needed for fuzzy links (without schema)
  179. to avoid false positives. By default:
  180. - 2-letter root zones are ok.
  181. - biz|com|edu|gov|net|org|pro|web|xxx|aero|asia|coop|info|museum|name|shop|рф are ok.
  182. - encoded (`xn--...`) root zones are ok.
  183. If that's not enough, you can reload defaults with more detailed zones list.
  184. ### .add(key, value)
  185. Add a new schema to the schemas object. As described in the constructor
  186. definition, `key` is a link prefix (`skype:`, for example), and `value`
  187. is a `str` to alias to another schema, or an `dict` with `validate` and
  188. optionally `normalize` definitions. To disable an existing rule, use
  189. `.add(key, None)`.
  190. ### .set(options)
  191. Override default options. Missed properties will not be changed.
  192. ## License
  193. [MIT](https://github.com/tsutsu3/linkify-it-py/blob/master/LICENSE)