1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @package s9e\TextFormatter |
5
|
|
|
* @copyright Copyright (c) 2010-2017 The s9e Authors |
6
|
|
|
* @license http://www.opensource.org/licenses/mit-license.php The MIT License |
7
|
|
|
*/ |
8
|
|
|
namespace s9e\TextFormatter\Plugins\Litedown\Parser\Passes; |
9
|
|
|
|
10
|
|
|
use s9e\TextFormatter\Plugins\Litedown\Parser\LinkAttributesSetter; |
11
|
|
|
|
12
|
|
|
class Images extends AbstractPass |
13
|
|
|
{ |
14
|
|
|
use LinkAttributesSetter; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* {@inheritdoc} |
18
|
|
|
*/ |
19
|
263 |
|
public function parse() |
20
|
|
|
{ |
21
|
263 |
|
$pos = $this->text->indexOf(' !== false) |
|
|
|
|
27
|
24 |
|
{ |
28
|
13 |
|
$this->parseInlineImages(); |
29
|
13 |
|
} |
30
|
24 |
|
if ($this->text->hasReferences) |
31
|
24 |
|
{ |
32
|
11 |
|
$this->parseReferenceImages(); |
33
|
11 |
|
} |
34
|
24 |
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Add an image tag for given text span |
38
|
|
|
* |
39
|
|
|
* @param integer $startPos Start tag position |
40
|
|
|
* @param integer $endPos End tag position |
41
|
|
|
* @param integer $endLen End tag length |
42
|
|
|
* @param string $linkInfo URL optionally followed by space and a title |
43
|
|
|
* @param string $alt Value for the alt attribute |
44
|
|
|
* @return void |
45
|
|
|
*/ |
46
|
24 |
|
protected function addImageTag($startPos, $endPos, $endLen, $linkInfo, $alt) |
47
|
|
|
{ |
48
|
24 |
|
$tag = $this->parser->addTagPair('IMG', $startPos, 2, $endPos, $endLen); |
49
|
24 |
|
$this->setLinkAttributes($tag, $linkInfo, 'src'); |
50
|
24 |
|
$tag->setAttribute('alt', $this->text->decode($alt)); |
51
|
|
|
|
52
|
|
|
// Overwrite the markup |
53
|
24 |
|
$this->text->overwrite($startPos, $endPos + $endLen - $startPos); |
54
|
24 |
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Parse inline images markup |
58
|
|
|
* |
59
|
|
|
* @return void |
60
|
|
|
*/ |
61
|
13 |
|
protected function parseInlineImages() |
62
|
|
|
{ |
63
|
13 |
|
preg_match_all( |
64
|
13 |
|
'/!\\[(?:[^\\x17[\\]]|\\[[^\\x17[\\]]*\\])*\\]\\(( *(?:[^\\x17\\s()]|\\([^\\x17\\s()]*\\))*(?=[ )]) *(?:"[^\\x17]*?"|\'[^\\x17]*?\'|\\([^\\x17)]*\\))? *)\\)/', |
65
|
13 |
|
$this->text, |
66
|
13 |
|
$matches, |
67
|
13 |
|
PREG_OFFSET_CAPTURE | PREG_SET_ORDER |
68
|
13 |
|
); |
69
|
13 |
|
foreach ($matches as $m) |
70
|
|
|
{ |
71
|
13 |
|
$linkInfo = $m[1][0]; |
72
|
13 |
|
$startPos = $m[0][1]; |
73
|
13 |
|
$endLen = 3 + strlen($linkInfo); |
74
|
13 |
|
$endPos = $startPos + strlen($m[0][0]) - $endLen; |
75
|
13 |
|
$alt = substr($m[0][0], 2, strlen($m[0][0]) - $endLen - 2); |
76
|
|
|
|
77
|
13 |
|
$this->addImageTag($startPos, $endPos, $endLen, $linkInfo, $alt); |
78
|
13 |
|
} |
79
|
13 |
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Parse reference images markup |
83
|
|
|
* |
84
|
|
|
* @return void |
85
|
|
|
*/ |
86
|
11 |
|
protected function parseReferenceImages() |
87
|
|
|
{ |
88
|
11 |
|
preg_match_all( |
89
|
11 |
|
'/!\\[((?:[^\\x17[\\]]|\\[[^\\x17[\\]]*\\])*)\\](?: ?\\[([^\\x17[\\]]+)\\])?/', |
90
|
11 |
|
$this->text, |
91
|
11 |
|
$matches, |
92
|
11 |
|
PREG_OFFSET_CAPTURE | PREG_SET_ORDER |
93
|
11 |
|
); |
94
|
11 |
|
foreach ($matches as $m) |
95
|
|
|
{ |
96
|
11 |
|
$startPos = $m[0][1]; |
97
|
11 |
|
$endPos = $startPos + 2 + strlen($m[1][0]); |
98
|
11 |
|
$endLen = 1; |
99
|
11 |
|
$alt = $m[1][0]; |
100
|
11 |
|
$id = $alt; |
101
|
|
|
|
102
|
11 |
|
if (isset($m[2][0], $this->text->linkReferences[$m[2][0]])) |
103
|
11 |
|
{ |
104
|
8 |
|
$endLen = strlen($m[0][0]) - strlen($alt) - 2; |
105
|
8 |
|
$id = $m[2][0]; |
106
|
8 |
|
} |
107
|
4 |
|
elseif (!isset($this->text->linkReferences[$id])) |
108
|
|
|
{ |
109
|
1 |
|
continue; |
110
|
|
|
} |
111
|
|
|
|
112
|
11 |
|
$this->addImageTag($startPos, $endPos, $endLen, $this->text->linkReferences[$id], $alt); |
113
|
11 |
|
} |
114
|
|
|
} |
115
|
|
|
} |
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.