rbeautify 0.0.7

Sign up to get free protection for your applications and to get access to all the features.
data/README ADDED
@@ -0,0 +1,35 @@
1
+ A ruby code beautifier/prettifier cleans ruby code for you [and give it the right indentation, etc.]
2
+
3
+ What it does:
4
+
5
+ you run rbeautify on poorly-aligned code, like
6
+
7
+ def go
8
+ if 3
9
+ stuff
10
+ end
11
+ end
12
+
13
+ and it automatically converts said code to
14
+
15
+ def go
16
+ if 3
17
+ stuff
18
+ end
19
+ end
20
+
21
+ Quite convenient for those of us who edit using editors without auto format. It also
22
+ helps find indentation mismatches [i.e. missing end's and }'s ] by cleaning
23
+ up the code so you can see scopes, and warning you where it sees mismatches [similar to ruby19 -w but better].
24
+
25
+ Many thanks to the original author [I just gemif-ied his existing script].
26
+ http://www.arachnoid.com/ruby/rubyBeautifier.html
27
+
28
+ Installation:
29
+ gem sources -a http://gems.github.com
30
+ sudo gem install rogerdpack-rbeautify
31
+
32
+ Usage:
33
+ rbeautify <script name>
34
+
35
+ Feedback welcome rogerdpack on github
data/Rakefile ADDED
@@ -0,0 +1,9 @@
1
+ require 'jeweler'
2
+ Jeweler::Tasks.new do |s|
3
+ s.name = %q{rbeautify}
4
+ s.authors = ["Roger Pack"]
5
+ s.description = s.summary = %q{prettifier/beautifier for Ruby code [from http://www.arachnoid.com/ruby/]}
6
+ s.email = ["rogerdpack@gmail.comm"]
7
+ s.homepage = %q{http://github.com/rogerdpack/rbeautify}
8
+
9
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.0.7
data/bin/rbeautify ADDED
@@ -0,0 +1,5 @@
1
+ #!/usr/bin/ruby -w
2
+ require 'rbeautify.rb'
3
+
4
+ # always run it, since this is the binary file
5
+ RBeautify.main
data/lib/rbeautify.rb ADDED
@@ -0,0 +1,217 @@
1
+ =begin
2
+ /***************************************************************************
3
+ * Copyright (C) 2008, Paul Lutus *
4
+ * *
5
+ * This program is free software; you can redistribute it and/or modify *
6
+ * it under the terms of the GNU General Public License as published by *
7
+ * the Free Software Foundation; either version 2 of the License, or *
8
+ * (at your option) any later version. *
9
+ * *
10
+ * This program is distributed in the hope that it will be useful, *
11
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13
+ * GNU General Public License for more details. *
14
+ * *
15
+ * You should have received a copy of the GNU General Public License *
16
+ * along with this program; if not, write to the *
17
+ * Free Software Foundation, Inc., *
18
+ * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19
+ ***************************************************************************/
20
+ =end
21
+
22
+ PVERSION = "modified from Version 2.9, 10/24/2008"
23
+
24
+ module RBeautify
25
+
26
+ # user-customizable values
27
+
28
+ RBeautify::TabStr = " "
29
+ RBeautify::TabSize = 2
30
+
31
+ # indent regexp tests
32
+
33
+ IndentExp = [
34
+ /^module\b/,
35
+ /^class\b/,
36
+ /^if\b/,
37
+ /(=\s*|^)until\b/,
38
+ /(=\s*|^)for\b/,
39
+ /^unless\b/,
40
+ /(=\s*|^)while\b/,
41
+ /(=\s*|^)begin\b/,
42
+ /(^| )case\b/,
43
+ /\bthen\b/,
44
+ /^rescue\b/,
45
+ /^def\b/,
46
+ /\bdo\b/,
47
+ /^else\b/,
48
+ /^elsif\b/,
49
+ /^ensure\b/,
50
+ /\bwhen\b/,
51
+ /\{[^\}]*$/,
52
+ /\[[^\]]*$/
53
+ ]
54
+
55
+ # outdent regexp tests
56
+
57
+ OutdentExp = [
58
+ /^rescue\b/,
59
+ /^ensure\b/,
60
+ /^elsif\b/,
61
+ /^end\b/,
62
+ /^else\b/,
63
+ /\bwhen\b/,
64
+ /^[^\{]*\}/,
65
+ /^[^\[]*\]/
66
+ ]
67
+
68
+ def RBeautify.rb_make_tab(tab)
69
+ return (tab < 0)?"":TabStr * TabSize * tab
70
+ end
71
+
72
+ def RBeautify.rb_add_line(line,tab)
73
+ line.strip!
74
+ line = rb_make_tab(tab) + line if line.length > 0
75
+ return line
76
+ end
77
+
78
+ def RBeautify.beautify_string(source, path = "")
79
+ comment_block = false
80
+ in_here_doc = false
81
+ here_doc_term = ""
82
+ program_end = false
83
+ multiLine_array = []
84
+ multiLine_str = ""
85
+ tab = 0
86
+ output = []
87
+ source.each_line do |line|
88
+ line.chomp!
89
+ if(!program_end)
90
+ # detect program end mark
91
+ if(line =~ /^__END__$/)
92
+ program_end = true
93
+ else
94
+ # combine continuing lines
95
+ if(!(line =~ /^\s*#/) && line =~ /[^\\]\\\s*$/)
96
+ multiLine_array.push line
97
+ multiLine_str += line.sub(/^(.*)\\\s*$/,"\\1")
98
+ next
99
+ end
100
+
101
+ # add final line
102
+ if(multiLine_str.length > 0)
103
+ multiLine_array.push line
104
+ multiLine_str += line.sub(/^(.*)\\\s*$/,"\\1")
105
+ end
106
+
107
+ tline = ((multiLine_str.length > 0)?multiLine_str:line).strip
108
+ if(tline =~ /^=begin/)
109
+ comment_block = true
110
+ end
111
+ if(in_here_doc)
112
+ in_here_doc = false if tline =~ %r{\s*#{here_doc_term}\s*}
113
+ else # not in here_doc
114
+ if tline =~ %r{=\s*<<}
115
+ here_doc_term = tline.sub(%r{.*=\s*<<-?\s*([_|\w]+).*},"\\1")
116
+ in_here_doc = here_doc_term.size > 0
117
+ end
118
+ end
119
+ end
120
+ end
121
+ if(comment_block || program_end || in_here_doc)
122
+ # add the line unchanged
123
+ output << line
124
+ else
125
+ comment_line = (tline =~ /^#/)
126
+ if(!comment_line)
127
+ # throw out sequences that will
128
+ # only sow confusion
129
+ while tline.gsub!(/\{[^\{]*?\}/,"")
130
+ end
131
+ while tline.gsub!(/\[[^\[]*?\]/,"")
132
+ end
133
+ while tline.gsub!(/'.*?'/,"")
134
+ end
135
+ while tline.gsub!(/".*?"/,"")
136
+ end
137
+ while tline.gsub!(/\`.*?\`/,"")
138
+ end
139
+ while tline.gsub!(/\([^\(]*?\)/,"")
140
+ end
141
+ while tline.gsub!(/\/.*?\//,"")
142
+ end
143
+ while tline.gsub!(/%r(.).*?\1/,"")
144
+ end
145
+ # delete end-of-line comments
146
+ tline.sub!(/#[^\"]+$/,"")
147
+ # convert quotes
148
+ tline.gsub!(/\\\"/,"'")
149
+ OutdentExp.each do |re|
150
+ if(tline =~ re)
151
+ tab -= 1
152
+ break
153
+ end
154
+ end
155
+ end
156
+ if (multiLine_array.length > 0)
157
+ multiLine_array.each do |ml|
158
+ output << rb_add_line(ml,tab)
159
+ end
160
+ multiLine_array.clear
161
+ multiLine_str = ""
162
+ else
163
+ output << rb_add_line(line,tab)
164
+ end
165
+ if(!comment_line)
166
+ IndentExp.each do |re|
167
+ if(tline =~ re && !(tline =~ /\s+end\s*$/))
168
+ tab += 1
169
+ break
170
+ end
171
+ end
172
+ end
173
+ end
174
+ if(tline =~ /^=end/)
175
+ comment_block = false
176
+ end
177
+ end
178
+ error = (tab != 0)
179
+ STDERR.puts "Error: indent/outdent mismatch: #{tab}." if error
180
+ # attempt to accomodate for DOS style line endings...
181
+ line_joiner = source.include?("\r\n") ? "\r\n" : "\n"
182
+ return output.join(line_joiner) + line_joiner,error
183
+ end # beautify_string
184
+
185
+ def RBeautify.beautify_file(path)
186
+ error = false
187
+ if(path == '-') # stdin source
188
+ source = STDIN.read
189
+ dest,error = beautify_string(source,"stdin")
190
+ print dest
191
+ else # named file source
192
+ source = File.open(path, 'rb') do |file| file.read; end
193
+ dest,error = beautify_string(source,path)
194
+ if(source != dest)
195
+ # make a backup copy
196
+ File.open(path + "~","wb") { |f| f.write(source) }
197
+ # overwrite the original
198
+ File.open(path,"wb") { |f| f.write(dest) }
199
+ end
200
+ end
201
+ return error
202
+ end # beautify_file
203
+
204
+ def RBeautify.main
205
+ error = false
206
+ if(!ARGV[0])
207
+ STDERR.puts "usage: Ruby filenames or \"-\" for stdin."
208
+ exit 0
209
+ end
210
+ ARGV.each do |path|
211
+ error = (beautify_file(path))?true:error
212
+ end
213
+ error = (error)?1:0
214
+ exit error
215
+ end # main
216
+
217
+ end # module RBeautify
data/rbeautify.gemspec ADDED
@@ -0,0 +1,44 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run `rake gemspec`
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{rbeautify}
8
+ s.version = "0.0.7"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Roger Pack"]
12
+ s.date = %q{2009-10-29}
13
+ s.default_executable = %q{rbeautify}
14
+ s.description = %q{prettifier/beautifier for Ruby code [from http://www.arachnoid.com/ruby/]}
15
+ s.email = ["rogerdpack@gmail.comm"]
16
+ s.executables = ["rbeautify"]
17
+ s.extra_rdoc_files = [
18
+ "README"
19
+ ]
20
+ s.files = [
21
+ "README",
22
+ "Rakefile",
23
+ "VERSION",
24
+ "bin/rbeautify",
25
+ "lib/rbeautify.rb",
26
+ "rbeautify.gemspec",
27
+ "spec/beautify.spec"
28
+ ]
29
+ s.homepage = %q{http://github.com/rogerdpack/rbeautify}
30
+ s.rdoc_options = ["--charset=UTF-8"]
31
+ s.require_paths = ["lib"]
32
+ s.rubygems_version = %q{1.3.5}
33
+ s.summary = %q{prettifier/beautifier for Ruby code [from http://www.arachnoid.com/ruby/]}
34
+
35
+ if s.respond_to? :specification_version then
36
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
37
+ s.specification_version = 3
38
+
39
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
40
+ else
41
+ end
42
+ else
43
+ end
44
+ end
@@ -0,0 +1,15 @@
1
+ # NOT DONE YET
2
+ class Test; end
3
+
4
+ describe Test do
5
+
6
+ before do
7
+ #Object.send(:remove_const, 'Klass') rescue nil
8
+ end
9
+
10
+ it "should translate \n to \n" do
11
+
12
+ __DIR__.should_not == nil
13
+ end
14
+
15
+ end
metadata ADDED
@@ -0,0 +1,62 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rbeautify
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.7
5
+ platform: ruby
6
+ authors:
7
+ - Roger Pack
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-10-29 00:00:00 -06:00
13
+ default_executable: rbeautify
14
+ dependencies: []
15
+
16
+ description: prettifier/beautifier for Ruby code [from http://www.arachnoid.com/ruby/]
17
+ email:
18
+ - rogerdpack@gmail.comm
19
+ executables:
20
+ - rbeautify
21
+ extensions: []
22
+
23
+ extra_rdoc_files:
24
+ - README
25
+ files:
26
+ - README
27
+ - Rakefile
28
+ - VERSION
29
+ - bin/rbeautify
30
+ - lib/rbeautify.rb
31
+ - rbeautify.gemspec
32
+ - spec/beautify.spec
33
+ has_rdoc: true
34
+ homepage: http://github.com/rogerdpack/rbeautify
35
+ licenses: []
36
+
37
+ post_install_message:
38
+ rdoc_options:
39
+ - --charset=UTF-8
40
+ require_paths:
41
+ - lib
42
+ required_ruby_version: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ version: "0"
47
+ version:
48
+ required_rubygems_version: !ruby/object:Gem::Requirement
49
+ requirements:
50
+ - - ">="
51
+ - !ruby/object:Gem::Version
52
+ version: "0"
53
+ version:
54
+ requirements: []
55
+
56
+ rubyforge_project:
57
+ rubygems_version: 1.3.5
58
+ signing_key:
59
+ specification_version: 3
60
+ summary: prettifier/beautifier for Ruby code [from http://www.arachnoid.com/ruby/]
61
+ test_files: []
62
+