$tabSize = 2 $tabStr = " " # indent regexp tests $indentExp = [ /^module\b/, /(=\s*|^)if\b/, /(=\s*|^)until\b/, /(=\s*|^)for\b/, /(=\s*|^)unless\b/, /(=\s*|^)while\b/, /(=\s*|^)begin\b/, /(=\s*|^)case\b/, /\bthen\b/, /^class\b/, /^rescue\b/, /^def\b/, /\bdo\b/, /^else\b/, /^elsif\b/, /^ensure\b/, /\bwhen\b/, /\{[^\}]*$/, /\[[^\]]*$/ ] # outdent regexp tests $outdentExp = [ /^rescue\b/, /^ensure\b/, /^elsif\b/, /^end\b/, /^else\b/, /\bwhen\b/, /^[^\{]*\}/, /^[^\[]*\]/ ] def makeTab(tab) return (tab < 0)?"":$tabStr * $tabSize * tab end def addLine(line,tab) line.strip! line = makeTab(tab)+line if line.length > 0 return line + "\n" end def format_ruby commentBlock = false multiLineArray = Array.new multiLineStr = "" tab = 0 ############ #filename entered on command line as argument gets passed in Ruby's #special argument array ARGV. The ARGV[0] zeroth element has the filename ############ filename = ARGV[0] source = File.new(filename,'r').read dest = "" source.split("\n").each do |line| # combine continuing lines if(!(line =~ /^\s*#/) && line =~ /[^\\]\\\s*$/) puts "hereeeeer" #multiLineArray.push line #multiLineStr += line.sub(/^(.*)\\\s*$/,"\\1") next end # add final line if(multiLineStr.length > 0) puts "HERLKJHLJKEHR" #multiLineArray.push line #multiLineStr += line.sub(/^(.*)\\\s*$/,"\\1") end tline = ((multiLineStr.length > 0)?multiLineStr:line).strip if(tline =~ /^=begin/) commentBlock = true end if(commentBlock) # add the line unchanged dest += line + "\n" else commentLine = (tline =~ /^#/) if(!commentLine) # throw out sequences that will # only sow confusion tline.gsub!(/\/.*?\//,"") tline.gsub!(/%r\{.*?\}/,"") tline.gsub!(/%r(.).*?\1/,"") tline.gsub!(/\\\"/,"'") tline.gsub!(/".*?"/,"\"\"") tline.gsub!(/'.*?'/,"''") tline.gsub!(/#\{.*?\}/,"") $outdentExp.each do |re| if(tline =~ re) tab -= 1 break end end end if (multiLineArray.length > 0) multiLineArray.each do |ml| dest += addLine(ml,tab) end multiLineArray.clear multiLineStr = "" else dest += addLine(line,tab) end if(!commentLine) $indentExp.each do |re| if(tline =~ re && !(tline =~ /\s+end\s*$/)) tab += 1 break end end end end if(tline =~ /^=end/) commentBlock = false end end #STDOUT.write(dest) #File.open(filename, 'w') {|fw| fw.write(dest)} puts "File #{ARGV[0]} has been formatted." # uncomment this to complain about mismatched blocks if(tab != 0) STDERR.puts "Indentation error: #{tab}" end end format_ruby