Top Level Namespace

Includes:
ChopErrors, RBeautify

Defined Under Namespace

Modules: ChopErrors, Knife, Logging, RBeautify, RBeautifyMatchers Classes: Chef, FalseClass, Hash, Object, String, TrueClass

Constant Summary

Constants included from RBeautify

RBeautify::VERSION

Instance Method Summary collapse

Methods included from RBeautify

beautify_string

Instance Method Details

#addLine(line, tab) ⇒ Object



57
58
59
60
61
# 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) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# 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) ⇒ Object



53
54
55
# File 'lib/ruby-beautify/lib/beautifier.rb', line 53

def makeTab(tab)
  (tab < 0) ? "" : @tabStr * @tabSize * tab
end

#run_fixtures_for_language(language) ⇒ Object



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# 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