File: beautifier.rb

package info (click to toggle)
ruby-beautify 0.92.2-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 232 kB
  • ctags: 88
  • sloc: ruby: 1,001; makefile: 4
file content (168 lines) | stat: -rw-r--r-- 3,513 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
#!/usr/bin/env ruby

# Ruby beautifier, version 2.1, 09/11/2006
# Copyright (c) 2006, P. Lutus
# Released under the GPL

# Changes
# Craig Williams
# 12/26/2011
# Modified (very minor) to work with a Sublime Text 2 plugin

# indent regexp tests

@indentExp = [
  /^module\b/,
  /^if\b/,
  /^\@{0,2}[\w\.]*[\s\t]*\=[\s\t]*if\b/,
  /(=\s*|^)until\b/,
  /(=\s*|^)for\b/,
  /^unless\b/,
  /^\@{0,2}[\w\.]*[\s\t]*\=[\s\t]*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)
  (tab < 0) ? "" : @tabStr * @tabSize * tab
end

def addLine(line,tab)
  line.strip!
  line = makeTab(tab)+line if line.length > 0
  line + "\n"
end

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

tab_or_space = ARGV.first
path         = ARGV.last
path.gsub!("\\", "/") if RUBY_PLATFORM =~ /mswin/
contents     = IO.read(path)

if tab_or_space == 'space'
  @tabSize = 2
  @tabStr = " "
else
  @tabSize = 1
  @tabStr = "\t"
end

beautifyRuby(contents)