Debian Codegraph @

linkify_it/ucre.py

raw symbols SCIP
1 from uc_micro.categories import Cc, Cf, P, Z
2 from uc_micro.properties import Any
3
4 SRC_ANY = Any.REGEX
5 SRC_CC = Cc.REGEX
6 SRC_CF = Cf.REGEX
7 SRC_P = P.REGEX
8 SRC_Z = Z.REGEX
9
10 # \p{\Z\P\Cc\CF} (white spaces + control + format + punctuation)
11 SRC_ZPCC = "|".join([SRC_Z, SRC_P, SRC_CC])
12
13 # \p{\Z\Cc} (white spaces + control)
14 SRC_ZCC = "|".join([SRC_Z, SRC_CC])
15
16 # Experimental. List of chars, completely prohibited in links
17 # because can separate it from other part of text
18 TEXT_SEPARATORS = "[><\uff5c]"
19
20 # All possible word characters (everything without punctuation, spaces & controls)
21 # Defined via punctuation & spaces to save space
22 # Should be something like \p{\L\N\S\M} (\w but without `_`)
23 SRC_PSEUDO_LETTER = "(?:(?!" + TEXT_SEPARATORS + "|" + SRC_ZPCC + ")" + SRC_ANY + ")"
24 # The same as abothe but without [0-9]
25 # var SRC_PSEUDO_LETTER_non_d = '(?:(?![0-9]|' + SRC_ZPCC + ')' + SRC_ANY + ')'
26
27 # =============================================================================
28
29 SRC_IP4 = (
30 "(?:(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(25[0-5]|"
31 + "2[0-4][0-9]|[01]?[0-9][0-9]?)"
32 )
33
34 # Prohibit any of "@/[]()" in user/pass to avoid wrong domain fetch.
35 SRC_AUTH = "(?:(?:(?!" + SRC_ZCC + "|[@/\\[\\]()]).)+@)?"
36
37 SRC_PORT = (
38 "(?::(?:6(?:[0-4]\\d{3}|5(?:[0-4]\\d{2}|5(?:[0-2]\\d|3[0-5])))|[1-5]?\\d{1,4}))?"
39 )
40
41 # Allow anything in markdown spec, forbid quote (") at the first position
42 # because emails enclosed in quotes are far more common
43 SRC_EMAIL_NAME = '[\\-:&=\\+\\$,\\.a-zA-Z0-9_][\\-:&=\\+\\$,\\"\\.a-zA-Z0-9_]*'
44
45 SRC_XN = "xn--[a-z0-9\\-]{1,59}"
46
47 # More to read about domain names
48 # http:#serverfault.com/questions/638260/
49
50 # Allow letters & digits (http:#test1)
51 SRC_DOMAIN_ROOT = "(?:" + SRC_XN + "|" + SRC_PSEUDO_LETTER + "{1,63}" + ")"
52
53 SRC_DOMAIN = (
54 "(?:"
55 + SRC_XN
56 + "|"
57 + "(?:"
58 + SRC_PSEUDO_LETTER
59 + ")"
60 + "|"
61 + "(?:"
62 + SRC_PSEUDO_LETTER
63 + "(?:-|"
64 + SRC_PSEUDO_LETTER
65 + "){0,61}"
66 + SRC_PSEUDO_LETTER
67 + ")"
68 + ")"
69 )
70
71 SRC_HOST = (
72 "(?:"
73 +
74 # Don't need IP check, because digits are already allowed in normal domain names
75 # SRC_IP4 +
76 # '|' +
77 "(?:(?:(?:"
78 + SRC_DOMAIN
79 + ")\\.)*"
80 + SRC_DOMAIN # _root
81 + ")"
82 + ")"
83 )
84
85 TPL_HOST_FUZZY = (
86 "(?:" + SRC_IP4 + "|" + "(?:(?:(?:" + SRC_DOMAIN + ")\\.)+(?:%TLDS%))" + ")"
87 )
88
89 TPL_HOST_NO_IP_FUZZY = "(?:(?:(?:" + SRC_DOMAIN + ")\\.)+(?:%TLDS%))"
90
91
92 # =============================================================================
93
94 # Rude test fuzzy links by host, for quick deny
95 TPL_HOST_FUZZY_TEST = (
96 "localhost|www\\.|\\.\\d{1,3}\\.|(?:\\.(?:%TLDS%)(?:" + SRC_ZPCC + "|>|$))"
97 )
98
99
100 def _re_host_terminator(opts):
101 src_host_terminator = (
102 "(?=$|"
103 + TEXT_SEPARATORS
104 + "|"
105 + SRC_ZPCC
106 + ")"
107 + "(?!"
108 + ("-(?!--)|" if opts.get("---") else "-|")
109 + "_|:\\d|\\.-|\\.(?!$|"
110 + SRC_ZPCC
111 + "))"
112 )
113 return src_host_terminator
114
115
116 def _re_src_path(opts):
117 src_path = (
118 "(?:"
119 + "[/?#]"
120 + "(?:"
121 + "(?!"
122 + SRC_ZCC
123 + "|"
124 + TEXT_SEPARATORS
125 + "|[()[\\]{}.,\"'?!\\-;]).|"
126 + "\\[(?:(?!"
127 + SRC_ZCC
128 + "|\\]).)*\\]|"
129 + "\\((?:(?!"
130 + SRC_ZCC
131 + "|[)]).)*\\)|"
132 + "\\{(?:(?!"
133 + SRC_ZCC
134 + "|[}]).)*\\}|"
135 + '\\"(?:(?!'
136 + SRC_ZCC
137 + '|["]).)+\\"|'
138 + "\\'(?:(?!"
139 + SRC_ZCC
140 + "|[']).)+\\'|"
141 + "\\'(?="
142 + SRC_PSEUDO_LETTER
143 + "|[-])|"
144 + "\\.{2,}[a-zA-Z0-9%/&]|"
145 # google has many dots in "google search" links (#66, #81).
146 # github has ... in commit range links,
147 # ReSTRICT to
148 # - english
149 # - percent-encoded
150 # - parts of file path
151 # - params separator
152 # until more examples found.
153 + "\\.(?!"
154 + SRC_ZCC
155 + "|[.]|$)|"
156 + ("\\-(?!--(?:[^-]|$))(?:-*)|" if opts.get("---") else "\\-+|")
157 + ",(?!"
158 + SRC_ZCC
159 + "|$)|" # allow `,,,` in paths
160 + ";(?!"
161 + SRC_ZCC
162 + "|$)|" # allow `,,,` in paths
163 + "\\!+(?!"
164 + SRC_ZCC
165 + "|[!]|$)|" # allow `!!!` in paths, but not at the end
166 + "\\?(?!"
167 + SRC_ZCC
168 + "|[?]|$)"
169 + ")+"
170 + "|\\/"
171 + ")?"
172 )
173
174 return src_path
175
176
177 def build_re(opts):
178 """Build regex
179
180 Args:
181 opts (dict): options
182
183 Return:
184 dict: dict of regex string
185 """
186 SRC_HOST_STRICT = SRC_HOST + _re_host_terminator(opts)
187
188 TPL_HOST_FUZZY_STRICT = TPL_HOST_FUZZY + _re_host_terminator(opts)
189
190 SRC_HOST_PORT_STRICT = SRC_HOST + SRC_PORT + _re_host_terminator(opts)
191
192 TPL_HOST_PORT_FUZZY_STRICT = TPL_HOST_FUZZY + SRC_PORT + _re_host_terminator(opts)
193
194 TPL_HOST_PORT_NO_IP_FUZZY_STRICT = (
195 TPL_HOST_NO_IP_FUZZY + SRC_PORT + _re_host_terminator(opts)
196 )
197
198 TPL_EMAIL_FUZZY = (
199 "(^|"
200 + TEXT_SEPARATORS
201 + '|"|\\(|'
202 + SRC_ZCC
203 + ")"
204 + "("
205 + SRC_EMAIL_NAME
206 + "@"
207 + TPL_HOST_FUZZY_STRICT
208 + ")"
209 )
210
211 regex = {
212 "src_Any": SRC_ANY,
213 "src_Cc": SRC_CC,
214 "src_Cf": SRC_CF,
215 "src_Z": SRC_Z,
216 "src_P": SRC_P,
217 "src_ZPCc": SRC_ZPCC,
218 "src_ZCc": SRC_ZCC,
219 "src_pseudo_letter": SRC_PSEUDO_LETTER,
220 "src_ip4": SRC_IP4,
221 "src_auth": SRC_AUTH,
222 "src_port": SRC_PORT,
223 "src_host_terminator": _re_host_terminator(opts),
224 "src_path": _re_src_path(opts),
225 "src_email_name": SRC_EMAIL_NAME,
226 "src_xn": SRC_XN,
227 "src_domain_root": SRC_DOMAIN_ROOT,
228 "src_domain": SRC_DOMAIN,
229 "src_host": SRC_HOST,
230 "tpl_host_fuzzy": TPL_HOST_FUZZY,
231 "tpl_host_no_ip_fuzzy": TPL_HOST_NO_IP_FUZZY,
232 "src_host_strict": SRC_HOST_STRICT,
233 "tpl_host_fuzzy_strict": TPL_HOST_FUZZY_STRICT,
234 "src_host_port_strict": SRC_HOST_PORT_STRICT,
235 "tpl_host_port_fuzzy_strict": TPL_HOST_PORT_FUZZY_STRICT,
236 "tpl_host_port_no_ip_fuzzy_strict": TPL_HOST_PORT_FUZZY_STRICT,
237 # Main rules
238 "tpl_host_fuzzy_test": TPL_HOST_FUZZY_TEST,
239 "tpl_email_fuzzy": TPL_EMAIL_FUZZY,
240 # Fuzzy link can't be prepended with .:/\- and non punctuation.
241 # but can start with > (markdown blockquote)
242 "tpl_link_fuzzy": (
243 "(^|(?![.:/\\-_@])(?:[$+<=>^`|\uff5c]|"
244 + SRC_ZPCC
245 + "))"
246 + "((?![$+<=>^`|\uff5c])"
247 + TPL_HOST_PORT_FUZZY_STRICT
248 + _re_src_path(opts)
249 + ")"
250 ),
251 # Fuzzy link can't be prepended with .:/\- and non punctuation.
252 # but can start with > (markdown blockquote)
253 "tpl_link_no_ip_fuzzy": (
254 "(^|(?![.:/\\-_@])(?:[$+<=>^`|\uff5c]|"
255 + SRC_ZPCC
256 + "))"
257 + "((?![$+<=>^`|\uff5c])"
258 + TPL_HOST_PORT_NO_IP_FUZZY_STRICT
259 + _re_src_path(opts)
260 + ")"
261 ),
262 }
263
264 return regex