js自动换行缩进

来源:互联网 发布:2016淘宝店怎么引流量 编辑:程序博客网 时间:2024/06/11 19:34

my $infile = "input.js";
my $outfile = "> output.js";
open(INPUT,$infile) or die("读取失败!");
open(OUTPUT,$outfile) or die("写入失败!");
my $ident = 0;

while(<INPUT>)
{
 chomp;
 
 s/([{;])/$1/n/g; #将{和;后添加换行
 s/(?<!/n)/}//n/}/g; #保证}开头
 
 @lines = split //n/;
 
 for my $line (@lines)
 {
  chomp;
  
  # 如果闭合,先--
  if($line=~/^[}]/)
  {
   $ident--;
  }
  
  for($i=0;$i<$ident;$i++)
  {
   print OUTPUT ("/t");
  }
  
  # 开辟新区域++
  if($line=~/[{]$/)
  {
   $ident++;
  }
  
  print OUTPUT $line."/n";
 }
}

close(OUTPUT);
close(INPUT);
 

原创粉丝点击