|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace OsiemSiedem\Autolink; |
| 6 | + |
| 7 | +use Illuminate\Support\HtmlString; |
| 8 | +use OsiemSiedem\Autolink\Contracts\Filter; |
| 9 | +use OsiemSiedem\Autolink\Contracts\Parser; |
| 10 | + |
| 11 | +class Autolink |
| 12 | +{ |
| 13 | + /** |
| 14 | + * @var array |
| 15 | + */ |
| 16 | + protected $filters = []; |
| 17 | + |
| 18 | + /** |
| 19 | + * @var array |
| 20 | + */ |
| 21 | + protected $parsers = []; |
| 22 | + |
| 23 | + /** |
| 24 | + * @var array |
| 25 | + */ |
| 26 | + protected $ignored = ['a', 'pre', 'code', 'kbd', 'script']; |
| 27 | + |
| 28 | + /** |
| 29 | + * Add a new extension. |
| 30 | + * |
| 31 | + * @param \OsiemSiedem\Autolink\Contracts\Filter $filter |
| 32 | + * @return $this |
| 33 | + */ |
| 34 | + public function addFilter(Filter $filter): self |
| 35 | + { |
| 36 | + $this->filters[] = $filter; |
| 37 | + |
| 38 | + return $this; |
| 39 | + } |
| 40 | + |
| 41 | + /** |
| 42 | + * Add a new parser. |
| 43 | + * |
| 44 | + * @param \OsiemSiedem\Autolink\Contracts\Parser $parser |
| 45 | + * @return $this |
| 46 | + */ |
| 47 | + public function addParser(Parser $parser): self |
| 48 | + { |
| 49 | + foreach ($parser->getCharacters() as $character) { |
| 50 | + $this->parsers[$character] = $parser; |
| 51 | + } |
| 52 | + |
| 53 | + return $this; |
| 54 | + } |
| 55 | + |
| 56 | + /** |
| 57 | + * Set the ignored tags. |
| 58 | + * |
| 59 | + * @param array $ignore |
| 60 | + * @return $this |
| 61 | + */ |
| 62 | + public function ignore(array $ignored): self |
| 63 | + { |
| 64 | + $this->ignored = $ignored; |
| 65 | + |
| 66 | + return $this; |
| 67 | + } |
| 68 | + |
| 69 | + /** |
| 70 | + * Convert the URLs into clickable links. |
| 71 | + * |
| 72 | + * @param string $text |
| 73 | + * @param callable $callback |
| 74 | + * @return \Illuminate\Support\HtmlString |
| 75 | + */ |
| 76 | + public function convert(string $text, callable $callback = null): HtmlString |
| 77 | + { |
| 78 | + $cursor = new Cursor($text); |
| 79 | + |
| 80 | + $links = []; |
| 81 | + |
| 82 | + foreach ($cursor as $character) { |
| 83 | + if ($character === '<') { |
| 84 | + foreach ($this->ignored as $ignored) { |
| 85 | + if ($cursor->match("#^<{$ignored}[\s>]#i")) { |
| 86 | + $cursor->next(strlen($ignored) + 1); |
| 87 | + |
| 88 | + while ($cursor->valid()) { |
| 89 | + while ($cursor->valid() && $cursor->getCharacter() !== '<') { |
| 90 | + $cursor->next(); |
| 91 | + } |
| 92 | + |
| 93 | + if ($cursor->getPosition() === $cursor->getLength()) { |
| 94 | + break 2; |
| 95 | + } |
| 96 | + |
| 97 | + if ($cursor->match("#^</{$ignored}[\s>]#i")) { |
| 98 | + break 2; |
| 99 | + } |
| 100 | + |
| 101 | + $cursor->next(); |
| 102 | + } |
| 103 | + |
| 104 | + break; |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + while ($cursor->valid() && $cursor->getCharacter() !== '>') { |
| 109 | + $cursor->next(); |
| 110 | + } |
| 111 | + |
| 112 | + continue; |
| 113 | + } |
| 114 | + |
| 115 | + $parser = array_get($this->parsers, $character); |
| 116 | + |
| 117 | + if (is_null($parser)) { |
| 118 | + continue; |
| 119 | + } |
| 120 | + |
| 121 | + if ($link = $parser->parse($cursor)) { |
| 122 | + $links[] = $link; |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + for ($i = count($links) - 1; $i >= 0; $i--) { |
| 127 | + $start = $links[$i]->getStart(); |
| 128 | + |
| 129 | + $end = $links[$i]->getEnd(); |
| 130 | + |
| 131 | + foreach ($this->filters as $filter) { |
| 132 | + $links[$i] = $filter->filter($links[$i]); |
| 133 | + } |
| 134 | + |
| 135 | + if ( ! is_null($callback)) { |
| 136 | + $links[$i] = $callback($links[$i]); |
| 137 | + } |
| 138 | + |
| 139 | + $text = mb_substr($text, 0, $start) |
| 140 | + .$links[$i]->toHtml() |
| 141 | + .mb_substr($text, $end, mb_strlen($text) - $end); |
| 142 | + } |
| 143 | + |
| 144 | + return new HtmlString($text); |
| 145 | + } |
| 146 | +} |
0 commit comments