# HG changeset patch # User thomas_-_s # Date 1324992538 0 # Node ID f55b7f34a3e1ea297a4bba43f15f0aed380a0801 # Parent f69bf02134452c511fed30cf7da05277b50b87a7 Updated Ruby Script Beautifier to version 2.9. git-svn-id: svn://svn.code.sf.net/p/universalindent/code/trunk@1054 59b1889a-e5ac-428c-b0c7-476e01d41282 diff -r f69bf0213445 -r f55b7f34a3e1 indenters/example.rb --- a/indenters/example.rb Tue Dec 27 10:48:25 2011 +0000 +++ b/indenters/example.rb Tue Dec 27 13:28:58 2011 +0000 @@ -1,229 +1,224 @@ -#!/usr/bin/env ruby -#odd assignments -BEGIN { - puts "a block i have never seen used" -} -entry = Post.update(params["id"],{:title => params["title"],:post => params['post'],:context => params["context"],:creator => session[:creator]}) -moo = if false - "Sdf" -elsif true - "df" -end -defintion = "moo" -puts moo -moo = case 3 -when 2 - "unless proceeding to 3" -when 3 - "right" -when 4 - "one to many" -when 5 - "three sir" -end - -puts moo -def pointless_call - if false - "Sdf" - elsif true - "df" - end -end -puts pointless_call - -if true - puts "moo" -end -i = 5 -def title - doc = load_page - title = doc.search("h1").first.inner_html - clean_html_tags(title) - clean_9_0(title) - title -end -if i - if true - puts "moo" - elsif i < 3 * 23 - "sdf" - else - "df" - end -end -class Tested - def sadf - "asdf" - end -end - -module Moo - def t434t - "352" - end#comments at the end -end #comments again debug_if - -=begin -block comments - should have no formatting done -ever -=end - -#java formatter test parts - -ping(argument) {|block| - -} - -if (moo) - cow; -else - dog; -end - -x = 5 -x = 5 -x = 5 -x = 5 -IN_OUTS_RX = /^(def|class|module|begin|case|if|unless|loop|while|until|for)/ - -#end java formatter test parts - -here_doc = <<-EOX -This should not -loose its formatting -EOX -dsfffffffff=[2, 3, 4, 5] -print <<-STRING1, <<-STRING2 -Concat -STRING1 -enate -STRING2 - -unless false - "4" -else - "5" -end -x = 2 -while x > 0 - x -= 1 - if x == 1 - "p" - else - "3" - end -end -x = 2 -until x < 0 - x -= 1 -end -a = 3 -a *= 2 while a < 100 -a -= 10 until a < 100 -print "Hello\n" while false -print "Goodbye\n" while false -3.times do - print "Ho! " -end -0.upto(9) do | x| - print x, " " -end -0.step(12, 3) {|x | print x, " " } -x = 0 -loop { - if x == 5 - break - end - x += 1 -} -(1..4).each {|x| - puts x -} -(1..4).each do | x| - puts x -end -for i in (1..4) - puts i -end -i = 0 -loop do - i += 1 - next if i < 3 - print i - break if i > 4 -end -string = "x+1" -begin - eval string -rescue SyntaxError, NameError => boom - print "String doesn't compile: " + boom -rescue StandardError => bang - print "Error running script: " + bang -ensure - print "thank you pick axe" -end -a = "Fats ' ' \\\" do Waller" -a =~ /\/a/ -if true then print "a" end -x = 3 -unless true then print "a" end -x = 3 -begin raise "cow" -rescue Exception => e -end -x = 3 -puts i += 1 while i < 3 # ruby -x = 3 -klass = Fixnum -#its like a do while loop -begin - print klass - klass = klass.superclass - print " < " if klass -end while klass -puts -p Fixnum.ancestors -boom = %q / this is a spinal tap/ -boom = %q - string- -boom =%q(a (nested) string) -x = "done with string" - -puts "In parent,term = #{ENV['TERM']}" -cow = if true - "moot" -else - "woot" -end - -fork do - puts "Start of child 1,term=#{ENV['TERM']}" - ENV['TERM'] = "ansi" - fork do - puts "Start of child 2, term=#{ENV['TERM']}" - begin - if moo < 3 - p "asdf4" - elsif 9 * 0 - p "asde" - else - puts cow - end - end while x > 3 - end - Process.wait - puts "End of child 1, term=#{ENV['TERM']}" -end -Process.wait -puts "Back in parent, term=#{ENV['TERM']}" - -OPENOFFICE = true # do Openoffice - Spreadsheet Tests? -EXCEL = true # do Excel Tests? -GOOGLE = true # do Google - Spreadsheet Tests? - -OPENOFFICEWRITE = false # experimental: - -END{ - puts "another block i have never seen" +#!/usr/bin/env ruby +#odd assignments +BEGIN { + puts "a block i have never seen used" +} +entry = Post.update(params["id"],{:title => params["title"],:post => params['post'],:context => params["context"],:creator => session[:creator]}) +definition = "moo" +puts moo +moo = case 3 +when 2 + "unless proceeding to 3" +when 3 + "right" +when 4 + "one to many" +when 5 + "three sir" +end + +puts moo +def pointless_call + if false + "Sdf" + elsif true + "df" + end +end +puts pointless_call + +if true + puts "moo" +end +i = 5 +def title + doc = load_page + title = doc.search("h1").first.inner_html + clean_html_tags(title) + clean_9_0(title) + title +end +if i + if true + puts "moo" + elsif i < 3 * 23 + "sdf" + else + "df" + end +end +class Tested + def sadf + "asdf" + end +end + +module Moo + def t434t + "352" + end#comments at the end +end #comments again debug_if + +=begin +block comments + should have no formatting done +ever +=end + +#java formatter test parts + +ping(argument) {|block| + +} + +if (moo) + cow; +else + dog; +end + +x = 5 +x = 5 +x = 5 +x = 5 +IN_OUTS_RX = /^(def|class|module|begin|case|if|unless|loop|while|until|for)/ + +#end java formatter test parts + +here_doc = <<-EOX +This should not +loose its formatting +EOX +dsfffffffff=[2, 3, 4, 5] +print <<-STRING1, <<-STRING2 +Concat +STRING1 +enate +STRING2 + +unless false + "4" +else + "5" +end +x = 2 +while x > 0 + x -= 1 + if x == 1 + "p" + else + "3" + end +end +x = 2 +until x < 0 + x -= 1 +end +a = 3 +a *= 2 while a < 100 +a -= 10 until a < 100 +print "Hello\n" while false +print "Goodbye\n" while false +3.times do + print "Ho! " +end +0.upto(9) do | x| + print x, " " +end +0.step(12, 3) {|x | print x, " " } +x = 0 +loop { + if x == 5 + break + end + x += 1 +} +(1..4).each {|x| + puts x +} +(1..4).each do | x| + puts x +end +for i in (1..4) + puts i +end +i = 0 +loop do + i += 1 + next if i < 3 + print i + break if i > 4 +end +string = "x+1" +begin + eval string +rescue SyntaxError, NameError => boom + print "String doesn't compile: " + boom +rescue StandardError => bang + print "Error running script: " + bang +ensure + print "thank you pick axe" +end +a = "Fats ' ' \\\" do Waller" +a =~ /\/a/ +if true then print "a" end +x = 3 +unless true then print "a" end +x = 3 +begin raise "cow" +rescue Exception => e +end +x = 3 +puts i += 1 while i < 3 # ruby +x = 3 +klass = Fixnum +#its like a do while loop +begin + print klass + klass = klass.superclass + print " < " if klass +end while klass +puts +p Fixnum.ancestors +boom = %q / this is a spinal tap/ +boom = %q - string- +boom =%q(a (nested) string) +x = "done with string" + +puts "In parent,term = #{ENV['TERM']}" +cow = if true + "moot" +else + "woot" +end + +fork do + puts "Start of child 1,term=#{ENV['TERM']}" + ENV['TERM'] = "ansi" + fork do + puts "Start of child 2, term=#{ENV['TERM']}" + begin + if moo < 3 + p "asdf4" + elsif 9 * 0 + p "asde" + else + puts cow + end + end while x > 3 + end + Process.wait + puts "End of child 1, term=#{ENV['TERM']}" +end +Process.wait +puts "Back in parent, term=#{ENV['TERM']}" + +OPENOFFICE = true # do Openoffice - Spreadsheet Tests? +EXCEL = true # do Excel Tests? +GOOGLE = true # do Google - Spreadsheet Tests? + +OPENOFFICEWRITE = false # experimental: + +END{ + puts "another block i have never seen" } \ No newline at end of file diff -r f69bf0213445 -r f55b7f34a3e1 indenters/rbeautify.rb --- a/indenters/rbeautify.rb Tue Dec 27 10:48:25 2011 +0000 +++ b/indenters/rbeautify.rb Tue Dec 27 13:28:58 2011 +0000 @@ -1,186 +1,222 @@ -#!/usr/bin/ruby -w - -# NEED_SYMLINK - -=begin -/*************************************************************************** - * Copyright (C) 2006, Paul Lutus * - * * - * This program is free software; you can redistribute it and/or modify * - * it under the terms of the GNU General Public License as published by * - * the Free Software Foundation; either version 2 of the License, or * - * (at your option) any later version. * - * * - * This program is distributed in the hope that it will be useful, * - * but WITHOUT ANY WARRANTY; without even the implied warranty of * - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * - * GNU General Public License for more details. * - * * - * You should have received a copy of the GNU General Public License * - * along with this program; if not, write to the * - * Free Software Foundation, Inc., * - * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * - ***************************************************************************/ -=end - -PVERSION = "Version 2.2, 10/29/2007" - -$tabSize = 3 -$tabStr = " " - -# indent regexp tests - -$indentExp = [ - /^module\b/, - /^if\b/, - /(=\s*|^)until\b/, - /(=\s*|^)for\b/, - /^unless\b/, - /(=\s*|^)while\b/, - /(=\s*|^)begin\b/, - /(^| )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 beautifyRuby(path) - commentBlock = false - programEnd = false - multiLineArray = Array.new - multiLineStr = "" - tab = 0 - source = File.read(path) - dest = "" - source.split("\n").each do |line| - if(!programEnd) - # detect program end mark - if(line =~ /^__END__$/) - programEnd = true - else - # combine continuing lines - if(!(line =~ /^\s*#/) && line =~ /[^\\]\\\s*$/) - multiLineArray.push line - multiLineStr += line.sub(/^(.*)\\\s*$/,"\\1") - next - end - - # add final line - if(multiLineStr.length > 0) - multiLineArray.push line - multiLineStr += line.sub(/^(.*)\\\s*$/,"\\1") - end - - tline = ((multiLineStr.length > 0)?multiLineStr:line).strip - if(tline =~ /^=begin/) - commentBlock = true - end - end - end - if(commentBlock || programEnd) - # add the line unchanged - dest += line + "\n" - else - commentLine = (tline =~ /^#/) - if(!commentLine) - # throw out sequences that will - # only sow confusion - while tline.gsub!(/\{[^\{]*?\}/,"") - end - while tline.gsub!(/\[[^\[]*?\]/,"") - end - while tline.gsub!(/'.*?'/,"") - end - while tline.gsub!(/".*?"/,"") - end - while tline.gsub!(/\`.*?\`/,"") - end - while tline.gsub!(/\([^\(]*?\)/,"") - end - while tline.gsub!(/\/.*?\//,"") - end - while tline.gsub!(/%r(.).*?\1/,"") - end - # delete end-of-line comments - tline.sub!(/#[^\"]+$/,"") - # convert quotes - 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 - if(source != dest) - # make a backup copy - File.open(path + "~","w") { |f| f.write(source) } - # overwrite the original - File.open(path,"w") { |f| f.write(dest) } - end - if(tab != 0) - STDERR.puts "#{path}: Indentation error: #{tab}" - end -end - -if(!ARGV[0]) - STDERR.puts "usage: Ruby filenames to beautify." - exit 0 -end - -ARGV.each do |path| - beautifyRuby(path) +#!/usr/bin/ruby -w + + +=begin +/*************************************************************************** + * Copyright (C) 2008, Paul Lutus * + * * + * This program is free software; you can redistribute it and/or modify * + * it under the terms of the GNU General Public License as published by * + * the Free Software Foundation; either version 2 of the License, or * + * (at your option) any later version. * + * * + * This program is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * GNU General Public License for more details. * + * * + * You should have received a copy of the GNU General Public License * + * along with this program; if not, write to the * + * Free Software Foundation, Inc., * + * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * + ***************************************************************************/ +=end + +PVERSION = "Version 2.9, 10/24/2008" + +module RBeautify + + # user-customizable values + + RBeautify::TabStr = " " + RBeautify::TabSize = 3 + + # indent regexp tests + + IndentExp = [ + /^module\b/, + /^class\b/, + /^if\b/, + /(=\s*|^)until\b/, + /(=\s*|^)for\b/, + /^unless\b/, + /(=\s*|^)while\b/, + /(=\s*|^)begin\b/, + /(^| )case\b/, + /\bthen\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 RBeautify.rb_make_tab(tab) + return (tab < 0)?"":TabStr * TabSize * tab + end + + def RBeautify.rb_add_line(line,tab) + line.strip! + line = rb_make_tab(tab) + line if line.length > 0 + return line + end + + def RBeautify.beautify_string(source, path = "") + comment_block = false + in_here_doc = false + here_doc_term = "" + program_end = false + multiLine_array = [] + multiLine_str = "" + tab = 0 + output = [] + source.each do |line| + line.chomp! + if(!program_end) + # detect program end mark + if(line =~ /^__END__$/) + program_end = true + else + # combine continuing lines + if(!(line =~ /^\s*#/) && line =~ /[^\\]\\\s*$/) + multiLine_array.push line + multiLine_str += line.sub(/^(.*)\\\s*$/,"\\1") + next + end + + # add final line + if(multiLine_str.length > 0) + multiLine_array.push line + multiLine_str += line.sub(/^(.*)\\\s*$/,"\\1") + end + + tline = ((multiLine_str.length > 0)?multiLine_str:line).strip + if(tline =~ /^=begin/) + comment_block = true + end + if(in_here_doc) + in_here_doc = false if tline =~ %r{\s*#{here_doc_term}\s*} + else # not in here_doc + if tline =~ %r{=\s*<<} + here_doc_term = tline.sub(%r{.*=\s*<<-?\s*([_|\w]+).*},"\\1") + in_here_doc = here_doc_term.size > 0 + end + end + end + end + if(comment_block || program_end || in_here_doc) + # add the line unchanged + output << line + else + comment_line = (tline =~ /^#/) + if(!comment_line) + # throw out sequences that will + # only sow confusion + while tline.gsub!(/\{[^\{]*?\}/,"") + end + while tline.gsub!(/\[[^\[]*?\]/,"") + end + while tline.gsub!(/'.*?'/,"") + end + while tline.gsub!(/".*?"/,"") + end + while tline.gsub!(/\`.*?\`/,"") + end + while tline.gsub!(/\([^\(]*?\)/,"") + end + while tline.gsub!(/\/.*?\//,"") + end + while tline.gsub!(/%r(.).*?\1/,"") + end + # delete end-of-line comments + tline.sub!(/#[^\"]+$/,"") + # convert quotes + tline.gsub!(/\\\"/,"'") + OutdentExp.each do |re| + if(tline =~ re) + tab -= 1 + break + end + end + end + if (multiLine_array.length > 0) + multiLine_array.each do |ml| + output << rb_add_line(ml,tab) + end + multiLine_array.clear + multiLine_str = "" + else + output << rb_add_line(line,tab) + end + if(!comment_line) + IndentExp.each do |re| + if(tline =~ re && !(tline =~ /\s+end\s*$/)) + tab += 1 + break + end + end + end + end + if(tline =~ /^=end/) + comment_block = false + end + end + error = (tab != 0) + STDERR.puts "Error: indent/outdent mismatch: #{tab}." if error + return output.join("\n") + "\n",error + end # beautify_string + + def RBeautify.beautify_file(path) + error = false + if(path == '-') # stdin source + source = STDIN.read + dest,error = beautify_string(source,"stdin") + print dest + else # named file source + source = File.read(path) + dest,error = beautify_string(source,path) + if(source != dest) + # make a backup copy + File.open(path + "~","w") { |f| f.write(source) } + # overwrite the original + File.open(path,"w") { |f| f.write(dest) } + end + end + return error + end # beautify_file + + def RBeautify.main + error = false + if(!ARGV[0]) + STDERR.puts "usage: Ruby filenames or \"-\" for stdin." + exit 0 + end + ARGV.each do |path| + error = (beautify_file(path))?true:error + end + error = (error)?1:0 + exit error + end # main +end # module RBeautify + +# if launched as a standalone program, not loaded as a module +if __FILE__ == $0 + RBeautify.main end \ No newline at end of file diff -r f69bf0213445 -r f55b7f34a3e1 indenters/uigui_rbeautify.ini --- a/indenters/uigui_rbeautify.ini Tue Dec 27 10:48:25 2011 +0000 +++ b/indenters/uigui_rbeautify.ini Tue Dec 27 13:28:58 2011 +0000 @@ -1,17 +1,17 @@ -[header] -categories= -cfgFileParameterEnding=" " -configFilename= -fileTypes=*.rb -indenterFileName=rbeautify.rb -indenterName=Ruby Script Beautifier (Ruby) -inputFileName=indentinput -inputFileParameter= -manual=http://www.arachnoid.com/ruby/rubyBeautifier.html -outputFileName=indentinput -outputFileParameter=none -parameterOrder=pio -showHelpParameter= -stringparaminquotes=false -useCfgFileParameter= -version=2.2 +[header] +categories= +cfgFileParameterEnding=" " +configFilename= +fileTypes=*.rb +indenterFileName=rbeautify.rb +indenterName=Ruby Script Beautifier (Ruby) +inputFileName=indentinput +inputFileParameter= +manual=http://www.arachnoid.com/ruby/rubyBeautifier.html +outputFileName=indentinput +outputFileParameter=none +parameterOrder=pio +showHelpParameter= +stringparaminquotes=false +useCfgFileParameter= +version=2.9