r2844 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r2843‎ | r2844 | r2845 >
Date:17:14, 26 March 2004
Author:e23
Status:old
Tags:
Comment:
Code cleanup for strip() and unstrip()
Modified paths:
  • /trunk/phase3/includes/Parser.php (modified) (history)

Diff [purge]

Index: trunk/phase3/includes/Parser.php
@@ -95,112 +95,91 @@
9696 {
9797 return dechex(mt_rand(0, 0x7fffffff)) . dechex(mt_rand(0, 0x7fffffff));
9898 }
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;
117110 $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+
126112 while ( "" != $text ) {
127 - $p = preg_split( "/<\\s*nowiki\\s*>/i", $text, 2 );
 113+ $p = preg_split( "/<\\s*$tag\\s*>/i", $text, 2 );
128114 $stripped .= $p[0];
129115 if ( ( count( $p ) < 2 ) || ( "" == $p[1] ) ) {
130116 $text = "";
131117 } 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;
142122 $text = $q[1];
143123 }
144124 }
 125+ return $stripped;
 126+ }
145127
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();
155137
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>";
165148 }
166 - } else {
167 - $stripped2 = $stripped;
168149 }
169150
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'];
178151
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 );
181157 } else {
182 - $state['prelist'][$state['presecs']] = "<pre>{$q[0]}</pre>";
 158+ $math_content[$marker] = "<math>$content</math>";
183159 }
184 -
185 - $stripped3 .= $state['preunq'] . sprintf("%08X", $state['presecs']);
186 - $stripped2 = $q[1];
187160 }
188161 }
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;
190175 }
191176
192177 function unstrip( $text, &$state )
193178 {
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+ }
196183 }
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 - }
205184 return $text;
206185 }
207186

Status & tagging log