Package: linkify-it-py
linkify-it-pyThis is Python port oflinkify-it.Links recognition library with FULL unicode support.
Focused on high quality link patterns detection in plain text.DemoJavascript DemoWhy it's awesome:Full unicode support,with astral characters!International domains support.Allows rules extension & custom normalizers.Installpipinstalllinkify-it-pyorcondainstall-cconda-forgelinkify-it-pyUsage examplesExample 1. Simple usefromlinkify_itimportLinkifyItlinkify=LinkifyIt()print(linkify.test("Site github.com!"))# => Trueprint(linkify.match("Site github.com!"))# => [linkify_it.main.Match({#         'schema': '',#         'index': 5,#         'last_index': 15,#         'raw': 'github.com',#         'text': 'github.com',#         'url': 'http://github.com'#     }]Example 2. With optionsfromlinkify_itimportLinkifyItfromlinkify_it.tldsimportTLDS# Reload full tlds list & add unofficial `.onion` domain.linkify=(LinkifyIt().tlds(TLDS)# Reload with full tlds list.tlds("onion",True)# Add unofficial `.onion` domain.add("git:","http:")# Add `git:` protocol as "alias".add("ftp:",None)# Disable `ftp:` protocol.set({"fuzzy_ip":True})# Enable IPs in fuzzy links (without schema))print(linkify.test("Site tamanegi.onion!"))# => Trueprint(linkify.match("Site tamanegi.onion!"))# => [linkify_it.main.Match({#         'schema': '',#         'index': 5,#         'last_index': 19,#         'raw': 'tamanegi.onion',#         'text': 'tamanegi.onion',#         'url': 'http://tamanegi.onion'#     }]Example 3. Add twitter mentions handlerfromlinkify_itimportLinkifyItlinkify=LinkifyIt()defvalidate(obj,text,pos):tail=text[pos:]ifnotobj.re.get("twitter"):obj.re["twitter"]=re.compile("^([a-zA-Z0-9_]){1,15}(?!_)(?=$|"+obj.re["src_ZPCc"]+")")ifobj.re["twitter"].search(tail):ifpos>2andtail[pos-2]=="@":returnFalsereturnlen(obj.re["twitter"].search(tail).group())return0defnormalize(obj,match):match.url="https://twitter.com/"+re.sub(r"^@","",match.url)linkify.add("@",{"validate":validate,"normalize":normalize})APIAPI documentationLinkifyIt(schemas, options)Creates new linkifier instance with optional additional schemas.By default understands:http(s)://...,ftp://...,mailto:...&//...links"fuzzy" links and emails (google.com,foo@bar.com).schemasis an dict, where each key/value describes protocol/rule:key- link prefix (usually, protocol name with:at the end,skype:for example).linkify-it-pymakes sure that prefix is not preceded with
alphanumeric char.value- rule to check tail after link prefixstrjust alias to existing ruledictvalidate- either are.Pattern(start with^, and don't include the
link prefix itself), or a validatorfunctionwhich, given argumentsself,textandpos, returns the length of a match intextstarting at indexpos.posis the index right after the link prefix.selfcan be used to access the linkify object to cache data.normalize- optional function to normalize text & url of matched result
(for example, for twitter mentions).options:fuzzy_link- recognize URL-s withouthttp(s)://head. DefaultTrue.fuzzy_ip- allow IPs in fuzzy links above. Can conflict with some texts
like version numbers. DefaultFalse.fuzzy_email- recognize emails withoutmailto:prefix. DefaultTrue.---- setTrueto terminate link with---(if it's considered as long dash)..test(text)Searches linkifiable pattern and returnsTrueon success orFalseon fail..pretest(text)Quick check if link MAY BE can exist. Can be used to optimize more expensive.test()calls. ReturnFalseif link can not be found,True- if.test()call needed to know exactly..test_schema_at(text, name, position)Similar to.test()but checks only specific protocol tail exactly at given
position. Returns length of found pattern (0 on fail)..match(text)Returnslistof found link matches or null if nothing found.Each match has:schema- link schema, can be empty for fuzzy links, or//for
protocol-neutral links.index- offset of matched textlast_index- index of next char after mathch endraw- matched texttext- normalized texturl- link, generated from matched text.matchAtStart(text)Checks if a match exists at the start of the string. ReturnsMatch(see docs formatch(text)) or null if no URL is at the start.
Doesn't work with fuzzy links..tlds(list_tlds, keep_old=False)Load (or merge) new tlds list. Those are needed for fuzzy links (without schema)
to avoid false positives. By default:2-letter root zones are ok.biz|com|edu|gov|net|org|pro|web|xxx|aero|asia|coop|info|museum|name|shop|рф are ok.encoded (xn--...) root zones are ok.If that's not enough, you can reload defaults with more detailed zones list..add(key, value)Add a new schema to the schemas object. As described in the constructor
definition,keyis a link prefix (skype:, for example), andvalueis astrto alias to another schema, or andictwithvalidateand
optionallynormalizedefinitions.  To disable an existing rule, use.add(key, None)..set(options)Override default options. Missed properties will not be changed.LicenseMIT