Commit | Line | Data |
---|---|---|
a2ab786b H |
1 | #!/usr/bin/php |
2 | //Downloaded from http://shadsplace.org/beautify-php/beautify.phps | |
3 | <?php | |
4 | function publicProcessHandler($str, $indent) | |
5 | { | |
6 | // placeholders prevent strings and comments from being processed | |
7 | preg_match_all("/\/\*.*?\*\/|\/\/[^\n]*|#[^\n]|([\"'])[^\\\\]*?(\\\\.[^\\\\]*?)*?\\1/s", $str, $matches); | |
8 | $matches[0]=array_values(array_unique($matches[0])); | |
9 | for ($i=0;$i<count($matches[0]);$i++){ | |
10 | $patterns[]="/".preg_quote($matches[0][$i], '/')."/"; | |
11 | $placeholders[]="'placeholder$i'"; | |
12 | // double backslashes must be escaped if we want to use them in the replacement argument | |
13 | $matches[0][$i]=str_replace('\\\\', '\\\\\\\\', $matches[0][$i]); | |
14 | } | |
15 | ||
16 | if ($placeholders){ | |
17 | $str=preg_replace($patterns, $placeholders, $str); | |
18 | } | |
19 | ||
20 | //parsing and indenting | |
21 | $str=privateIndentParsedString(privateParseString($str), $indent); | |
22 | ||
23 | // insert original strings and comments | |
24 | for ($i=count($placeholders)-1;$i>=0;$i--){ | |
25 | $placeholders[$i]="/".$placeholders[$i]."/"; | |
26 | } | |
27 | ||
28 | if ($placeholders){ | |
29 | $str=preg_replace($placeholders, $matches[0], $str); | |
30 | ||
31 | } | |
32 | return $str; | |
33 | } | |
34 | function privateParseString($str) | |
35 | { | |
36 | // inserting missing braces (does only match up to 2 nested parenthesis) | |
37 | $str=preg_replace("/(if|for|while|switch)\s*(\([^()]*(\([^()]*\)[^()]*)*\))([^{;]*;)/i", "\\1 \\2 {\\4\n}", $str); | |
38 | // missing braces for else statements | |
39 | $str=preg_replace("/(else)\s*([^{;]*;)/i", "\\1 {\\2\n}", $str); | |
40 | ||
41 | // line break check | |
42 | $str=preg_replace("/([;{}]|case\s[^:]+:)\n?/i", "\\1\n", $str); | |
43 | $str=preg_replace("/^function\s+([^\n]+){/mi", "function \\1\n{", $str); | |
44 | ||
45 | // remove inserted line breaks at else and for statements | |
46 | $str=preg_replace("/}\s*else\s*/m", "} else ", $str); | |
47 | $str=preg_replace("/(for\s*\()([^;]+;)(\s*)([^;]+;)(\s*)/mi", "\\1\\2 \\4 ", $str); | |
48 | ||
49 | // remove spaces between function call and parenthesis and start of argument list | |
50 | $str=preg_replace("/(\w+)\s*\(\s*/", "\\1(", $str); | |
51 | ||
52 | // remove line breaks between condition and brace, | |
53 | // set one space between control keyword and condition | |
54 | $str=preg_replace("/(if|for|while|switch)\s*(\([^{]+\))\s*{/i", "\\1 \\2 {", $str); | |
55 | ||
56 | return $str; | |
57 | } | |
58 | function privateIndentParsedString($str, $indent) | |
59 | { | |
60 | $count = substr_count($str, '}')-substr_count($str, '{'); | |
61 | if ($count<0){ | |
62 | $count = 0; | |
63 | } | |
64 | ||
65 | $strarray=explode("\n", $str); | |
66 | ||
67 | for($i=0;$i<count($strarray);$i++){ | |
68 | $strarray[$i]=trim($strarray[$i]); | |
69 | if (strstr($strarray[$i], '}')){ | |
70 | $count--; | |
71 | } | |
72 | if (preg_match("/^case\s/i", $strarray[$i])){ | |
73 | $level=str_repeat("\t", $indent*($count-1)); | |
74 | } else if (preg_match("/^or\s/i", $strarray[$i])){ | |
75 | $level=str_repeat("\t", $indent*($count+1)); | |
76 | } else { | |
77 | $level=str_repeat("\t", $indent*$count); | |
78 | } | |
79 | $strarray[$i]=$level.$strarray[$i]; | |
80 | if (strstr($strarray[$i], '{')){ | |
81 | $count++; | |
82 | } | |
83 | } | |
84 | $parsedstr=implode("\n", $strarray); | |
85 | return $parsedstr; | |
86 | } | |
87 | $indent=1; | |
88 | ||
89 | if(substr($argv[1],-3) == 'php') | |
90 | $strarray = file($argv[1]); | |
91 | else { | |
92 | $str = file_get_contents('php://stdin'); | |
93 | $strarray = explode("\n",$str); | |
94 | } | |
95 | for($i=0;$i<count($strarray);$i++){ | |
96 | $strarray[$i]=trim($strarray[$i]); | |
97 | } | |
98 | $str=implode("\n", $strarray); | |
99 | $str=publicProcessHandler($str, $indent); | |
100 | echo ($str); | |
101 | ?> |