| 1 |
Metadata-Version: 2.4 |
| 2 |
Name: linkify-it-py |
| 3 |
Version: 2.1.0 |
| 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.10 |
| 12 |
Classifier: Programming Language :: Python :: 3.11 |
| 13 |
Classifier: Programming Language :: Python :: 3.12 |
| 14 |
Classifier: Programming Language :: Python :: 3.13 |
| 15 |
Classifier: Programming Language :: Python :: 3.14 |
| 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.10 |
| 21 |
Description-Content-Type: text/markdown |
| 22 |
License-File: LICENSE |
| 23 |
Requires-Dist: uc-micro-py |
| 24 |
Provides-Extra: test |
| 25 |
Requires-Dist: pytest; extra == "test" |
| 26 |
Requires-Dist: coverage; extra == "test" |
| 27 |
Requires-Dist: pytest-cov; extra == "test" |
| 28 |
Provides-Extra: dev |
| 29 |
Requires-Dist: pre-commit; extra == "dev" |
| 30 |
Requires-Dist: isort; extra == "dev" |
| 31 |
Requires-Dist: flake8; extra == "dev" |
| 32 |
Requires-Dist: black; extra == "dev" |
| 33 |
Requires-Dist: pyproject-flake8; extra == "dev" |
| 34 |
Provides-Extra: benchmark |
| 35 |
Requires-Dist: pytest; extra == "benchmark" |
| 36 |
Requires-Dist: pytest-benchmark; extra == "benchmark" |
| 37 |
Provides-Extra: doc |
| 38 |
Requires-Dist: sphinx; extra == "doc" |
| 39 |
Requires-Dist: sphinx_book_theme; extra == "doc" |
| 40 |
Requires-Dist: myst-parser; extra == "doc" |
| 41 |
Dynamic: license-file |
| 42 |
|
| 43 |
# linkify-it-py |
| 44 |
|
| 45 |
[](https://github.com/tsutsu3/linkify-it-py/actions/workflows/github-ci.yml) |
| 46 |
[](https://pypi.org/project/linkify-it-py/) |
| 47 |
[](https://anaconda.org/conda-forge/linkify-it-py) |
| 48 |
[](https://linkify-it-py.readthedocs.io/en/latest/?badge=latest) |
| 49 |
[](https://codecov.io/gh/tsutsu3/linkify-it-py) |
| 50 |
|
| 51 |
This is Python port of [linkify-it](https://github.com/markdown-it/linkify-it). |
| 52 |
|
| 53 |
> Links recognition library with FULL unicode support. |
| 54 |
> Focused on high quality link patterns detection in plain text. |
| 55 |
|
| 56 |
__[Demo](https://linkify-it-py-demo.vercel.app/)__ |
| 57 |
|
| 58 |
__[Javascript Demo](http://markdown-it.github.io/linkify-it/)__ |
| 59 |
|
| 60 |
Why it's awesome: |
| 61 |
|
| 62 |
- Full unicode support, _with astral characters_! |
| 63 |
- International domains support. |
| 64 |
- Allows rules extension & custom normalizers. |
| 65 |
|
| 66 |
## Python Version Support |
| 67 |
|
| 68 |
Tested on Python 3.10–3.14. The primary version for code coverage follows the latest |
| 69 |
[security phase](https://devguide.python.org/versions/) release (currently __3.12__). |
| 70 |
|
| 71 |
## Install |
| 72 |
|
| 73 |
```bash |
| 74 |
pip install linkify-it-py |
| 75 |
``` |
| 76 |
|
| 77 |
or |
| 78 |
|
| 79 |
```bash |
| 80 |
conda install -c conda-forge linkify-it-py |
| 81 |
``` |
| 82 |
|
| 83 |
## Usage examples |
| 84 |
|
| 85 |
### Example 1. Simple use |
| 86 |
|
| 87 |
```python |
| 88 |
from linkify_it import LinkifyIt |
| 89 |
|
| 90 |
|
| 91 |
linkify = LinkifyIt() |
| 92 |
|
| 93 |
print(linkify.test("Site github.com!")) |
| 94 |
# => True |
| 95 |
|
| 96 |
print(linkify.match("Site github.com!")) |
| 97 |
# => [linkify_it.main.Match({ |
| 98 |
# 'schema': '', |
| 99 |
# 'index': 5, |
| 100 |
# 'last_index': 15, |
| 101 |
# 'raw': 'github.com', |
| 102 |
# 'text': 'github.com', |
| 103 |
# 'url': 'http://github.com' |
| 104 |
# }] |
| 105 |
``` |
| 106 |
|
| 107 |
### Example 2. With options |
| 108 |
|
| 109 |
```python |
| 110 |
from linkify_it import LinkifyIt |
| 111 |
from linkify_it.tlds import TLDS |
| 112 |
|
| 113 |
|
| 114 |
# Reload full tlds list & add unofficial `.onion` domain. |
| 115 |
linkify = ( |
| 116 |
LinkifyIt() |
| 117 |
.tlds(TLDS) # Reload with full tlds list |
| 118 |
.tlds("onion", True) # Add unofficial `.onion` domain |
| 119 |
.add("git:", "http:") # Add `git:` protocol as "alias" |
| 120 |
.add("ftp:", None) # Disable `ftp:` protocol |
| 121 |
.set({"fuzzy_ip": True}) # Enable IPs in fuzzy links (without schema) |
| 122 |
) |
| 123 |
print(linkify.test("Site tamanegi.onion!")) |
| 124 |
# => True |
| 125 |
|
| 126 |
print(linkify.match("Site tamanegi.onion!")) |
| 127 |
# => [linkify_it.main.Match({ |
| 128 |
# 'schema': '', |
| 129 |
# 'index': 5, |
| 130 |
# 'last_index': 19, |
| 131 |
# 'raw': 'tamanegi.onion', |
| 132 |
# 'text': 'tamanegi.onion', |
| 133 |
# 'url': 'http://tamanegi.onion' |
| 134 |
# }] |
| 135 |
``` |
| 136 |
|
| 137 |
### Example 3. Add twitter mentions handler |
| 138 |
|
| 139 |
```python |
| 140 |
from linkify_it import LinkifyIt |
| 141 |
|
| 142 |
|
| 143 |
linkify = LinkifyIt() |
| 144 |
|
| 145 |
def validate(obj, text, pos): |
| 146 |
tail = text[pos:] |
| 147 |
|
| 148 |
if not obj.re.get("twitter"): |
| 149 |
obj.re["twitter"] = re.compile( |
| 150 |
"^([a-zA-Z0-9_]){1,15}(?!_)(?=$|" + obj.re["src_ZPCc"] + ")" |
| 151 |
) |
| 152 |
if obj.re["twitter"].search(tail): |
| 153 |
if pos > 2 and tail[pos - 2] == "@": |
| 154 |
return False |
| 155 |
return len(obj.re["twitter"].search(tail).group()) |
| 156 |
return 0 |
| 157 |
|
| 158 |
def normalize(obj, match): |
| 159 |
match.url = "https://twitter.com/" + re.sub(r"^@", "", match.url) |
| 160 |
|
| 161 |
linkify.add("@", {"validate": validate, "normalize": normalize}) |
| 162 |
``` |
| 163 |
|
| 164 |
## API |
| 165 |
|
| 166 |
[API documentation](https://linkify-it-py.readthedocs.io/en/latest/) |
| 167 |
|
| 168 |
### LinkifyIt(schemas, options) |
| 169 |
|
| 170 |
Creates new linkifier instance with optional additional schemas. |
| 171 |
|
| 172 |
By default understands: |
| 173 |
|
| 174 |
- `http(s)://...` , `ftp://...`, `mailto:...` & `//...` links |
| 175 |
- "fuzzy" links and emails (google.com, [email protected]). |
| 176 |
|
| 177 |
`schemas` is an dict, where each key/value describes protocol/rule: |
| 178 |
|
| 179 |
- __key__ - link prefix (usually, protocol name with `:` at the end, `skype:` |
| 180 |
for example). `linkify-it-py` makes sure that prefix is not preceded with |
| 181 |
alphanumeric char. |
| 182 |
- __value__ - rule to check tail after link prefix |
| 183 |
- _str_ |
| 184 |
- just alias to existing rule |
| 185 |
- _dict_ |
| 186 |
- _validate_ - either a `re.Pattern` (start with `^`, and don't include the |
| 187 |
link prefix itself), or a validator `function` which, given arguments |
| 188 |
_self_, _text_ and _pos_, returns the length of a match in _text_ |
| 189 |
starting at index _pos_. _pos_ is the index right after the link prefix. |
| 190 |
_self_ can be used to access the linkify object to cache data. |
| 191 |
- _normalize_ - optional function to normalize text & url of matched result |
| 192 |
(for example, for twitter mentions). |
| 193 |
|
| 194 |
`options`: |
| 195 |
|
| 196 |
- __fuzzy_link__ - recognize URL-s without `http(s)://` head. Default `True`. |
| 197 |
- __fuzzy_ip__ - allow IPs in fuzzy links above. Can conflict with some texts |
| 198 |
like version numbers. Default `False`. |
| 199 |
- __fuzzy_email__ - recognize emails without `mailto:` prefix. Default `True`. |
| 200 |
- __---__ - set `True` to terminate link with `---` (if it's considered as long dash). |
| 201 |
|
| 202 |
### .test(text) |
| 203 |
|
| 204 |
Searches linkifiable pattern and returns `True` on success or `False` on fail. |
| 205 |
|
| 206 |
### .pretest(text) |
| 207 |
|
| 208 |
Quick check if link MAY BE can exist. Can be used to optimize more expensive |
| 209 |
`.test()` calls. Return `False` if link can not be found, `True` - if `.test()` |
| 210 |
call needed to know exactly. |
| 211 |
|
| 212 |
### .test_schema_at(text, name, position) |
| 213 |
|
| 214 |
Similar to `.test()` but checks only specific protocol tail exactly at given |
| 215 |
position. Returns length of found pattern (0 on fail). |
| 216 |
|
| 217 |
### .match(text) |
| 218 |
|
| 219 |
Returns `list` of found link matches or null if nothing found. |
| 220 |
|
| 221 |
Each match has: |
| 222 |
|
| 223 |
- __schema__ - link schema, can be empty for fuzzy links, or `//` for |
| 224 |
protocol-neutral links. |
| 225 |
- __index__ - offset of matched text |
| 226 |
- __last_index__ - index of next char after mathch end |
| 227 |
- __raw__ - matched text |
| 228 |
- __text__ - normalized text |
| 229 |
- __url__ - link, generated from matched text |
| 230 |
|
| 231 |
### .matchAtStart(text) |
| 232 |
|
| 233 |
Checks if a match exists at the start of the string. Returns `Match` |
| 234 |
(see docs for `match(text)`) or null if no URL is at the start. |
| 235 |
Doesn't work with fuzzy links. |
| 236 |
|
| 237 |
### .tlds(list_tlds, keep_old=False) |
| 238 |
|
| 239 |
Load (or merge) new tlds list. Those are needed for fuzzy links (without schema) |
| 240 |
to avoid false positives. By default: |
| 241 |
|
| 242 |
- 2-letter root zones are ok. |
| 243 |
- biz|com|edu|gov|net|org|pro|web|xxx|aero|asia|coop|info|museum|name|shop|рф are ok. |
| 244 |
- encoded (`xn--...`) root zones are ok. |
| 245 |
|
| 246 |
If that's not enough, you can reload defaults with more detailed zones list. |
| 247 |
|
| 248 |
### .add(key, value) |
| 249 |
|
| 250 |
Add a new schema to the schemas object. As described in the constructor |
| 251 |
definition, `key` is a link prefix (`skype:`, for example), and `value` |
| 252 |
is a `str` to alias to another schema, or an `dict` with `validate` and |
| 253 |
optionally `normalize` definitions. To disable an existing rule, use |
| 254 |
`.add(key, None)`. |
| 255 |
|
| 256 |
### .set(options) |
| 257 |
|
| 258 |
Override default options. Missed properties will not be changed. |
| 259 |
|
| 260 |
## License |
| 261 |
|
| 262 |
[MIT](https://github.com/tsutsu3/linkify-it-py/blob/master/LICENSE) |