11import re
22import string
3- import unicodedata
43from bisect import bisect_left
54
6- from .blocks import line_flags
7-
8- _GITLAB_URL_RE = re .compile (r"(?<![a-z0-9+.-])[a-z][a-z0-9+.-]{1,31}://[^\s<>`()\[\]\"'*~|\\]+" )
9- _GITHUB_URL_RE = re .compile (r"(?<![A-Za-z])(?i:https?)://(?=\S)|(?<![^\s*_~(])www\." )
10- _ANGLE_RE = re .compile (r"(?<!\\)<(?i:https?|mailto):[^\x00-\x20<>\x7f]*>" )
11- _TAIL_RE = re .compile (r"[^\s<>`()\[\]\"'*~|\\]*" )
125_SPACE = " \t \n \v \f \r "
13- _CONTAINER_PREFIX_RE = re .compile (r"^[ \t>]+" , re .MULTILINE )
14- _TICKS_RE = re .compile (r"`+" )
6+ TICKS_RE = re .compile (r"`+" )
157_SPACES_RE = re .compile (r"[ \t\n\v\f\r]*" )
168_ANGLE_DEST_RE = re .compile (r"<(?:\\[\s\S]|[^\\<>\n])*>" )
179_TITLE_RE = re .compile (r"\"(?:[^\"]|(?<=\\)\")*\"|'(?:[^']|(?<=\\)')*'|\((?:[^()]|(?<=\\)[()])*\)" )
2719)
2820
2921
30- def _is_host_char (char : str ) -> bool :
31- return not (char .isspace () or char in string .punctuation or unicodedata .category (char ).startswith ("P" ))
32-
33-
34- def _domain_end (text : str , start : int ) -> int :
35- end = start
36- while end < len (text ) and (text [end ] in "._-" or _is_host_char (text [end ])):
37- end += 1
38- return end
39-
40-
41- def _is_valid_domain (domain : str ) -> bool :
42- return _is_host_char (domain [:1 ] or " " ) and "_" not in "" .join (domain .split ("." )[- 2 :])
43-
44-
4522def _bare_destination_end (text : str , pos : int , end : int ) -> int | None :
4623 start = pos
4724 depth = 0
@@ -100,8 +77,11 @@ def _spills(text: str, start: int, stop: int, end: int, last: dict[str, int]) ->
10077 return False
10178
10279
103- def _open_bracket_spans (text : str , start : int , end : int , ticks : dict [int , list [int ]]) -> list [tuple [int , int ]]:
80+ def open_bracket_spans (
81+ text : str , start : int , end : int , ticks : dict [int , list [int ]], urls : dict [int , int ]
82+ ) -> tuple [list [tuple [int , int ]], list [tuple [int , int ]]]:
10483 spans = []
84+ extents = []
10585 last = {closer : text .rfind (closer , start , end ) for closer in ("`" , * (closer for _ , closer in _RAW_HTML ))}
10686 depth = 0
10787 lax = False
@@ -113,7 +93,7 @@ def _open_bracket_spans(text: str, start: int, end: int, ticks: dict[int, list[i
11393 if char == "\\ " and step < end and text [step ] in string .punctuation :
11494 step += 1
11595 elif char == "`" and not lax :
116- step = _TICKS_RE .match (text , pos , end ).end ()
96+ step = TICKS_RE .match (text , pos , end ).end ()
11797 runs = ticks .get (step - pos , [])
11898 closer = bisect_left (runs , step )
11999 if closer < len (runs ) and runs [closer ] < end :
@@ -133,6 +113,9 @@ def _open_bracket_spans(text: str, start: int, end: int, ticks: dict[int, list[i
133113 if tail_end and not lax :
134114 depth += text .count ("[" , step , tail_end )
135115 step = tail_end
116+ elif not depth and pos in urls :
117+ step = min (urls [pos ], end )
118+ extents .append ((pos , step ))
136119
137120 if not before and depth :
138121 opened = pos
@@ -141,54 +124,4 @@ def _open_bracket_spans(text: str, start: int, end: int, ticks: dict[int, list[i
141124 pos = step
142125 if depth :
143126 spans .append ((opened , end ))
144- return spans
145-
146-
147- def _bracket_spans (text : str ) -> list [tuple [int , int ]]:
148- code_by_start , boundaries , html_starts = line_flags (text )
149- masked = _CONTAINER_PREFIX_RE .sub (lambda prefix : " " * len (prefix .group ()), text )
150- ticks : dict [int , list [int ]] = {}
151- for run in _TICKS_RE .finditer (masked ):
152- ticks .setdefault (len (run .group ()), []).append (run .start ())
153-
154- spans = []
155- stops = set (boundaries )
156- run_start = None
157- for offset , is_code in [* code_by_start .items (), (len (text ) + 1 , True )]:
158- raw = is_code or offset in html_starts
159- if run_start is not None and (raw or offset in stops ):
160- spans += _open_bracket_spans (masked , run_start , offset - 1 , ticks )
161- run_start = None
162- if run_start is None and not raw :
163- run_start = offset
164- return spans
165-
166-
167- def gitlab_autolinks (text : str ) -> list [tuple [int , int ]]:
168- return [match .span () for match in _GITLAB_URL_RE .finditer (text )]
169-
170-
171- def github_autolinks (text : str ) -> list [tuple [int , int ]]:
172- spans = [match .span () for match in _ANGLE_RE .finditer (text )]
173- brackets = _bracket_spans (text )
174- bracket_starts = [start for start , _ in brackets ]
175- covered = 0
176- for match in _GITHUB_URL_RE .finditer (text ):
177- if match .start () < covered :
178- continue
179- inside = bisect_left (bracket_starts , match .start () + 1 ) - 1
180- if inside >= 0 and match .start () < brackets [inside ][1 ]:
181- continue
182- domain = match .start () if match .group ().startswith ("www" ) else match .end ()
183- covered = _domain_end (text , domain )
184- if _is_valid_domain (text [domain :covered ]):
185- covered = _TAIL_RE .match (text , match .end ()).end ()
186- spans .append ((match .start (), covered ))
187-
188- merged : list [tuple [int , int ]] = []
189- for start , end in sorted (spans ):
190- if merged and start <= merged [- 1 ][1 ]:
191- merged [- 1 ] = (merged [- 1 ][0 ], max (end , merged [- 1 ][1 ]))
192- else :
193- merged .append ((start , end ))
194- return merged
127+ return spans , extents
0 commit comments