rogerdpack-rbeautify 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. data/bin/rbeautify +222 -0
  2. metadata +54 -0
data/bin/rbeautify ADDED
@@ -0,0 +1,222 @@
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 = "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 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
+ return output.join("\n") + "\n",error
184
+ end # beautify_string
185
+
186
+ def RBeautify.beautify_file(path)
187
+ error = false
188
+ if(path == '-') # stdin source
189
+ source = STDIN.read
190
+ dest,error = beautify_string(source,"stdin")
191
+ print dest
192
+ else # named file source
193
+ source = File.read(path)
194
+ dest,error = beautify_string(source,path)
195
+ if(source != dest)
196
+ # make a backup copy
197
+ File.open(path + "~","w") { |f| f.write(source) }
198
+ # overwrite the original
199
+ File.open(path,"w") { |f| f.write(dest) }
200
+ end
201
+ end
202
+ return error
203
+ end # beautify_file
204
+
205
+ def RBeautify.main
206
+ error = false
207
+ if(!ARGV[0])
208
+ STDERR.puts "usage: Ruby filenames or \"-\" for stdin."
209
+ exit 0
210
+ end
211
+ ARGV.each do |path|
212
+ error = (beautify_file(path))?true:error
213
+ end
214
+ error = (error)?1:0
215
+ exit error
216
+ end # main
217
+ end # module RBeautify
218
+
219
+ # if launched as a standalone program, not loaded as a module
220
+ if __FILE__ == $0
221
+ RBeautify.main
222
+ end
metadata ADDED
@@ -0,0 +1,54 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rogerdpack-rbeautify
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Roger Pack
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-05-22 00:00:00 -07:00
13
+ default_executable:
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
+
25
+ files:
26
+ - bin/rbeautify
27
+ has_rdoc: false
28
+ homepage: http://github.com/rogerdpack/rbeautify
29
+ post_install_message:
30
+ rdoc_options: []
31
+
32
+ require_paths:
33
+ - lib
34
+ required_ruby_version: !ruby/object:Gem::Requirement
35
+ requirements:
36
+ - - ">="
37
+ - !ruby/object:Gem::Version
38
+ version: "0"
39
+ version:
40
+ required_rubygems_version: !ruby/object:Gem::Requirement
41
+ requirements:
42
+ - - ">="
43
+ - !ruby/object:Gem::Version
44
+ version: "0"
45
+ version:
46
+ requirements: []
47
+
48
+ rubyforge_project:
49
+ rubygems_version: 1.2.0
50
+ signing_key:
51
+ specification_version: 2
52
+ summary: prettifier/beautifier for Ruby code [from http://www.arachnoid.com/ruby/]
53
+ test_files: []
54
+