1 | <?php |
||
19 | final class LinkParserHelper |
||
20 | { |
||
21 | /** |
||
22 | * Attempt to parse link destination |
||
23 | * |
||
24 | * @param Cursor $cursor |
||
25 | * |
||
26 | * @return null|string The string, or null if no match |
||
27 | */ |
||
28 | 387 | public static function parseLinkDestination(Cursor $cursor) |
|
29 | { |
||
30 | 387 | if ($res = $cursor->match(RegexHelper::REGEX_LINK_DESTINATION_BRACES)) { |
|
31 | // Chop off surrounding <..>: |
||
32 | 12 | return UrlEncoder::unescapeAndEncode( |
|
33 | 12 | RegexHelper::unescape(substr($res, 1, -1)) |
|
34 | 4 | ); |
|
35 | } |
||
36 | |||
37 | 375 | $oldState = $cursor->saveState(); |
|
38 | 375 | $openParens = 0; |
|
39 | 375 | while (($c = $cursor->getCharacter()) !== null) { |
|
40 | 372 | if ($c === '\\' && RegexHelper::isEscapable($cursor->peek())) { |
|
41 | 18 | $cursor->advanceBy(2); |
|
42 | 372 | } elseif ($c === '(') { |
|
43 | 18 | $cursor->advance(); |
|
44 | 18 | $openParens++; |
|
45 | 372 | } elseif ($c === ')') { |
|
46 | 117 | if ($openParens < 1) { |
|
47 | 114 | break; |
|
48 | } |
||
49 | |||
50 | 18 | $cursor->advance(); |
|
51 | 18 | $openParens--; |
|
52 | 369 | } elseif (preg_match(RegexHelper::REGEX_WHITESPACE_CHAR, $c)) { |
|
53 | 168 | break; |
|
54 | } else { |
||
55 | 369 | $cursor->advance(); |
|
56 | } |
||
57 | 123 | } |
|
58 | |||
59 | 375 | $newPos = $cursor->getPosition(); |
|
60 | 375 | $cursor->restoreState($oldState); |
|
61 | |||
62 | 375 | $cursor->advanceBy($newPos - $cursor->getPosition()); |
|
63 | |||
64 | 375 | $res = $cursor->getPreviousText(); |
|
65 | |||
66 | 375 | return UrlEncoder::unescapeAndEncode( |
|
67 | 375 | RegexHelper::unescape($res) |
|
68 | 125 | ); |
|
69 | } |
||
70 | |||
71 | /** |
||
72 | * @param Cursor $cursor |
||
73 | * |
||
74 | * @return int |
||
75 | */ |
||
76 | 393 | public static function parseLinkLabel(Cursor $cursor) |
|
77 | { |
||
78 | 393 | $match = $cursor->match('/^\[(?:[^\\\\\[\]]|' . RegexHelper::PARTIAL_ESCAPED_CHAR . '|\\\\)*\]/'); |
|
79 | 393 | if ($match === null || preg_match('/[^\\\\]\\\\\]$/', $match)) { |
|
80 | 198 | return 0; |
|
81 | } |
||
82 | |||
83 | 366 | $length = mb_strlen($match, 'utf-8'); |
|
84 | |||
85 | 366 | if ($length > 1001) { |
|
86 | return 0; |
||
87 | } |
||
88 | |||
89 | 366 | return $length; |
|
90 | } |
||
91 | |||
92 | /** |
||
93 | * Attempt to parse link title (sans quotes) |
||
94 | * |
||
95 | * @param Cursor $cursor |
||
96 | * |
||
97 | * @return null|string The string, or null if no match |
||
98 | */ |
||
99 | 270 | public static function parseLinkTitle(Cursor $cursor) |
|
106 | } |
||
107 |