Added PHP beautifier: will process whole code through it once i'll test it properly...
authorHarvie <tomas@mudrunka.cz>
Mon, 1 Nov 2010 16:09:37 +0000 (17:09 +0100)
committerHarvie <tomas@mudrunka.cz>
Mon, 1 Nov 2010 16:09:37 +0000 (17:09 +0100)
scripts/beautify.php [new file with mode: 0755]

diff --git a/scripts/beautify.php b/scripts/beautify.php
new file mode 100755 (executable)
index 0000000..787f238
--- /dev/null
@@ -0,0 +1,101 @@
+#!/usr/bin/php
+//Downloaded from http://shadsplace.org/beautify-php/beautify.phps
+<?php
+function publicProcessHandler($str, $indent)
+{
+    // placeholders prevent strings and comments from being processed
+    preg_match_all("/\/\*.*?\*\/|\/\/[^\n]*|#[^\n]|([\"'])[^\\\\]*?(\\\\.[^\\\\]*?)*?\\1/s", $str, $matches);
+    $matches[0]=array_values(array_unique($matches[0]));
+    for ($i=0;$i<count($matches[0]);$i++){
+        $patterns[]="/".preg_quote($matches[0][$i], '/')."/";
+        $placeholders[]="'placeholder$i'";
+        // double backslashes must be escaped if we want to use them in the replacement argument
+        $matches[0][$i]=str_replace('\\\\', '\\\\\\\\', $matches[0][$i]);
+    }
+
+    if ($placeholders){
+        $str=preg_replace($patterns, $placeholders, $str);
+    }
+
+    //parsing and indenting
+    $str=privateIndentParsedString(privateParseString($str), $indent);
+
+    // insert original strings and comments
+    for ($i=count($placeholders)-1;$i>=0;$i--){
+        $placeholders[$i]="/".$placeholders[$i]."/";
+    }
+
+    if ($placeholders){
+        $str=preg_replace($placeholders, $matches[0], $str);
+
+    }
+    return $str;
+}
+function privateParseString($str)
+{
+    // inserting missing braces (does only match up to 2 nested parenthesis)
+    $str=preg_replace("/(if|for|while|switch)\s*(\([^()]*(\([^()]*\)[^()]*)*\))([^{;]*;)/i", "\\1 \\2 {\\4\n}", $str);
+    // missing braces for else statements
+    $str=preg_replace("/(else)\s*([^{;]*;)/i", "\\1 {\\2\n}", $str);
+
+    // line break check
+    $str=preg_replace("/([;{}]|case\s[^:]+:)\n?/i", "\\1\n", $str);
+    $str=preg_replace("/^function\s+([^\n]+){/mi", "function \\1\n{", $str);
+
+    // remove inserted line breaks at else and for statements
+    $str=preg_replace("/}\s*else\s*/m", "} else ", $str);
+    $str=preg_replace("/(for\s*\()([^;]+;)(\s*)([^;]+;)(\s*)/mi", "\\1\\2 \\4 ", $str);
+
+    // remove spaces between function call and parenthesis and start of argument list
+    $str=preg_replace("/(\w+)\s*\(\s*/", "\\1(", $str);
+
+    // remove line breaks between condition and brace,
+    // set one space between control keyword and condition
+    $str=preg_replace("/(if|for|while|switch)\s*(\([^{]+\))\s*{/i", "\\1 \\2 {", $str);
+
+    return $str;
+}
+function privateIndentParsedString($str, $indent)
+{
+    $count = substr_count($str, '}')-substr_count($str, '{');
+    if ($count<0){
+        $count = 0;
+    }
+
+    $strarray=explode("\n", $str);
+
+    for($i=0;$i<count($strarray);$i++){
+        $strarray[$i]=trim($strarray[$i]);
+        if (strstr($strarray[$i], '}')){
+            $count--;
+        }
+        if (preg_match("/^case\s/i", $strarray[$i])){
+            $level=str_repeat("\t", $indent*($count-1));
+        } else if (preg_match("/^or\s/i", $strarray[$i])){
+            $level=str_repeat("\t", $indent*($count+1));
+        } else {
+            $level=str_repeat("\t", $indent*$count);
+        }
+        $strarray[$i]=$level.$strarray[$i];
+        if (strstr($strarray[$i], '{')){
+            $count++;
+        }
+    }
+    $parsedstr=implode("\n", $strarray);
+    return $parsedstr;
+}
+$indent=1;
+
+if(substr($argv[1],-3) == 'php')
+    $strarray = file($argv[1]);
+else {
+        $str = file_get_contents('php://stdin');
+        $strarray = explode("\n",$str);
+}
+for($i=0;$i<count($strarray);$i++){
+    $strarray[$i]=trim($strarray[$i]);
+}
+$str=implode("\n", $strarray);
+$str=publicProcessHandler($str, $indent);
+echo ($str);
+?>
This page took 0.141321 seconds and 4 git commands to generate.