1 | <?php |
||
13 | class Parser |
||
14 | { |
||
15 | |||
16 | /** |
||
17 | * @var string |
||
18 | */ |
||
19 | private $text = ""; |
||
20 | |||
21 | /** |
||
22 | * @var \SplStack |
||
23 | */ |
||
24 | private $transformedTextStack; |
||
25 | |||
26 | /** |
||
27 | * @var \SplStack |
||
28 | */ |
||
29 | private $blockTypeStack; |
||
30 | |||
31 | /** |
||
32 | * @var \SplStack |
||
33 | */ |
||
34 | private $blockAttributesStack; |
||
35 | |||
36 | /** |
||
37 | * Parser constructor. |
||
38 | * |
||
39 | * Sets up the stacks |
||
40 | */ |
||
41 | 6 | public function __construct() |
|
47 | |||
48 | /** |
||
49 | * Function called on the start of an element |
||
50 | * |
||
51 | * Mostly used to prepend text to things, like the "*"s on LIs |
||
52 | * |
||
53 | * @see xml_set_element_handler() |
||
54 | * |
||
55 | * @param resource $parser |
||
56 | * @param string $name |
||
57 | * @param array $attrs |
||
58 | */ |
||
59 | 6 | public function startElement($parser, $name, $attrs) |
|
69 | |||
70 | /** |
||
71 | * Called when we begin a block. |
||
72 | * |
||
73 | * We build a series of stacks to represent the tree of the document |
||
74 | * |
||
75 | * We operate at this level of the stack when we're editing content |
||
76 | * |
||
77 | * @param string $name |
||
78 | * @param array $attributes |
||
79 | */ |
||
80 | 6 | private function blockBegin($name, $attributes) |
|
86 | |||
87 | /** |
||
88 | * Append some text to the current level of the stack |
||
89 | * |
||
90 | * @param string $value |
||
91 | */ |
||
92 | 6 | private function appendBlockText($value) |
|
96 | |||
97 | /** |
||
98 | * Set the text for a block |
||
99 | * |
||
100 | * @param string $value |
||
101 | */ |
||
102 | 6 | private function setBlockText($value) |
|
107 | |||
108 | /** |
||
109 | * Get the current text that's in this block |
||
110 | * |
||
111 | * @return string |
||
112 | */ |
||
113 | 6 | private function getBlockText() |
|
117 | |||
118 | /** |
||
119 | * When we reach a closing element do something |
||
120 | * |
||
121 | * This is mostly used to add stuff to the end of a statement, like putting new lines where div tags close |
||
122 | * |
||
123 | * @see xml_set_element_handler() |
||
124 | * |
||
125 | * @param resource $parser |
||
126 | * @param string $name |
||
127 | */ |
||
128 | 6 | public function endElement($parser, $name) |
|
159 | |||
160 | /** |
||
161 | * Get the transformed text off the stack, and clear down the other stacks |
||
162 | * |
||
163 | * @return string |
||
164 | */ |
||
165 | 6 | private function blockFinished() |
|
173 | |||
174 | /** |
||
175 | * This converts character data to human character data |
||
176 | * |
||
177 | * Primarily this is used for removing newlines and replacing them with spaces. |
||
178 | * |
||
179 | * @see xml_set_character_data_handler() |
||
180 | * |
||
181 | * @param resource $parser |
||
182 | * @param string $data |
||
183 | */ |
||
184 | 6 | public function characterData($parser, $data) |
|
188 | |||
189 | /** |
||
190 | * This gets the text that has been parsed and returns it |
||
191 | * |
||
192 | * @return string |
||
193 | */ |
||
194 | 6 | public function getText() |
|
201 | } |
||
202 |