niquola-rbeautify 0.0.7

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