class Object

Constants

Options

Public Instance Methods

addLine(line,tab) click to toggle source
# File lib/ruby-beautify/lib/beautifier.rb, line 57
def addLine(line,tab)
  line.strip!
  line = makeTab(tab)+line if line.length > 0
  line + "\n"
end
beautifyRuby(contents) click to toggle source
# File lib/ruby-beautify/lib/beautifier.rb, line 63
def beautifyRuby(contents)
  commentBlock = false
  programEnd = false
  multiLineArray = Array.new
  multiLineStr = ""
  tab = 0
  source = contents
  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!(/%r(.).*?\1/,"")
        end
        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)
  # uncomment this to complain about mismatched blocks
  # if(tab != 0)
  #   STDERR.puts "#{path}: Indentation error: #{tab}"
  # end
end
makeTab(tab) click to toggle source
# File lib/ruby-beautify/lib/beautifier.rb, line 53
def makeTab(tab)
  (tab < 0) ? "" : @tabStr * @tabSize * tab
end
run_fixtures_for_language(language) click to toggle source
# File lib/ruby-beautify/spec/spec_helper.rb, line 102
def run_fixtures_for_language(language)
  fixtures = YAML.load_file(File.dirname(__FILE__) + "/fixtures/#{language}.yml")

  describe language do
    fixtures.each do |fixture|
      it "should #{fixture['name']}" do
        input = fixture['input']
        output = fixture['output'] || input
        debug = fixture['debug'] || false

        if fixture['pending']
          pending fixture['pending'] do
            RBeautify.beautify_string(language, input).should == output
          end
        else
          RBeautify::BlockMatcher.debug = debug
          RBeautify.beautify_string(language, input).should == output
        end
      end
    end
  end

end
to_rb() click to toggle source
# File lib/chef/knife/chop/chef_part.rb, line 48
def to_rb
  to_s
end