1 | <?php |
||
18 | class Parser |
||
19 | { |
||
20 | /** |
||
21 | * @var string |
||
22 | */ |
||
23 | private $text = ''; |
||
24 | |||
25 | /** |
||
26 | * @var \SplStack |
||
27 | */ |
||
28 | private $transformedTextStack; |
||
29 | |||
30 | /** |
||
31 | * @var \SplStack |
||
32 | */ |
||
33 | private $blockTypeStack; |
||
34 | |||
35 | /** |
||
36 | * @var \SplStack |
||
37 | */ |
||
38 | private $blockAttributesStack; |
||
39 | |||
40 | /** |
||
41 | * Parser constructor. |
||
42 | * |
||
43 | * Sets up the stacks |
||
44 | */ |
||
45 | 6 | public function __construct() |
|
51 | |||
52 | /** |
||
53 | * Function called on the start of an element. |
||
54 | * |
||
55 | * Mostly used to prepend text to things, like the "*"s on LIs |
||
56 | * |
||
57 | * @see xml_set_element_handler() |
||
58 | * |
||
59 | * @param resource $parser |
||
60 | * @param string $name |
||
61 | * @param array $attrs |
||
62 | */ |
||
63 | 6 | public function startElement($parser, $name, $attrs) |
|
74 | |||
75 | /** |
||
76 | * Called when we begin a block. |
||
77 | * |
||
78 | * We build a series of stacks to represent the tree of the document |
||
79 | * |
||
80 | * We operate at this level of the stack when we're editing content |
||
81 | * |
||
82 | * @param string $name |
||
83 | * @param array $attributes |
||
84 | */ |
||
85 | 6 | private function blockBegin($name, $attributes) |
|
91 | |||
92 | /** |
||
93 | * Append some text to the current level of the stack. |
||
94 | * |
||
95 | * @param string $value |
||
96 | */ |
||
97 | 6 | private function appendBlockText($value) |
|
101 | |||
102 | /** |
||
103 | * Set the text for a block. |
||
104 | * |
||
105 | * @param string $value |
||
106 | */ |
||
107 | 6 | private function setBlockText($value) |
|
112 | |||
113 | /** |
||
114 | * Get the current text that's in this block. |
||
115 | * |
||
116 | * @return string |
||
117 | */ |
||
118 | 6 | private function getBlockText() |
|
122 | |||
123 | /** |
||
124 | * When we reach a closing element do something. |
||
125 | * |
||
126 | * This is mostly used to add stuff to the end of a statement, like putting new lines where div tags close |
||
127 | * |
||
128 | * @see xml_set_element_handler() |
||
129 | * |
||
130 | * @param resource $parser |
||
131 | * @param string $name |
||
132 | */ |
||
133 | 6 | public function endElement($parser, $name) |
|
168 | |||
169 | /** |
||
170 | * Get the transformed text off the stack, and clear down the other stacks. |
||
171 | * |
||
172 | * @return string |
||
173 | */ |
||
174 | 6 | private function blockFinished() |
|
182 | |||
183 | /** |
||
184 | * This converts character data to human character data. |
||
185 | * |
||
186 | * Primarily this is used for removing newlines and replacing them with spaces. |
||
187 | * |
||
188 | * @see xml_set_character_data_handler() |
||
189 | * |
||
190 | * @param resource $parser |
||
191 | * @param string $data |
||
192 | */ |
||
193 | 6 | public function characterData($parser, $data) |
|
197 | |||
198 | /** |
||
199 | * This gets the text that has been parsed and returns it. |
||
200 | * |
||
201 | * @return string |
||
202 | */ |
||
203 | 6 | public function getText() |
|
210 | } |
||
211 |