|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* @package s9e\TextFormatter |
|
5
|
|
|
* @copyright Copyright (c) 2010-2018 The s9e Authors |
|
6
|
|
|
* @license http://www.opensource.org/licenses/mit-license.php The MIT License |
|
7
|
|
|
*/ |
|
8
|
|
|
namespace s9e\TextFormatter\Plugins\Preg; |
|
9
|
|
|
|
|
10
|
|
|
use s9e\TextFormatter\Plugins\ParserBase; |
|
11
|
|
|
|
|
12
|
|
|
class Parser extends ParserBase |
|
13
|
|
|
{ |
|
14
|
|
|
/** |
|
15
|
|
|
* {@inheritdoc} |
|
16
|
|
|
*/ |
|
17
|
15 |
|
public function parse($text, array $matches) |
|
18
|
|
|
{ |
|
19
|
15 |
|
foreach ($this->config['generics'] as list($tagName, $regexp, $passthroughIdx, $map)) |
|
20
|
|
|
{ |
|
21
|
15 |
|
preg_match_all($regexp, $text, $matches, PREG_SET_ORDER | PREG_OFFSET_CAPTURE); |
|
22
|
|
|
|
|
23
|
15 |
|
foreach ($matches as $m) |
|
24
|
|
|
{ |
|
25
|
15 |
|
$startTagPos = $m[0][1]; |
|
26
|
15 |
|
$matchLen = strlen($m[0][0]); |
|
27
|
|
|
|
|
28
|
15 |
|
if ($passthroughIdx && isset($m[$passthroughIdx]) && $m[$passthroughIdx][0] !== '') |
|
29
|
|
|
{ |
|
30
|
|
|
// Compute the position and length of the start tag, end tag, and the content in |
|
31
|
|
|
// between. PREG_OFFSET_CAPTURE gives us the position of the content, and we |
|
32
|
|
|
// know its length. Everything before is considered part of the start tag, and |
|
33
|
|
|
// everything after is considered part of the end tag |
|
34
|
9 |
|
$contentPos = $m[$passthroughIdx][1]; |
|
35
|
9 |
|
$contentLen = strlen($m[$passthroughIdx][0]); |
|
36
|
9 |
|
$startTagLen = $contentPos - $startTagPos; |
|
37
|
9 |
|
$endTagPos = $contentPos + $contentLen; |
|
38
|
9 |
|
$endTagLen = $matchLen - ($startTagLen + $contentLen); |
|
39
|
|
|
|
|
40
|
9 |
|
$tag = $this->parser->addTagPair($tagName, $startTagPos, $startTagLen, $endTagPos, $endTagLen, -100); |
|
41
|
|
|
} |
|
42
|
|
|
else |
|
43
|
|
|
{ |
|
44
|
6 |
|
$tag = $this->parser->addSelfClosingTag($tagName, $startTagPos, $matchLen, -100); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
15 |
|
foreach ($map as $i => $attrName) |
|
48
|
|
|
{ |
|
49
|
15 |
|
if ($attrName && isset($m[$i]) && $m[$i][0] !== '') |
|
50
|
|
|
{ |
|
51
|
15 |
|
$tag->setAttribute($attrName, $m[$i][0]); |
|
52
|
|
|
} |
|
53
|
|
|
} |
|
54
|
|
|
} |
|
55
|
|
|
} |
|
56
|
|
|
} |
|
57
|
|
|
} |