Index: trunk/phase3/includes/Parser.php |
— | — | @@ -95,112 +95,91 @@ |
96 | 96 | { |
97 | 97 | return dechex(mt_rand(0, 0x7fffffff)) . dechex(mt_rand(0, 0x7fffffff)); |
98 | 98 | } |
99 | | - |
100 | | - # Strips <nowiki>, <pre> and <math> |
101 | | - # Returns the text, and fills an array with data needed in unstrip() |
102 | | - # |
103 | | - function strip( $text, &$state ) |
104 | | - { |
105 | | - $state = array( |
106 | | - 'nwlist' => array(), |
107 | | - 'nwsecs' => 0, |
108 | | - 'nwunq' => Parser::getRandomString(), |
109 | | - 'mathlist' => array(), |
110 | | - 'mathsecs' => 0, |
111 | | - 'mathunq' => Parser::getRandomString(), |
112 | | - 'prelist' => array(), |
113 | | - 'presecs' => 0, |
114 | | - 'preunq' => Parser::getRandomString() |
115 | | - ); |
116 | | - $render = ($this->mOutputType == OT_HTML); |
| 99 | + |
| 100 | + # Replaces all occurences of <$tag>content</$tag> in the text |
| 101 | + # with a random marker and returns the new text. the output parameter |
| 102 | + # $content will be an associative array filled with data on the form |
| 103 | + # $unique_marker => content. |
| 104 | + |
| 105 | + /* static */ function extractTags($tag, $text, &$content, $uniq_prefix = ""){ |
| 106 | + $result = array(); |
| 107 | + $rnd = $uniq_prefix . Parser::getRandomString(); |
| 108 | + $content = array( ); |
| 109 | + $n = 1; |
117 | 110 | $stripped = ""; |
118 | | - $stripped2 = ""; |
119 | | - $stripped3 = ""; |
120 | | - |
121 | | - # Replace any instances of the placeholders |
122 | | - $text = str_replace( $state['nwunq'], wfHtmlEscapeFirst( $state['nwunq'] ), $text ); |
123 | | - $text = str_replace( $state['mathunq'], wfHtmlEscapeFirst( $state['mathunq'] ), $text ); |
124 | | - $text = str_replace( $state['preunq'], wfHtmlEscapeFirst( $state['preunq'] ), $text ); |
125 | | - |
| 111 | + |
126 | 112 | while ( "" != $text ) { |
127 | | - $p = preg_split( "/<\\s*nowiki\\s*>/i", $text, 2 ); |
| 113 | + $p = preg_split( "/<\\s*$tag\\s*>/i", $text, 2 ); |
128 | 114 | $stripped .= $p[0]; |
129 | 115 | if ( ( count( $p ) < 2 ) || ( "" == $p[1] ) ) { |
130 | 116 | $text = ""; |
131 | 117 | } else { |
132 | | - $q = preg_split( "/<\\/\\s*nowiki\\s*>/i", $p[1], 2 ); |
133 | | - ++$state['nwsecs']; |
134 | | - |
135 | | - if ( $render ) { |
136 | | - $state['nwlist'][$state['nwsecs']] = wfEscapeHTMLTagsOnly($q[0]); |
137 | | - } else { |
138 | | - $state['nwlist'][$state['nwsecs']] = "<nowiki>{$q[0]}</nowiki>"; |
139 | | - } |
140 | | - |
141 | | - $stripped .= $state['nwunq'] . sprintf("%08X", $state['nwsecs']); |
| 118 | + $q = preg_split( "/<\\/\\s*$tag\\s*>/i", $p[1], 2 ); |
| 119 | + $marker = $rnd . sprintf("%08X", $n++); |
| 120 | + $content[$marker] = $q[0]; |
| 121 | + $stripped .= $marker; |
142 | 122 | $text = $q[1]; |
143 | 123 | } |
144 | 124 | } |
| 125 | + return $stripped; |
| 126 | + } |
145 | 127 | |
146 | | - if( $this->mOptions->getUseTeX() ) { |
147 | | - while ( "" != $stripped ) { |
148 | | - $p = preg_split( "/<\\s*math\\s*>/i", $stripped, 2 ); |
149 | | - $stripped2 .= $p[0]; |
150 | | - if ( ( count( $p ) < 2 ) || ( "" == $p[1] ) ) { |
151 | | - $stripped = ""; |
152 | | - } else { |
153 | | - $q = preg_split( "/<\\/\\s*math\\s*>/i", $p[1], 2 ); |
154 | | - ++$state['mathsecs']; |
| 128 | + # Strips <nowiki>, <pre> and <math> |
| 129 | + # Returns the text, and fills an array with data needed in unstrip() |
| 130 | + # |
| 131 | + function strip( $text, &$state ) |
| 132 | + { |
| 133 | + $render = ($this->mOutputType == OT_HTML); |
| 134 | + $nowiki_content = array(); |
| 135 | + $math_content = array(); |
| 136 | + $pre_content = array(); |
155 | 137 | |
156 | | - if ( $render ) { |
157 | | - $state['mathlist'][$state['mathsecs']] = renderMath($q[0]); |
158 | | - } else { |
159 | | - $state['mathlist'][$state['mathsecs']] = "<math>{$q[0]}</math>"; |
160 | | - } |
161 | | - |
162 | | - $stripped2 .= $state['mathunq'] . sprintf("%08X", $state['mathsecs']); |
163 | | - $stripped = $q[1]; |
164 | | - } |
| 138 | + # Replace any instances of the placeholders |
| 139 | + $uniq_prefix = "NaodW29"; |
| 140 | + $text = str_replace( $uniq_prefix, wfHtmlEscapeFirst( $uniq_prefix ), $text ); |
| 141 | + |
| 142 | + $text = Parser::extractTags("nowiki", $text, $nowiki_content, $uniq_prefix); |
| 143 | + foreach( $nowiki_content as $marker => $content ){ |
| 144 | + if( $render ){ |
| 145 | + $nowiki_content[$marker] = wfEscapeHTMLTagsOnly( $content ); |
| 146 | + } else { |
| 147 | + $nowiki_content[$marker] = "<nowiki>$content</nowiki>"; |
165 | 148 | } |
166 | | - } else { |
167 | | - $stripped2 = $stripped; |
168 | 149 | } |
169 | 150 | |
170 | | - while ( "" != $stripped2 ) { |
171 | | - $p = preg_split( "/<\\s*pre\\s*>/i", $stripped2, 2 ); |
172 | | - $stripped3 .= $p[0]; |
173 | | - if ( ( count( $p ) < 2 ) || ( "" == $p[1] ) ) { |
174 | | - $stripped2 = ""; |
175 | | - } else { |
176 | | - $q = preg_split( "/<\\/\\s*pre\\s*>/i", $p[1], 2 ); |
177 | | - ++$state['presecs']; |
178 | 151 | |
179 | | - if ( $render ) { |
180 | | - $state['prelist'][$state['presecs']] = "<pre>". wfEscapeHTMLTagsOnly($q[0]). "</pre>\n"; |
| 152 | + if( true or $this->mOptions->getUseTeX() ){ |
| 153 | + $text = Parser::extractTags("math", $text, $math_content, $uniq_prefix); |
| 154 | + foreach( $math_content as $marker => $content ){ |
| 155 | + if( $render ){ |
| 156 | + $math_content[$marker] = renderMath( $content ); |
181 | 157 | } else { |
182 | | - $state['prelist'][$state['presecs']] = "<pre>{$q[0]}</pre>"; |
| 158 | + $math_content[$marker] = "<math>$content</math>"; |
183 | 159 | } |
184 | | - |
185 | | - $stripped3 .= $state['preunq'] . sprintf("%08X", $state['presecs']); |
186 | | - $stripped2 = $q[1]; |
187 | 160 | } |
188 | 161 | } |
189 | | - return $stripped3; |
| 162 | + |
| 163 | + $text = Parser::extractTags("pre", $text, $pre_content, $uniq_prefix); |
| 164 | + foreach( $pre_content as $marker => $content ){ |
| 165 | + if( $render ){ |
| 166 | + $pre_content[$marker] = "<pre>" . wfEscapeHTMLTagsOnly( $content ) . "</pre>"; |
| 167 | + } else { |
| 168 | + $pre_content[$marker] = "<pre>$content</pre>"; |
| 169 | + } |
| 170 | + } |
| 171 | + |
| 172 | + $state = array( $nowiki_content, $math_content, $pre_content ); |
| 173 | + |
| 174 | + return $text; |
190 | 175 | } |
191 | 176 | |
192 | 177 | function unstrip( $text, &$state ) |
193 | 178 | { |
194 | | - for ( $i = 1; $i <= $state['presecs']; ++$i ) { |
195 | | - $text = str_replace( $state['preunq'] . sprintf("%08X", $i), $state['prelist'][$i], $text ); |
| 179 | + foreach( $state as $content_dict ){ |
| 180 | + foreach( $content_dict as $marker => $content ){ |
| 181 | + $text = str_replace( $marker, $content, $text ); |
| 182 | + } |
196 | 183 | } |
197 | | - |
198 | | - for ( $i = 1; $i <= $state['mathsecs']; ++$i ) { |
199 | | - $text = str_replace( $state['mathunq'] . sprintf("%08X", $i), $state['mathlist'][$i], $text ); |
200 | | - } |
201 | | - |
202 | | - for ( $i = 1; $i <= $state['nwsecs']; ++$i ) { |
203 | | - $text = str_replace( $state['nwunq'] . sprintf("%08X", $i), $state['nwlist'][$i], $text ); |
204 | | - } |
205 | 184 | return $text; |
206 | 185 | } |
207 | 186 | |