Changeset 588

Show
Ignore:
Timestamp:
09/24/07 16:03:59 (1 year ago)
Author:
laurentj
Message:

improved jbuildtools : extra-whitespace are removed when using the stripcomments flag

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/build/lib/jManifest.class.php

    r313 r588  
    9595        $result = ''; 
    9696        $firstcomment= true; 
     97        $currentWhistpace =''; 
     98        $firstPHPfound = false; 
    9799        foreach ($tokens as $token) { 
    98100            if (is_string($token)) { 
     101                if(in_array($token, array('(',')','{','}')) && strpos($currentWhitespace, "\n") === false) { 
     102                   $currentWhitespace=''; 
     103                } 
     104                if($currentWhitespace != '') { 
     105                    $s = self::strip_ws($currentWhitespace); 
     106                    $result.=$s; 
     107                    $currentWhitespace =''; 
     108                } 
    99109                $result.=$token; 
    100110            } else { 
    101111                switch ($token[0]) { 
     112                    case T_OPEN_TAG: 
     113                        if($currentWhitespace != '') { 
     114                            $s = self::strip_ws($currentWhitespace); 
     115                            $result.=$s; 
     116                            $currentWhitespace =''; 
     117                        } 
     118                        $result.=$token[1]; 
     119                        if(!$firstPHPfound) { 
     120                            $result.= "/* comments & extra-whitespaces have been removed by jBuildTools*/\n"; 
     121                            $firstPHPfound=true; 
     122                        } 
     123                        break; 
    102124                    case T_COMMENT: 
     125                        $currentWhitespace.="\n"; 
    103126                        break; 
    104127                    case T_DOC_COMMENT: 
    105128                        // on garde le premier commentaire documentaire 
    106129                        if($firstcomment){ 
     130                            if($currentWhitespace != '') { 
     131                                $s = self::strip_ws($currentWhitespace); 
     132                                $result.=$s; 
     133                                $currentWhitespace =''; 
     134                            } 
    107135                            $result.=$token[1]; 
    108136                            $firstcomment = false; 
    109137                        } 
    110138                        break; 
     139                    case T_WHITESPACE: 
     140                        $currentWhitespace.=$token[1]; 
     141                        break; 
    111142                    default: 
     143                        if($currentWhitespace != '') { 
     144                            $s = self::strip_ws($currentWhitespace); 
     145                            $result.=$s; 
     146                            $currentWhitespace =''; 
     147                        } 
    112148                        $result.=$token[1]; 
    113149                        break; 
     
    116152        } 
    117153        return $result; 
     154    } 
    118155 
     156    static protected function strip_ws($s){ 
     157        $result = $s; 
     158        $result = str_replace("\n\r","\n",$result); // removed \r 
     159        $result = str_replace("\r","\n",$result); // removed standalone \r 
     160        $result = preg_replace("(\n+)", "\n", $result); 
     161        $result = str_replace("\t","    ",$result); 
     162        $result = str_replace("    ","\t",$result); 
     163        $result = preg_replace("/^([\n \t]+)\n([ \t]*)$/", "\n$2", $result); 
     164        return $result; 
    119165    } 
    120166} 
  • trunk/build/lib/jManifest.class.php

    r313 r588  
    9595        $result = ''; 
    9696        $firstcomment= true; 
     97        $currentWhistpace =''; 
     98        $firstPHPfound = false; 
    9799        foreach ($tokens as $token) { 
    98100            if (is_string($token)) { 
     101                if(in_array($token, array('(',')','{','}')) && strpos($currentWhitespace, "\n") === false) { 
     102                   $currentWhitespace=''; 
     103                } 
     104                if($currentWhitespace != '') { 
     105                    $s = self::strip_ws($currentWhitespace); 
     106                    $result.=$s; 
     107                    $currentWhitespace =''; 
     108                } 
    99109                $result.=$token; 
    100110            } else { 
    101111                switch ($token[0]) { 
     112                    case T_OPEN_TAG: 
     113                        if($currentWhitespace != '') { 
     114                            $s = self::strip_ws($currentWhitespace); 
     115                            $result.=$s; 
     116                            $currentWhitespace =''; 
     117                        } 
     118                        $result.=$token[1]; 
     119                        if(!$firstPHPfound) { 
     120                            $result.= "/* comments & extra-whitespaces have been removed by jBuildTools*/\n"; 
     121                            $firstPHPfound=true; 
     122                        } 
     123                        break; 
    102124                    case T_COMMENT: 
     125                        $currentWhitespace.="\n"; 
    103126                        break; 
    104127                    case T_DOC_COMMENT: 
    105128                        // on garde le premier commentaire documentaire 
    106129                        if($firstcomment){ 
     130                            if($currentWhitespace != '') { 
     131                                $s = self::strip_ws($currentWhitespace); 
     132                                $result.=$s; 
     133                                $currentWhitespace =''; 
     134                            } 
    107135                            $result.=$token[1]; 
    108136                            $firstcomment = false; 
    109137                        } 
    110138                        break; 
     139                    case T_WHITESPACE: 
     140                        $currentWhitespace.=$token[1]; 
     141                        break; 
    111142                    default: 
     143                        if($currentWhitespace != '') { 
     144                            $s = self::strip_ws($currentWhitespace); 
     145                            $result.=$s; 
     146                            $currentWhitespace =''; 
     147                        } 
    112148                        $result.=$token[1]; 
    113149                        break; 
     
    116152        } 
    117153        return $result; 
     154    } 
    118155 
     156    static protected function strip_ws($s){ 
     157        $result = $s; 
     158        $result = str_replace("\n\r","\n",$result); // removed \r 
     159        $result = str_replace("\r","\n",$result); // removed standalone \r 
     160        $result = preg_replace("(\n+)", "\n", $result); 
     161        $result = str_replace("\t","    ",$result); 
     162        $result = str_replace("    ","\t",$result); 
     163        $result = preg_replace("/^([\n \t]+)\n([ \t]*)$/", "\n$2", $result); 
     164        return $result; 
    119165    } 
    120166} 
Download in other formats: Unified Diff Zip Archive