|
5 | 5 | namespace OsiemSiedem\Autolink; |
6 | 6 |
|
7 | 7 | use Illuminate\Support\HtmlString; |
8 | | -use OsiemSiedem\Autolink\Contracts\Filter; |
9 | | -use OsiemSiedem\Autolink\Contracts\Parser; |
10 | 8 |
|
11 | 9 | class Autolink |
12 | 10 | { |
13 | 11 | /** |
14 | | - * @var array |
| 12 | + * @var \OsiemSiedem\Autolink\Parser |
15 | 13 | */ |
16 | | - protected $filters = []; |
| 14 | + protected $parser; |
17 | 15 |
|
18 | 16 | /** |
19 | | - * @var array |
| 17 | + * @var \OsiemSiedem\Autolink\HtmlRenderer |
20 | 18 | */ |
21 | | - protected $parsers = []; |
| 19 | + protected $renderer; |
22 | 20 |
|
23 | 21 | /** |
24 | | - * @var array |
25 | | - */ |
26 | | - protected $ignored = ['a', 'pre', 'code', 'kbd', 'script']; |
27 | | - |
28 | | - /** |
29 | | - * Add a new extension. |
| 22 | + * Create a new instance. |
30 | 23 | * |
31 | | - * @param \OsiemSiedem\Autolink\Contracts\Filter $filter |
32 | | - * @return $this |
| 24 | + * @param \OsiemSiedem\Autolink\Parser $parser |
| 25 | + * @param \OsiemSiedem\Autolink\HtmlRenderer $renderer |
| 26 | + * @return void |
33 | 27 | */ |
34 | | - public function addFilter(Filter $filter): self |
| 28 | + public function __construct(Parser $parser, HtmlRenderer $renderer) |
35 | 29 | { |
36 | | - $this->filters[] = $filter; |
37 | | - |
38 | | - return $this; |
| 30 | + $this->parser = $parser; |
| 31 | + $this->renderer = $renderer; |
39 | 32 | } |
40 | 33 |
|
41 | 34 | /** |
42 | | - * Add a new parser. |
| 35 | + * Convert the URLs into clickable links. |
43 | 36 | * |
44 | | - * @param \OsiemSiedem\Autolink\Contracts\Parser $parser |
45 | | - * @return $this |
| 37 | + * @param string $text |
| 38 | + * @param callable|null $callback |
| 39 | + * @return \Illuminate\Support\HtmlString |
46 | 40 | */ |
47 | | - public function addParser(Parser $parser): self |
| 41 | + public function convert(string $text, callable $callback = null): HtmlString |
48 | 42 | { |
49 | | - foreach ($parser->getCharacters() as $character) { |
50 | | - $this->parsers[$character][] = $parser; |
51 | | - } |
| 43 | + $elements = $this->parse($text); |
52 | 44 |
|
53 | | - return $this; |
| 45 | + return $this->render($text, $elements, $callback); |
54 | 46 | } |
55 | 47 |
|
56 | 48 | /** |
57 | | - * Set the ignored tags. |
| 49 | + * Parse the text. |
58 | 50 | * |
59 | | - * @param array $ignored |
60 | | - * @return $this |
| 51 | + * @param string $text |
| 52 | + * @return array |
61 | 53 | */ |
62 | | - public function ignore(array $ignored): self |
| 54 | + public function parse(string $text): array |
63 | 55 | { |
64 | | - $this->ignored = $ignored; |
65 | | - |
66 | | - return $this; |
| 56 | + return $this->parser->parse($text); |
67 | 57 | } |
68 | 58 |
|
69 | 59 | /** |
70 | | - * Convert the URLs into clickable links. |
| 60 | + * Render the elements. |
71 | 61 | * |
72 | 62 | * @param string $text |
| 63 | + * @param array $elements |
73 | 64 | * @param callable|null $callback |
74 | 65 | * @return \Illuminate\Support\HtmlString |
75 | 66 | */ |
76 | | - public function convert(string $text, callable $callback = null): HtmlString |
| 67 | + public function render(string $text, array $elements, callable $callback = null): HtmlString |
77 | 68 | { |
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 | | - $parsers = array_get($this->parsers, $character); |
116 | | - |
117 | | - if (is_null($parsers)) { |
118 | | - continue; |
119 | | - } |
120 | | - |
121 | | - foreach ($parsers as $parser) { |
122 | | - if ($link = $parser->parse($cursor)) { |
123 | | - $links[] = $link; |
124 | | - |
125 | | - break; |
126 | | - } |
127 | | - } |
128 | | - } |
129 | | - |
130 | | - for ($i = count($links) - 1; $i >= 0; $i--) { |
131 | | - $start = $links[$i]->getStart(); |
132 | | - |
133 | | - $end = $links[$i]->getEnd(); |
134 | | - |
135 | | - foreach ($this->filters as $filter) { |
136 | | - $links[$i] = $filter->filter($links[$i]); |
137 | | - } |
138 | | - |
139 | | - if ( ! is_null($callback)) { |
140 | | - $links[$i] = $callback($links[$i]); |
141 | | - } |
142 | | - |
143 | | - $text = mb_substr($text, 0, $start) |
144 | | - .$links[$i]->toHtml() |
145 | | - .mb_substr($text, $end, mb_strlen($text) - $end); |
146 | | - } |
147 | | - |
148 | | - return new HtmlString($text); |
| 69 | + return $this->renderer->render($text, $elements, $callback); |
149 | 70 | } |
150 | 71 | } |
0 commit comments