forked from ruby-amqp/amqp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcleanify.rb
executable file
·50 lines (38 loc) · 982 Bytes
/
cleanify.rb
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
#!/usr/bin/env ruby -i
# encoding: utf-8
# Usage:
# find . | egrep '\.rb$' | egrep -v cleanify.rb | xargs ./bin/cleanify.rb
# \n at the end of the file
# def foo a, b, &block
# no trailing whitespace
# encoding declaration
ENCODING = "utf-8"
while line = ARGF.gets
# whitespace
# line.chomp!
line.gsub!(/\t/, " ")
line.rstrip!
# encoding
if line.length == (ARGF.pos - 1) && ! line.match(/^#.*coding/)
puts "# encoding: #{ENCODING}\n\n"
end
# def
regexp = /^(\s*def \w[\w\d]*)\s+(.+)$/
if line.match(regexp)
line.gsub!(regexp, '\1(\2)')
end
# foo{} => foo {}
line.gsub!(/([^%][^#( ])(\{)/, '\1 \2')
# a=foo => a = foo
line.gsub!(/([^ ])(\+=)/, '\1 \2')
line.gsub!(/(\+=)([^ ])/, '\1 \2')
line.gsub!(/([^ :])(<<)/, '\1 \2')
line.gsub!(/(<<)([^ ])/, '\1 \2')
# foo=>bar
line.gsub!(/([^\s])=>/, '\1 =>')
line.gsub!(/=>([^\s])/, '=> \1')
line.gsub!(/\{\|/, '{ |')
line.gsub!(/,\s*/, ', ')
line.rstrip!
puts line
end