#!/usr/bin/perl # # 簡単なjavascript構文を整形する # use strict; my $file = $ARGV[0]; if ($file eq '') { die "usage: format_javascript.pl [filename]\n\n"; } if (!-e $file) { die "can't find $file\n\n"; } if (!-r $file) { die "can't read $file\n\n"; } my $content = ''; open(IN, "$file") or die "can't open $file: $!\n\n"; while () { $_ =~ s/\r\n//g; $content .= $_; } close(IN); my $content_length = length($content); my @buffer = (); # 何文字バッファするか my $max_buffer = 100; # バッファ内のどの文字を作業文字(カレント)とするか my $buffer_index = 50; # バッファ初期化 for (my $i=0; $i<$max_buffer; $i++) { push(@buffer, ""); } # ここで $#buffer は $max_buffer-1 my $in_comment = 0; my $in_single_quotes = 0; my $in_double_quotes = 0; my $in_regex_quotes = 0; my $in_escape = 0; my $in_forloop = 0; my $brace_count = 0; # 前回の改行検知 my $last_is_linefeed = 0; for (my $i=0; $i<$content_length; $i++) { my $in_literal = 0; shift (@buffer); push (@buffer, substr($content, $i, 1)); my $curr_char = $buffer[$buffer_index]; if (!$in_escape && $curr_char eq '\\') { $in_escape = 1; } else { $in_escape = 0; } ### if (!$in_escape && !$in_comment && $curr_char eq '*' && $buffer[$buffer_index-1] eq '/') { $in_comment = 1; } if (!$in_escape && $in_comment && $curr_char eq '/' && $buffer[$buffer_index-1] eq '*') { $in_comment = 0; } if (!$in_escape && !$in_comment && !$in_single_quotes && !$in_double_quotes && $curr_char eq '"') { $in_double_quotes = 1; } elsif (!$in_escape && !$in_comment && !$in_single_quotes && $in_double_quotes && $curr_char eq '"') { $in_double_quotes = 0; } if (!$in_escape && !$in_comment && !$in_double_quotes && !$in_single_quotes && $curr_char eq "'") { $in_single_quotes = 1; } elsif (!$in_escape && !$in_comment && !$in_double_quotes && $in_single_quotes && $curr_char eq "'") { $in_single_quotes = 0; } if (!$in_escape && !$in_comment && !$in_double_quotes && !$in_single_quotes && $curr_char eq '(') { my $last_three = $buffer[$buffer_index-3]; $last_three.= $buffer[$buffer_index-2]; $last_three.= $buffer[$buffer_index-1]; if ($last_three eq 'for') { $in_forloop = 1; } } if ($in_forloop && $curr_char eq ')') { $in_forloop = 0; } # 検証処理終り if ($in_escape || $in_comment || $in_double_quotes || $in_single_quotes) { $in_literal = 1; } if (!$in_literal && $curr_char eq 'f') { my $tmp = $buffer[$buffer_index+1];#u $tmp.= $buffer[$buffer_index+2];#n $tmp.= $buffer[$buffer_index+3];#c $tmp.= $buffer[$buffer_index+4];#t $tmp.= $buffer[$buffer_index+2];#i $tmp.= $buffer[$buffer_index+2];#o $tmp.= $buffer[$buffer_index+2];#n if ($tmp eq 'unction') { print "\n\n"; } } if (!$in_literal && $curr_char eq '{') { $brace_count++; } if (!$in_literal && $curr_char eq '}') { $brace_count--; } if ($last_is_linefeed) { $last_is_linefeed = 0; for (my $j=0; $j<$brace_count; $j++) { print " "; } } print $curr_char; if (!$in_literal && !$in_forloop && $curr_char eq ";") { print "\n"; $last_is_linefeed = 1; } if (!$in_literal && $curr_char eq '{') { print "\n"; $last_is_linefeed = 1; } if (!$in_literal && $curr_char eq '}') { if (($buffer[$buffer_index+1].$buffer[$buffer_index+2].$buffer[$buffer_index+3]) ne 'els' && ($buffer[$buffer_index+1].$buffer[$buffer_index+2].$buffer[$buffer_index+3].$buffer[$buffer_index+4].$buffer[$buffer_index+5]) ne 'catch') { print "\n"; $last_is_linefeed = 1; } if ($brace_count == 0) { print "\n"; $last_is_linefeed = 1; } } if (!$in_literal && $buffer[$buffer_index+1] eq '}') { print ";\n"; $last_is_linefeed = 1; } } for(my $i=0; $i<($#buffer-$buffer_index); $i++) { print $buffer[$buffer_index+$i]; } exit; =cut $Log: format_javascript.pl,v $ Revision 1.1 2005/04/01 13:02:25 k-ozaki added to cvs =cut