#!/usr/bin/awk -f # PHP indenter. More precisely: "10 minutes hack" awk script for PHP indenting # # Limitation: # Detects PHP code section only if the "" sign is at the begining of the line. # # Result properties: # Will indent with TAB characters. # Not inserting or deleting newline characters. # All newline characters will be \n. # Parses comments and makes indenting inside them. # # If the requirements don't met then it will do nasty things. # Else we hope it will do the right thing. # # Public domain, no warrany. # # Darabos Edvárd Konrád, Hungary # voidnil@yahoo.com # 2006-03-08 function ltrim(s){ i=1; while(i<=length(s)){ c=substr(s,i,1); if(c!=" " && c!="\t") break; i++; } if(i>length(s)) return ""; else return substr(s,i); } #---------------------------------------------------------- function CountChar(s,c){ ours=s; ret=0; while(index(ours,c)!=0){ ret++; ours=substr(ours,index(ours,c)+1); } return ret; } #---------------------------------------------------------- BEGIN{ depth=1; # PHP lines begins at least with one TAB in my codes. That's my style. inphp=0; # We are in a PHP section } { shortline=ltrim($0); out=$0; if(substr(shortline,1,2)==""){ inphp=0; } else if(inphp==1){ copen=CountChar(shortline,"{"); cclose=CountChar(shortline,"}"); if(cclose>copen) depth+=copen-cclose; for(i=0;i