Index: trunk/tools/code-utils/stylize.php |
— | — | @@ -1,3 +1,4 @@ |
| 2 | +#!/usr/bin/php |
2 | 3 | <?php |
3 | 4 | |
4 | 5 | /** |
— | — | @@ -3,10 +4,9 @@ |
4 | 5 | * A PHP code beautifier aimed at adding lots of spaces to files that lack them, |
5 | 6 | * in keeping with MediaWiki's spacey site style. |
6 | | - * |
| 7 | + * |
7 | 8 | * @author Tim Starling |
8 | 9 | * @author Jeroen De Dauw |
9 | 10 | */ |
10 | 11 | |
11 | | - |
12 | 12 | if ( php_sapi_name() != 'cli' ) { |
13 | 13 | echo "This script must be run from the command line\n"; |
— | — | @@ -45,18 +45,18 @@ |
46 | 46 | |
47 | 47 | function stylize_file( $filename, $backup = true ) { |
48 | 48 | echo "Stylizing file $filename\n"; |
49 | | - |
| 49 | + |
50 | 50 | $s = ( $filename == '-' ) |
51 | 51 | ? file_get_contents( '/dev/stdin' ) |
52 | 52 | : file_get_contents( $filename ); |
53 | | - |
| 53 | + |
54 | 54 | if ( $s === false ) { |
55 | 55 | return; |
56 | 56 | } |
57 | | - |
| 57 | + |
58 | 58 | $stylizer = new Stylizer( $s ); |
59 | 59 | $s = $stylizer->stylize(); |
60 | | - |
| 60 | + |
61 | 61 | if ( $filename == '-' ) { |
62 | 62 | echo $s; |
63 | 63 | } else { |
— | — | @@ -133,7 +133,7 @@ |
134 | 134 | ); |
135 | 135 | static $spaceBefore = array( |
136 | 136 | ')', |
137 | | - '-', // $foo = -1; shouldn't change to $foo = - 1; |
| 137 | + '-', // $foo = -1; shouldn't change to $foo = - 1; |
138 | 138 | ); |
139 | 139 | static $spaceAfter = array( |
140 | 140 | '(', |
— | — | @@ -192,7 +192,7 @@ |
193 | 193 | function getPrev() { |
194 | 194 | return $this->get( $this->p - 1 ); |
195 | 195 | } |
196 | | - |
| 196 | + |
197 | 197 | function getNext() { |
198 | 198 | return $this->get( $this->p + 1 ); |
199 | 199 | } |
— | — | @@ -264,12 +264,14 @@ |
265 | 265 | // Add the token contents |
266 | 266 | if ( $curType == T_COMMENT ) { |
267 | 267 | $curText = $this->fixComment( $curText ); |
268 | | - } elseif ( $curType == T_WHITESPACE ) { |
269 | | - $curText = $this->fixWhitespace( $curText ); |
270 | 268 | } |
271 | 269 | |
272 | 270 | $out .= $curText; |
273 | 271 | |
| 272 | + if ( substr( $out, -1 ) === "\n" ) { |
| 273 | + $out = $this->fixWhitespace( $out ); |
| 274 | + } |
| 275 | + |
274 | 276 | $wantSpaceAfter = $this->isSpaceAfter( $curToken ); |
275 | 277 | // Special case: space after =& |
276 | 278 | if ( $prevType == '=' && $curType == '&' ) { |
— | — | @@ -299,6 +301,6 @@ |
300 | 302 | |
301 | 303 | function fixWhitespace( $s ) { |
302 | 304 | // Fix whitespace at the line end |
303 | | - return preg_replace( '!^([\t ]+)(\n.*)$!s', '\2', $s, 1 ); |
| 305 | + return preg_replace( "#[\t ]*\n#", "\n", $s ); |
304 | 306 | } |
305 | 307 | } |