|
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
|
|
|
abstract class AbstractScript extends AbstractPass |
|
11
|
|
|
{ |
|
12
|
|
|
/** |
|
13
|
|
|
* @var string $longRegexp Regexp used for the long form syntax |
|
14
|
|
|
*/ |
|
15
|
|
|
protected $longRegexp; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* @var string Regexp used for the short form syntax |
|
19
|
|
|
*/ |
|
20
|
|
|
protected $shortRegexp; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @var string Relevant character used by this syntax |
|
24
|
|
|
*/ |
|
25
|
|
|
protected $syntaxChar; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @var string Name of the tag used by this pass |
|
29
|
|
|
*/ |
|
30
|
|
|
protected $tagName; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @param string $tagName Name of the tag used by this pass |
|
34
|
|
|
* @param string $syntaxChar Relevant character used by this syntax |
|
35
|
|
|
* @param string $shortRegexp Regexp used for the short form syntax |
|
36
|
|
|
* @param string $longRegexp Regexp used for the long form syntax |
|
37
|
|
|
* @return void |
|
38
|
|
|
*/ |
|
39
|
10 |
|
protected function parseAbstractScript($tagName, $syntaxChar, $shortRegexp, $longRegexp) |
|
40
|
|
|
{ |
|
41
|
10 |
|
$this->tagName = $tagName; |
|
42
|
10 |
|
$this->syntaxChar = $syntaxChar; |
|
43
|
10 |
|
$this->shortRegexp = $shortRegexp; |
|
44
|
10 |
|
$this->longRegexp = $longRegexp; |
|
45
|
|
|
|
|
46
|
10 |
|
$pos = $this->text->indexOf($this->syntaxChar); |
|
47
|
10 |
|
if ($pos === false) |
|
48
|
|
|
{ |
|
49
|
1 |
|
return; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
9 |
|
$this->parseShortForm($pos); |
|
|
|
|
|
|
53
|
9 |
|
$this->parseLongForm($pos); |
|
|
|
|
|
|
54
|
9 |
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* Parse the long form x^(x) |
|
58
|
|
|
* |
|
59
|
|
|
* This syntax is supported by RDiscount |
|
60
|
|
|
* |
|
61
|
|
|
* @param integer $pos Position of the first relevant character |
|
62
|
|
|
* @return void |
|
63
|
|
|
*/ |
|
64
|
9 |
|
protected function parseLongForm($pos) |
|
65
|
|
|
{ |
|
66
|
9 |
|
$pos = $this->text->indexOf($this->syntaxChar . '(', $pos); |
|
67
|
9 |
|
if ($pos === false) |
|
68
|
|
|
{ |
|
69
|
7 |
|
return; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
5 |
|
preg_match_all($this->longRegexp, $this->text, $matches, PREG_OFFSET_CAPTURE, $pos); |
|
73
|
5 |
|
foreach ($matches[0] as list($match, $matchPos)) |
|
74
|
|
|
{ |
|
75
|
3 |
|
$matchLen = strlen($match); |
|
76
|
|
|
|
|
77
|
3 |
|
$this->parser->addTagPair($this->tagName, $matchPos, 2, $matchPos + $matchLen - 1, 1); |
|
78
|
3 |
|
$this->text->overwrite($matchPos, $matchLen); |
|
79
|
|
|
} |
|
80
|
5 |
|
if (!empty($matches[0])) |
|
81
|
|
|
{ |
|
82
|
3 |
|
$this->parseLongForm($pos); |
|
|
|
|
|
|
83
|
|
|
} |
|
84
|
5 |
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* Parse the short form x^x and x^x^ |
|
88
|
|
|
* |
|
89
|
|
|
* This syntax is supported by most implementations that support superscript |
|
90
|
|
|
* |
|
91
|
|
|
* @param integer $pos Position of the first relevant character |
|
92
|
|
|
* @return void |
|
93
|
|
|
*/ |
|
94
|
9 |
|
protected function parseShortForm($pos) |
|
95
|
|
|
{ |
|
96
|
9 |
|
preg_match_all($this->shortRegexp, $this->text, $matches, PREG_OFFSET_CAPTURE, $pos); |
|
97
|
9 |
|
foreach ($matches[0] as list($match, $matchPos)) |
|
98
|
|
|
{ |
|
99
|
5 |
|
$matchLen = strlen($match); |
|
100
|
5 |
|
$startPos = $matchPos; |
|
101
|
5 |
|
$endLen = (substr($match, -1) === $this->syntaxChar) ? 1 : 0; |
|
102
|
5 |
|
$endPos = $matchPos + $matchLen - $endLen; |
|
103
|
|
|
|
|
104
|
5 |
|
$this->parser->addTagPair($this->tagName, $startPos, 1, $endPos, $endLen); |
|
105
|
|
|
} |
|
106
|
|
|
} |
|
107
|
|
|
} |
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.