Split parser related files to have one class in one file
[lhc/web/wiklou.git] / includes / parser / PPDStackElement.php
1 <?php
2 /**
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
17 *
18 * @file
19 * @ingroup Parser
20 */
21
22 /**
23 * @ingroup Parser
24 */
25 class PPDStackElement {
26 /**
27 * @var string Opening character (\n for heading)
28 */
29 public $open;
30
31 /**
32 * @var string Matching closing character
33 */
34 public $close;
35
36 /**
37 * @var string Saved prefix that may affect later processing,
38 * e.g. to differentiate `-{{{{` and `{{{{` after later seeing `}}}`.
39 */
40 public $savedPrefix = '';
41
42 /**
43 * @var int Number of opening characters found (number of "=" for heading)
44 */
45 public $count;
46
47 /**
48 * @var PPDPart[] Array of PPDPart objects describing pipe-separated parts.
49 */
50 public $parts;
51
52 /**
53 * @var bool True if the open char appeared at the start of the input line.
54 * Not set for headings.
55 */
56 public $lineStart;
57
58 public $partClass = PPDPart::class;
59
60 public function __construct( $data = [] ) {
61 $class = $this->partClass;
62 $this->parts = [ new $class ];
63
64 foreach ( $data as $name => $value ) {
65 $this->$name = $value;
66 }
67 }
68
69 public function &getAccum() {
70 return $this->parts[count( $this->parts ) - 1]->out;
71 }
72
73 public function addPart( $s = '' ) {
74 $class = $this->partClass;
75 $this->parts[] = new $class( $s );
76 }
77
78 /**
79 * @return PPDPart
80 */
81 public function getCurrentPart() {
82 return $this->parts[count( $this->parts ) - 1];
83 }
84
85 /**
86 * @return array
87 */
88 public function getFlags() {
89 $partCount = count( $this->parts );
90 $findPipe = $this->open != "\n" && $this->open != '[';
91 return [
92 'findPipe' => $findPipe,
93 'findEquals' => $findPipe && $partCount > 1 && !isset( $this->parts[$partCount - 1]->eqpos ),
94 'inHeading' => $this->open == "\n",
95 ];
96 }
97
98 /**
99 * Get the output string that would result if the close is not found.
100 *
101 * @param bool|int $openingCount
102 * @return string
103 */
104 public function breakSyntax( $openingCount = false ) {
105 if ( $this->open == "\n" ) {
106 $s = $this->savedPrefix . $this->parts[0]->out;
107 } else {
108 if ( $openingCount === false ) {
109 $openingCount = $this->count;
110 }
111 $s = substr( $this->open, 0, -1 );
112 $s .= str_repeat(
113 substr( $this->open, -1 ),
114 $openingCount - strlen( $s )
115 );
116 $s = $this->savedPrefix . $s;
117 $first = true;
118 foreach ( $this->parts as $part ) {
119 if ( $first ) {
120 $first = false;
121 } else {
122 $s .= '|';
123 }
124 $s .= $part->out;
125 }
126 }
127 return $s;
128 }
129 }