|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @copyright Copyright (c) 2015 Nobuo Kihara |
|
4
|
|
|
* @license https://github.com/softark/creole/blob/master/LICENSE |
|
5
|
|
|
* @link https://github.com/softark/creole#readme |
|
6
|
|
|
*/ |
|
7
|
|
|
|
|
8
|
|
|
namespace softark\creole\inline; |
|
9
|
|
|
|
|
10
|
|
|
// work around https://github.com/facebook/hhvm/issues/1120 |
|
11
|
1 |
|
defined('ENT_HTML401') || define('ENT_HTML401', 0); |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* Adds links and images as well as url markers. |
|
15
|
|
|
* |
|
16
|
|
|
*/ |
|
17
|
|
|
trait LinkTrait |
|
18
|
|
|
{ |
|
19
|
|
|
/** |
|
20
|
|
|
* @var string the base url of the wiki which is used for the internal links. |
|
21
|
|
|
*/ |
|
22
|
|
|
public $wikiUrl = ''; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @var array a list of external wikis referenced in this document. |
|
26
|
|
|
* An element in the list should be a key-value pair in which key is the wiki name |
|
27
|
|
|
* and the value is the url of it. |
|
28
|
|
|
*/ |
|
29
|
|
|
public $externalWikis = []; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @var string regular expression pattern to detect an url |
|
33
|
|
|
* This does not allow '[' and ']' in the url. |
|
34
|
|
|
*/ |
|
35
|
|
|
private $_urlRegexp = <<< REGEXP |
|
36
|
|
|
/(?(R) # in case of recursion match parentheses |
|
37
|
|
|
\(((?>[^\s()]+)|(?R))*\) |
|
38
|
|
|
| # else match a link |
|
39
|
|
|
^(https?|ftp):\/\/(([^\s\[\]\<\>()]+)|(?R))+(?<![\[\]\<\>\.,:;\'"!\?\s]) |
|
40
|
|
|
)/x |
|
41
|
|
|
REGEXP; |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* Parses urls and adds auto linking feature. |
|
45
|
|
|
* @marker http |
|
46
|
|
|
* @marker ftp |
|
47
|
|
|
*/ |
|
48
|
4 |
|
protected function parseUrl($markdown) |
|
49
|
|
|
{ |
|
50
|
4 |
View Code Duplication |
if (!in_array('parseLink', $this->context) && preg_match($this->_urlRegexp, $markdown, $matches)) { |
|
|
|
|
|
|
51
|
|
|
return [ |
|
52
|
3 |
|
['autoUrl', $matches[0]], |
|
53
|
3 |
|
strlen($matches[0]) |
|
54
|
|
|
]; |
|
55
|
|
|
} |
|
56
|
4 |
|
return [['text', substr($markdown, 0, 6)], 6]; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
3 |
|
protected function renderAutoUrl($block) |
|
60
|
|
|
{ |
|
61
|
3 |
|
$href = htmlspecialchars($block[1], ENT_COMPAT | ENT_HTML401, 'UTF-8'); |
|
62
|
3 |
|
$text = htmlspecialchars(urldecode($block[1]), ENT_NOQUOTES | ENT_SUBSTITUTE, 'UTF-8'); |
|
63
|
3 |
|
return "<a href=\"$href\">$text</a>"; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* Parses urls starting with a tilde (~) into a plain text. |
|
68
|
|
|
* @marker ~http |
|
69
|
|
|
* @marker ~ftp |
|
70
|
|
|
*/ |
|
71
|
3 |
|
protected function parseEscapedUrl($markdown) |
|
72
|
|
|
{ |
|
73
|
3 |
|
$text = substr($markdown, 1); |
|
74
|
3 |
View Code Duplication |
if (preg_match($this->_urlRegexp, $text, $matches)) { |
|
|
|
|
|
|
75
|
|
|
return [ |
|
76
|
3 |
|
['text', $matches[0]], |
|
77
|
3 |
|
strlen($matches[0]) + 1 |
|
78
|
|
|
]; |
|
79
|
|
|
} |
|
80
|
|
|
return [['text', substr($markdown, 0, 4)], 4]; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* Parses a link: [[ ... ]] |
|
85
|
|
|
* @marker [[ |
|
86
|
|
|
*/ |
|
87
|
3 |
|
protected function parseLink($markdown) |
|
88
|
|
|
{ |
|
89
|
3 |
|
if (!in_array('parseLink', array_slice($this->context, 1)) && preg_match('/^\[\[(.*?)\]\]/', $markdown, $matches)) { |
|
90
|
|
|
// '[[link]] |
|
91
|
3 |
View Code Duplication |
if (preg_match('/^(.+?)\|(.*)$/', $matches[1], $parts)) { |
|
|
|
|
|
|
92
|
|
|
// 'link|text' pattern |
|
93
|
3 |
|
$url = $parts[1]; |
|
94
|
3 |
|
$text = ($parts[2] == '') ? $parts[1] : $parts[2]; |
|
95
|
|
|
} else { |
|
96
|
|
|
// 'link' only pattern |
|
97
|
3 |
|
$url = $matches[1]; |
|
98
|
3 |
|
$text = $matches[1]; |
|
99
|
|
|
} |
|
100
|
3 |
|
if (!preg_match('/^(https?|ftp):\/\//', $url)) { |
|
101
|
|
|
// not an external link, i.e., a wiki link |
|
102
|
3 |
|
if (preg_match('/^(.*):(.*)$/', $url, $urlMatches)) { |
|
103
|
|
|
// inter wiki link |
|
104
|
3 |
|
$extWiki = $urlMatches[1]; |
|
105
|
3 |
|
if (isset($this->externalWikis[$extWiki])) { |
|
106
|
3 |
|
$url = $this->externalWikis[$extWiki] . urlencode($urlMatches[2]); |
|
107
|
|
|
} else { |
|
108
|
|
|
return [ |
|
109
|
|
|
[ |
|
110
|
2 |
|
'text', |
|
111
|
2 |
|
$matches[0], |
|
112
|
|
|
], |
|
113
|
3 |
|
strlen($matches[0]) |
|
114
|
|
|
]; |
|
115
|
|
|
} |
|
116
|
|
|
} else { |
|
117
|
|
|
// internal link |
|
118
|
3 |
|
$url = $this->wikiUrl . urlencode($url); |
|
119
|
|
|
} |
|
120
|
|
|
} |
|
121
|
|
|
return [ |
|
122
|
|
|
[ |
|
123
|
3 |
|
'link', |
|
124
|
3 |
|
'text' => $this->parseInline($text), |
|
125
|
3 |
|
'url' => $url, |
|
126
|
|
|
], |
|
127
|
3 |
|
strlen($matches[0]) |
|
128
|
|
|
]; |
|
129
|
|
View Code Duplication |
} else { |
|
|
|
|
|
|
130
|
|
|
// remove all starting [ markers to avoid next one to be parsed as link |
|
131
|
|
|
$result = '[['; |
|
132
|
|
|
$i = 2; |
|
133
|
|
|
while (isset($markdown[$i]) && $markdown[$i] == '[') { |
|
134
|
|
|
$result .= '['; |
|
135
|
|
|
$i++; |
|
136
|
|
|
} |
|
137
|
|
|
return [['text', $result], $i]; |
|
138
|
|
|
} |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
/** |
|
142
|
|
|
* Parses an image: {{ ... }} |
|
143
|
|
|
* @marker {{ |
|
144
|
|
|
*/ |
|
145
|
3 |
|
protected function parseImage($markdown) |
|
146
|
|
|
{ |
|
147
|
3 |
|
if (preg_match('/^\{\{(.*?)\}\}/', $markdown, $matches)) { |
|
148
|
|
|
// '{{image}} |
|
149
|
3 |
View Code Duplication |
if (preg_match('/^(.+?)\|(.*)$/', $matches[1], $parts)) { |
|
|
|
|
|
|
150
|
|
|
// 'image|text' pattern |
|
151
|
3 |
|
$url = $parts[1]; |
|
152
|
3 |
|
$text = ($parts[2] == '') ? $parts[1] : $parts[2]; |
|
153
|
|
|
} else { |
|
154
|
|
|
// 'image' only pattern |
|
155
|
2 |
|
$url = $matches[1]; |
|
156
|
2 |
|
$text = $matches[1]; |
|
157
|
|
|
} |
|
158
|
3 |
|
if ($url == $text) { |
|
159
|
2 |
|
if (preg_match('/([^\/]+\.(jpe?g|png|gif))/i', $url, $fileMatches)) { |
|
160
|
2 |
|
$text = $fileMatches[1]; |
|
161
|
|
|
} |
|
162
|
|
|
} |
|
163
|
|
|
return [ |
|
164
|
|
|
[ |
|
165
|
3 |
|
'image', |
|
166
|
3 |
|
'text' => $text, |
|
167
|
3 |
|
'url' => $url, |
|
168
|
|
|
], |
|
169
|
3 |
|
strlen($matches[0]) |
|
170
|
|
|
]; |
|
171
|
|
View Code Duplication |
} else { |
|
|
|
|
|
|
172
|
|
|
// remove all starting [ markers to avoid next one to be parsed as link |
|
173
|
|
|
$result = '{{'; |
|
174
|
|
|
$i = 2; |
|
175
|
|
|
while (isset($markdown[$i]) && $markdown[$i] == '{') { |
|
176
|
|
|
$result .= '['; |
|
177
|
|
|
$i++; |
|
178
|
|
|
} |
|
179
|
|
|
return [['text', $result], $i]; |
|
180
|
|
|
} |
|
181
|
|
|
} |
|
182
|
|
|
|
|
183
|
|
|
protected function renderUrl($block) |
|
184
|
|
|
{ |
|
185
|
|
|
$url = htmlspecialchars($block[1], ENT_COMPAT | ENT_HTML401, 'UTF-8'); |
|
186
|
|
|
$text = htmlspecialchars(urldecode($block[1]), ENT_NOQUOTES | ENT_SUBSTITUTE, 'UTF-8'); |
|
187
|
|
|
return "<a href=\"$url\">$text</a>"; |
|
188
|
|
|
} |
|
189
|
|
|
|
|
190
|
3 |
|
protected function renderLink($block) |
|
191
|
|
|
{ |
|
192
|
3 |
|
return '<a href="' . htmlspecialchars($block['url'], ENT_COMPAT | ENT_HTML401, 'UTF-8') . '"' |
|
193
|
3 |
|
. '>' . $this->renderAbsy($block['text']) . '</a>'; |
|
194
|
|
|
} |
|
195
|
|
|
|
|
196
|
3 |
|
protected function renderImage($block) |
|
197
|
|
|
{ |
|
198
|
3 |
|
return '<img src="' . htmlspecialchars($block['url'], ENT_COMPAT | ENT_HTML401, 'UTF-8') . '"' |
|
199
|
3 |
|
. ' alt="' . htmlspecialchars($block['text'], ENT_COMPAT | ENT_HTML401 | ENT_SUBSTITUTE, 'UTF-8') . '"' |
|
200
|
3 |
|
. ($this->html5 ? '>' : ' />'); |
|
|
|
|
|
|
201
|
|
|
} |
|
202
|
|
|
|
|
203
|
|
|
abstract protected function parseInline($text); |
|
204
|
|
|
|
|
205
|
|
|
abstract protected function renderAbsy($absy); |
|
206
|
|
|
} |
|
207
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: