Debian Codegraph @

third_party/python/markdown_it_py/markdown_it/_punycode.py

raw
1 # Copyright 2014 Mathias Bynens <https://mathiasbynens.be/>
2 # Copyright 2021 Taneli Hukkinen
3 #
4 # Permission is hereby granted, free of charge, to any person obtaining
5 # a copy of this software and associated documentation files (the
6 # "Software"), to deal in the Software without restriction, including
7 # without limitation the rights to use, copy, modify, merge, publish,
8 # distribute, sublicense, and/or sell copies of the Software, and to
9 # permit persons to whom the Software is furnished to do so, subject to
10 # the following conditions:
11 #
12 # The above copyright notice and this permission notice shall be
13 # included in all copies or substantial portions of the Software.
14 #
15 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19 # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20 # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21 # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22
23 import codecs
24 import re
25 from typing import Callable
26
27 REGEX_SEPARATORS = re.compile(r"[\x2E\u3002\uFF0E\uFF61]")
28 REGEX_NON_ASCII = re.compile(r"[^\0-\x7E]")
29
30
31 def encode(uni: str) -> str:
32 return codecs.encode(uni, encoding="punycode").decode()
33
34
35 def decode(ascii: str) -> str:
36 return codecs.decode(ascii, encoding="punycode") # type: ignore
37
38
39 def map_domain(string: str, fn: Callable[[str], str]) -> str:
40 parts = string.split("@")
41 result = ""
42 if len(parts) > 1:
43 # In email addresses, only the domain name should be punycoded. Leave
44 # the local part (i.e. everything up to `@`) intact.
45 result = parts[0] + "@"
46 string = parts[1]
47 labels = REGEX_SEPARATORS.split(string)
48 encoded = ".".join(fn(label) for label in labels)
49 return result + encoded
50
51
52 def to_unicode(obj: str) -> str:
53 def mapping(obj: str) -> str:
54 if obj.startswith("xn--"):
55 return decode(obj[4:].lower())
56 return obj
57
58 return map_domain(obj, mapping)
59
60
61 def to_ascii(obj: str) -> str:
62 def mapping(obj: str) -> str:
63 if REGEX_NON_ASCII.search(obj):
64 return "xn--" + encode(obj)
65 return obj
66
67 return map_domain(obj, mapping)