LinkTrait.php
7.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
<?php
/**
* @copyright Copyright (c) 2014 Carsten Brandt
* @license https://github.com/cebe/markdown/blob/master/LICENSE
* @link https://github.com/cebe/markdown#readme
*/
namespace cebe\markdown\inline;
// work around https://github.com/facebook/hhvm/issues/1120
defined('ENT_HTML401') || define('ENT_HTML401', 0);
/**
* Addes links and images as well as url markers.
*
* This trait conflicts with the HtmlTrait. If both are used together,
* you have to define a resolution, by defining the HtmlTrait::parseInlineHtml
* as private so it is not used directly:
*
* ```php
* use block\HtmlTrait {
* parseInlineHtml as private parseInlineHtml;
* }
* ```
*
* If the method exists it is called internally by this trait.
*
* Also make sure to reset references on prepare():
*
* ```php
* protected function prepare()
* {
* // reset references
* $this->references = [];
* }
* ```
*/
trait LinkTrait
{
/**
* @var array a list of defined references in this document.
*/
protected $references = [];
/**
* Remove backslash from escaped characters
* @param $text
* @return string
*/
protected function replaceEscape($text)
{
$strtr = [];
foreach($this->escapeCharacters as $char) {
$strtr["\\$char"] = $char;
}
return strtr($text, $strtr);
}
/**
* Parses a link indicated by `[`.
* @marker [
*/
protected function parseLink($markdown)
{
if (!in_array('parseLink', array_slice($this->context, 1)) && ($parts = $this->parseLinkOrImage($markdown)) !== false) {
list($text, $url, $title, $offset, $key) = $parts;
return [
[
'link',
'text' => $this->parseInline($text),
'url' => $url,
'title' => $title,
'refkey' => $key,
'orig' => substr($markdown, 0, $offset),
],
$offset
];
} else {
// remove all starting [ markers to avoid next one to be parsed as link
$result = '[';
$i = 1;
while (isset($markdown[$i]) && $markdown[$i] == '[') {
$result .= '[';
$i++;
}
return [['text', $result], $i];
}
}
/**
* Parses an image indicated by `![`.
* @marker ![
*/
protected function parseImage($markdown)
{
if (($parts = $this->parseLinkOrImage(substr($markdown, 1))) !== false) {
list($text, $url, $title, $offset, $key) = $parts;
return [
[
'image',
'text' => $text,
'url' => $url,
'title' => $title,
'refkey' => $key,
'orig' => substr($markdown, 0, $offset + 1),
],
$offset + 1
];
} else {
// remove all starting [ markers to avoid next one to be parsed as link
$result = '!';
$i = 1;
while (isset($markdown[$i]) && $markdown[$i] == '[') {
$result .= '[';
$i++;
}
return [['text', $result], $i];
}
}
protected function parseLinkOrImage($markdown)
{
if (strpos($markdown, ']') !== false && preg_match('/\[((?>[^\]\[]+|(?R))*)\]/', $markdown, $textMatches)) { // TODO improve bracket regex
$text = $textMatches[1];
$offset = strlen($textMatches[0]);
$markdown = substr($markdown, $offset);
$pattern = <<<REGEXP
/(?(R) # in case of recursion match parentheses
\(((?>[^\s()]+)|(?R))*\)
| # else match a link with title
^\(\s*(((?>[^\s()]+)|(?R))*)(\s+"(.*?)")?\s*\)
)/x
REGEXP;
if (preg_match($pattern, $markdown, $refMatches)) {
// inline link
return [
$text,
isset($refMatches[2]) ? $this->replaceEscape($refMatches[2]) : '', // url
empty($refMatches[5]) ? null: $refMatches[5], // title
$offset + strlen($refMatches[0]), // offset
null, // reference key
];
} elseif (preg_match('/^([ \n]?\[(.*?)\])?/s', $markdown, $refMatches)) {
// reference style link
if (empty($refMatches[2])) {
$key = strtolower($text);
} else {
$key = strtolower($refMatches[2]);
}
return [
$text,
null, // url
null, // title
$offset + strlen($refMatches[0]), // offset
$key,
];
}
}
return false;
}
/**
* Parses inline HTML.
* @marker <
*/
protected function parseLt($text)
{
if (strpos($text, '>') !== false) {
if (!in_array('parseLink', $this->context)) { // do not allow links in links
if (preg_match('/^<([^\s]*?@[^\s]*?\.\w+?)>/', $text, $matches)) {
// email address
return [
['email', $this->replaceEscape($matches[1])],
strlen($matches[0])
];
} elseif (preg_match('/^<([a-z]{3,}:\/\/[^\s]+?)>/', $text, $matches)) {
// URL
return [
['url', $this->replaceEscape($matches[1])],
strlen($matches[0])
];
}
}
// try inline HTML if it was neither a URL nor email if HtmlTrait is included.
if (method_exists($this, 'parseInlineHtml')) {
return $this->parseInlineHtml($text);
}
}
return [['text', '<'], 1];
}
protected function renderEmail($block)
{
$email = htmlspecialchars($block[1], ENT_NOQUOTES | ENT_SUBSTITUTE, 'UTF-8');
return "<a href=\"mailto:$email\">$email</a>";
}
protected function renderUrl($block)
{
$url = htmlspecialchars($block[1], ENT_COMPAT | ENT_HTML401, 'UTF-8');
$text = htmlspecialchars(urldecode($block[1]), ENT_NOQUOTES | ENT_SUBSTITUTE, 'UTF-8');
return "<a href=\"$url\">$text</a>";
}
protected function lookupReference($key)
{
$normalizedKey = preg_replace('/\s+/', ' ', $key);
if (isset($this->references[$key]) || isset($this->references[$key = $normalizedKey])) {
return $this->references[$key];
}
return false;
}
protected function renderLink($block)
{
if (isset($block['refkey'])) {
if (($ref = $this->lookupReference($block['refkey'])) !== false) {
$block = array_merge($block, $ref);
} else {
return $block['orig'];
}
}
return '<a href="' . htmlspecialchars($block['url'], ENT_COMPAT | ENT_HTML401, 'UTF-8') . '"'
. (empty($block['title']) ? '' : ' title="' . htmlspecialchars($block['title'], ENT_COMPAT | ENT_HTML401 | ENT_SUBSTITUTE, 'UTF-8') . '"')
. '>' . $this->renderAbsy($block['text']) . '</a>';
}
protected function renderImage($block)
{
if (isset($block['refkey'])) {
if (($ref = $this->lookupReference($block['refkey'])) !== false) {
$block = array_merge($block, $ref);
} else {
return $block['orig'];
}
}
return '<img src="' . htmlspecialchars($block['url'], ENT_COMPAT | ENT_HTML401, 'UTF-8') . '"'
. ' alt="' . htmlspecialchars($block['text'], ENT_COMPAT | ENT_HTML401 | ENT_SUBSTITUTE, 'UTF-8') . '"'
. (empty($block['title']) ? '' : ' title="' . htmlspecialchars($block['title'], ENT_COMPAT | ENT_HTML401 | ENT_SUBSTITUTE, 'UTF-8') . '"')
. ($this->html5 ? '>' : ' />');
}
// references
protected function identifyReference($line)
{
return ($line[0] === ' ' || $line[0] === '[') && preg_match('/^ {0,3}\[(.+?)\]:\s*([^\s]+?)(?:\s+[\'"](.+?)[\'"])?\s*$/', $line);
}
/**
* Consume link references
*/
protected function consumeReference($lines, $current)
{
while (isset($lines[$current]) && preg_match('/^ {0,3}\[(.+?)\]:\s*(.+?)(?:\s+[\(\'"](.+?)[\)\'"])?\s*$/', $lines[$current], $matches)) {
$label = strtolower($matches[1]);
$this->references[$label] = [
'url' => $this->replaceEscape($matches[2]),
];
if (isset($matches[3])) {
$this->references[$label]['title'] = $matches[3];
} else {
// title may be on the next line
if (isset($lines[$current + 1]) && preg_match('/^\s+[\(\'"](.+?)[\)\'"]\s*$/', $lines[$current + 1], $matches)) {
$this->references[$label]['title'] = $matches[1];
$current++;
}
}
$current++;
}
return [false, --$current];
}
}