3 # Ruby beautifier, version 2.1, 09/11/2006
4 # Copyright (c) 2006, P. Lutus
5 # Released under the GPL
34 # outdent regexp tests
48 return (tab < 0)?"":$tabStr * $tabSize * tab
53 line = makeTab(tab)+line if line.length > 0
57 def beautifyRuby(path)
60 multiLineArray = Array.new
63 source = File.read(path)
65 source.split("\n").each do |line|
67 # detect program end mark
68 if(line =~ /^__END__$/)
71 # combine continuing lines
72 if(!(line =~ /^\s*#/) && line =~ /[^\\]\\\s*$/)
73 multiLineArray.push line
74 multiLineStr += line.sub(/^(.*)\\\s*$/,"\\1")
79 if(multiLineStr.length > 0)
80 multiLineArray.push line
81 multiLineStr += line.sub(/^(.*)\\\s*$/,"\\1")
84 tline = ((multiLineStr.length > 0)?multiLineStr:line).strip
85 if(tline =~ /^=begin/)
90 if(commentBlock || programEnd)
91 # add the line unchanged
94 commentLine = (tline =~ /^#/)
96 # throw out sequences that will
98 while tline.gsub!(/'.*?'/,"")
100 while tline.gsub!(/".*?"/,"")
102 while tline.gsub!(/\`.*?\`/,"")
104 while tline.gsub!(/\{[^\{]*?\}/,"")
106 while tline.gsub!(/\([^\(]*?\)/,"")
108 while tline.gsub!(/\/.*?\//,"")
110 while tline.gsub!(/%r(.).*?\1/,"")
112 tline.gsub!(/\\\"/,"'")
113 $outdentExp.each do |re|
120 if (multiLineArray.length > 0)
121 multiLineArray.each do |ml|
122 dest += addLine(ml,tab)
127 dest += addLine(line,tab)
130 $indentExp.each do |re|
131 if(tline =~ re && !(tline =~ /\s+end\s*$/))
144 File.open(path + "~","w") { |f| f.write(source) }
145 # overwrite the original
146 File.open(path,"w") { |f| f.write(dest) }
149 STDERR.puts "#{path}: Indentation error: #{tab}"
154 STDERR.puts "usage: Ruby filenames to beautify."