rekode 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.document +5 -0
- data/.gitignore +21 -0
- data/LICENSE +20 -0
- data/README.rdoc +18 -0
- data/Rakefile +54 -0
- data/VERSION +1 -0
- data/bin/ruby-code-indenter +16 -0
- data/lib/rekode.rb +233 -0
- data/rekode.gemspec +57 -0
- data/test/helper.rb +10 -0
- data/test/test_rekode.rb +7 -0
- metadata +88 -0
data/.document
ADDED
data/.gitignore
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2010 Olle de Zwart
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.rdoc
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
= rekode
|
2
|
+
|
3
|
+
This module is a fork of the kode gem by Antono Vasiljev.
|
4
|
+
It can be used for indenting ruby code (Rekode::Indenter.
|
5
|
+
|
6
|
+
== Note on Patches/Pull Requests
|
7
|
+
|
8
|
+
* Fork the project.
|
9
|
+
* Make your feature addition or bug fix.
|
10
|
+
* Add tests for it. This is important so I don't break it in a
|
11
|
+
future version unintentionally.
|
12
|
+
* Commit, do not mess with rakefile, version, or history.
|
13
|
+
(if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
|
14
|
+
* Send me a pull request. Bonus points for topic branches.
|
15
|
+
|
16
|
+
== Copyright
|
17
|
+
|
18
|
+
Copyright (c) 2010 Olle de Zwart. See LICENSE for details.
|
data/Rakefile
ADDED
@@ -0,0 +1,54 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake'
|
3
|
+
|
4
|
+
begin
|
5
|
+
require 'jeweler'
|
6
|
+
Jeweler::Tasks.new do |gem|
|
7
|
+
gem.name = "rekode"
|
8
|
+
gem.summary = "rekode indents ruby code"
|
9
|
+
gem.description = "rekode indents ruby code"
|
10
|
+
gem.email = "olle@endforward.nl"
|
11
|
+
gem.homepage = "http://github.com/osdezwart/rekode"
|
12
|
+
gem.authors = ["Olle de Zwart"]
|
13
|
+
gem.add_development_dependency "thoughtbot-shoulda", ">= 0"
|
14
|
+
gem.executables = "ruby-code-indenter"
|
15
|
+
# gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
|
16
|
+
end
|
17
|
+
Jeweler::GemcutterTasks.new
|
18
|
+
rescue LoadError
|
19
|
+
puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
|
20
|
+
end
|
21
|
+
|
22
|
+
require 'rake/testtask'
|
23
|
+
Rake::TestTask.new(:test) do |test|
|
24
|
+
test.libs << 'lib' << 'test'
|
25
|
+
test.pattern = 'test/**/test_*.rb'
|
26
|
+
test.verbose = true
|
27
|
+
end
|
28
|
+
|
29
|
+
begin
|
30
|
+
require 'rcov/rcovtask'
|
31
|
+
Rcov::RcovTask.new do |test|
|
32
|
+
test.libs << 'test'
|
33
|
+
test.pattern = 'test/**/test_*.rb'
|
34
|
+
test.verbose = true
|
35
|
+
end
|
36
|
+
rescue LoadError
|
37
|
+
task :rcov do
|
38
|
+
abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
task :test => :check_dependencies
|
43
|
+
|
44
|
+
task :default => :test
|
45
|
+
|
46
|
+
require 'rake/rdoctask'
|
47
|
+
Rake::RDocTask.new do |rdoc|
|
48
|
+
version = File.exist?('VERSION') ? File.read('VERSION') : ""
|
49
|
+
|
50
|
+
rdoc.rdoc_dir = 'rdoc'
|
51
|
+
rdoc.title = "rekode #{version}"
|
52
|
+
rdoc.rdoc_files.include('README*')
|
53
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
54
|
+
end
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.1.0
|
@@ -0,0 +1,16 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
require File.dirname(__FILE__) + '/../lib/rekode'
|
3
|
+
|
4
|
+
if ARGV.any?
|
5
|
+
ARGV.each do |path|
|
6
|
+
Rekode::Indenter.process :file => path, :backup => true
|
7
|
+
end
|
8
|
+
elsif !(code = STDIN.read).empty?
|
9
|
+
puts Rekode::Indenter.process(:text => code)
|
10
|
+
else
|
11
|
+
STDERR.puts "usage: ruby-code-indenter file.rb"
|
12
|
+
STDERR.puts "usage: cat file.rb | ruby-code-indenter"
|
13
|
+
exit 0
|
14
|
+
end
|
15
|
+
|
16
|
+
# vim: syntax=Ruby
|
data/lib/rekode.rb
ADDED
@@ -0,0 +1,233 @@
|
|
1
|
+
=begin
|
2
|
+
/***************************************************************************
|
3
|
+
* Copyright (C) 2006, Paul Lutus *
|
4
|
+
* Copyright (C) 2008, Antono Vasiljev *
|
5
|
+
* Copyright (C) 2010, Olle de Zwart *
|
6
|
+
* *
|
7
|
+
* This program is free software; you can redistribute it and/or modify *
|
8
|
+
* it under the terms of the GNU General Public License as published by *
|
9
|
+
* the Free Software Foundation; either version 2 of the License, or *
|
10
|
+
* (at your option) any later version. *
|
11
|
+
* *
|
12
|
+
* This program is distributed in the hope that it will be useful, *
|
13
|
+
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
|
14
|
+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
|
15
|
+
* GNU General Public License for more details. *
|
16
|
+
* *
|
17
|
+
* You should have received a copy of the GNU General Public License *
|
18
|
+
* along with this program; if not, write to the *
|
19
|
+
* Free Software Foundation, Inc., *
|
20
|
+
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
|
21
|
+
***************************************************************************/
|
22
|
+
=end
|
23
|
+
module Rekode
|
24
|
+
|
25
|
+
VERSION = '0.1'
|
26
|
+
|
27
|
+
class Indenter
|
28
|
+
|
29
|
+
# indent regexp
|
30
|
+
INDENT_EXP = [
|
31
|
+
/^def\b/,
|
32
|
+
/^if\b/,
|
33
|
+
/^else\b/,
|
34
|
+
/^elsif\b/,
|
35
|
+
/\bdo\b/,
|
36
|
+
/^class\b/,
|
37
|
+
/^module\b/,
|
38
|
+
/(=\s*|^)until\b/,
|
39
|
+
/(=\s*|^)for\b/,
|
40
|
+
/^unless\b/,
|
41
|
+
/(=\s*|^)while\b/,
|
42
|
+
/(=\s*|^)begin\b/,
|
43
|
+
/(^| )case\b/,
|
44
|
+
/\bthen\b/,
|
45
|
+
/^rescue\b/,
|
46
|
+
/^ensure\b/,
|
47
|
+
/\bwhen\b/,
|
48
|
+
/\{[^\}]*$/,
|
49
|
+
/\[[^\]]*$/
|
50
|
+
]
|
51
|
+
|
52
|
+
# outdent regexp
|
53
|
+
OUTDENT_EXP = [
|
54
|
+
/^end\b/,
|
55
|
+
/^else\b/,
|
56
|
+
/^elsif\b/,
|
57
|
+
/^rescue\b/,
|
58
|
+
/^ensure\b/,
|
59
|
+
/\bwhen\b/,
|
60
|
+
/^[^\{]*\}/,
|
61
|
+
/^[^\[]*\]/
|
62
|
+
]
|
63
|
+
|
64
|
+
# Creates cleaner. Takes hash of parametrs:
|
65
|
+
#
|
66
|
+
# :indent_char - indent symbol (default: space)
|
67
|
+
#
|
68
|
+
# :indent_size - quantity of indent symbols for single step (default: 2)
|
69
|
+
#
|
70
|
+
def initialize(params = {})
|
71
|
+
@indent_char = params[:indent_char] || " "
|
72
|
+
@indent_size = params[:indent_size] || 2
|
73
|
+
end
|
74
|
+
|
75
|
+
# Cleaning source. Takes hash of parametrs:
|
76
|
+
#
|
77
|
+
# :file - Source code as path to file
|
78
|
+
#
|
79
|
+
# :text - Source code as ruby string
|
80
|
+
#
|
81
|
+
# :backup - makes backup copy if true
|
82
|
+
#
|
83
|
+
# :indent_char - indent symbol (default: " ")
|
84
|
+
#
|
85
|
+
# :indent_size - quantity of indent symbols for single step
|
86
|
+
#
|
87
|
+
#
|
88
|
+
def self.process(params = {})
|
89
|
+
unless params[:file].nil?
|
90
|
+
params[:text] = File.read(params[:file])
|
91
|
+
@path = params[:file]
|
92
|
+
end
|
93
|
+
|
94
|
+
output = self.new(params).process(params[:text])
|
95
|
+
|
96
|
+
if params[:backup] && (output != params[:text]) && params[:file]
|
97
|
+
File.open(params[:file] + ".ugly~","w") { |f| f.write(params[:text]) }
|
98
|
+
File.open(params[:file],"w") { |f| f.write(output) }
|
99
|
+
else
|
100
|
+
return output
|
101
|
+
end
|
102
|
+
|
103
|
+
end
|
104
|
+
|
105
|
+
|
106
|
+
# Cleaning source. Takes source code as ruby string.
|
107
|
+
#
|
108
|
+
def process(source)
|
109
|
+
|
110
|
+
cursor_inside_comment_block = false
|
111
|
+
cursor_at_program_end = false
|
112
|
+
|
113
|
+
multiline_array = []
|
114
|
+
multiline_string = ""
|
115
|
+
|
116
|
+
indent_level = 0
|
117
|
+
dest = ""
|
118
|
+
|
119
|
+
source.each_line do |line|
|
120
|
+
|
121
|
+
unless cursor_at_program_end
|
122
|
+
|
123
|
+
# detect program end mark
|
124
|
+
if line =~ /^__END__$/
|
125
|
+
cursor_at_program_end = true
|
126
|
+
else
|
127
|
+
|
128
|
+
# combine continuing lines
|
129
|
+
if(!(line =~ /^\s*#/) && line =~ /[^\\]\\\s*$/)
|
130
|
+
multiline_array.push line
|
131
|
+
multiline_string += line.sub(/^(.*)\\\s*$/,"\\1")
|
132
|
+
next
|
133
|
+
end
|
134
|
+
|
135
|
+
# add final line
|
136
|
+
if (multiline_string.length > 0)
|
137
|
+
multiline_array.push line
|
138
|
+
multiline_string += line.sub(/^(.*)\\\s*$/,"\\1")
|
139
|
+
end
|
140
|
+
|
141
|
+
tline = ((multiline_string.length > 0) ? multiline_string : line).strip
|
142
|
+
|
143
|
+
cursor_inside_comment_block = true if tline.match(/^=begin/)
|
144
|
+
end
|
145
|
+
end
|
146
|
+
|
147
|
+
if (cursor_inside_comment_block or cursor_at_program_end)
|
148
|
+
dest += line # add the line unchanged
|
149
|
+
else
|
150
|
+
|
151
|
+
cursor_at_comment_line = (tline =~ /^#/)
|
152
|
+
|
153
|
+
unless cursor_at_comment_line
|
154
|
+
# throw out sequences that will
|
155
|
+
# only sow confusion
|
156
|
+
# XXX WTF?
|
157
|
+
while tline.gsub!(/\{[^\{]*?\}/,"")
|
158
|
+
end
|
159
|
+
while tline.gsub!(/\[[^\[]*?\]/,"")
|
160
|
+
end
|
161
|
+
while tline.gsub!(/'.*?'/,"")
|
162
|
+
end
|
163
|
+
while tline.gsub!(/".*?"/,"")
|
164
|
+
end
|
165
|
+
while tline.gsub!(/\`.*?\`/,"")
|
166
|
+
end
|
167
|
+
while tline.gsub!(/\([^\(]*?\)/,"")
|
168
|
+
end
|
169
|
+
while tline.gsub!(/\/.*?\//,"")
|
170
|
+
end
|
171
|
+
while tline.gsub!(/%r(.).*?\1/,"")
|
172
|
+
end
|
173
|
+
|
174
|
+
# delete end-of-line comments
|
175
|
+
tline.sub!(/#[^\"]+$/,"")
|
176
|
+
# convert quotes
|
177
|
+
# WTF?
|
178
|
+
tline.gsub!(/\\\"/,"'")
|
179
|
+
OUTDENT_EXP.each do |re|
|
180
|
+
if (tline =~ re)
|
181
|
+
indent_level -= 1
|
182
|
+
break
|
183
|
+
end
|
184
|
+
end
|
185
|
+
end
|
186
|
+
|
187
|
+
unless multiline_array.empty?
|
188
|
+
multiline_array.each do |ml|
|
189
|
+
dest += add_line(ml,indent_level)
|
190
|
+
end
|
191
|
+
multiline_array.clear
|
192
|
+
multiline_string = ""
|
193
|
+
else
|
194
|
+
dest += add_line(line,indent_level)
|
195
|
+
end
|
196
|
+
|
197
|
+
unless cursor_at_comment_line
|
198
|
+
INDENT_EXP.each do |re|
|
199
|
+
if(tline =~ re && !(tline =~ /\s+end\s*$/))
|
200
|
+
indent_level += 1
|
201
|
+
break
|
202
|
+
end
|
203
|
+
end
|
204
|
+
end
|
205
|
+
|
206
|
+
end
|
207
|
+
|
208
|
+
cursor_inside_comment_block = false if tline =~ /^=end/
|
209
|
+
end
|
210
|
+
|
211
|
+
|
212
|
+
if (indent_level != 0)
|
213
|
+
STDERR.puts "#{@path}: Indentation error: #{indent_level}" if @path
|
214
|
+
end
|
215
|
+
|
216
|
+
return dest
|
217
|
+
end
|
218
|
+
|
219
|
+
private
|
220
|
+
|
221
|
+
def make_indent(indent_level)
|
222
|
+
return (indent_level < 0) ? "" : @indent_char * @indent_size * indent_level
|
223
|
+
end
|
224
|
+
|
225
|
+
def add_line(line,indent_level)
|
226
|
+
line.strip!
|
227
|
+
line = make_indent(indent_level)+line if line.length > 0
|
228
|
+
return line + "\n"
|
229
|
+
end
|
230
|
+
|
231
|
+
end
|
232
|
+
|
233
|
+
end
|
data/rekode.gemspec
ADDED
@@ -0,0 +1,57 @@
|
|
1
|
+
# Generated by jeweler
|
2
|
+
# DO NOT EDIT THIS FILE DIRECTLY
|
3
|
+
# Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
|
4
|
+
# -*- encoding: utf-8 -*-
|
5
|
+
|
6
|
+
Gem::Specification.new do |s|
|
7
|
+
s.name = %q{rekode}
|
8
|
+
s.version = "0.1.0"
|
9
|
+
|
10
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
|
+
s.authors = ["Olle de Zwart"]
|
12
|
+
s.date = %q{2010-10-31}
|
13
|
+
s.default_executable = %q{ruby-code-indenter}
|
14
|
+
s.description = %q{rekode indents ruby code}
|
15
|
+
s.email = %q{olle@endforward.nl}
|
16
|
+
s.executables = ["ruby-code-indenter"]
|
17
|
+
s.extra_rdoc_files = [
|
18
|
+
"LICENSE",
|
19
|
+
"README.rdoc"
|
20
|
+
]
|
21
|
+
s.files = [
|
22
|
+
".document",
|
23
|
+
".gitignore",
|
24
|
+
"LICENSE",
|
25
|
+
"README.rdoc",
|
26
|
+
"Rakefile",
|
27
|
+
"VERSION",
|
28
|
+
"bin/ruby-code-indenter",
|
29
|
+
"lib/rekode.rb",
|
30
|
+
"rekode.gemspec",
|
31
|
+
"test/helper.rb",
|
32
|
+
"test/test_rekode.rb"
|
33
|
+
]
|
34
|
+
s.homepage = %q{http://github.com/osdezwart/rekode}
|
35
|
+
s.rdoc_options = ["--charset=UTF-8"]
|
36
|
+
s.require_paths = ["lib"]
|
37
|
+
s.rubygems_version = %q{1.3.7}
|
38
|
+
s.summary = %q{rekode indents ruby code}
|
39
|
+
s.test_files = [
|
40
|
+
"test/helper.rb",
|
41
|
+
"test/test_rekode.rb"
|
42
|
+
]
|
43
|
+
|
44
|
+
if s.respond_to? :specification_version then
|
45
|
+
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
46
|
+
s.specification_version = 3
|
47
|
+
|
48
|
+
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
49
|
+
s.add_development_dependency(%q<thoughtbot-shoulda>, [">= 0"])
|
50
|
+
else
|
51
|
+
s.add_dependency(%q<thoughtbot-shoulda>, [">= 0"])
|
52
|
+
end
|
53
|
+
else
|
54
|
+
s.add_dependency(%q<thoughtbot-shoulda>, [">= 0"])
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
data/test/helper.rb
ADDED
data/test/test_rekode.rb
ADDED
metadata
ADDED
@@ -0,0 +1,88 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: rekode
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease: false
|
5
|
+
segments:
|
6
|
+
- 0
|
7
|
+
- 1
|
8
|
+
- 0
|
9
|
+
version: 0.1.0
|
10
|
+
platform: ruby
|
11
|
+
authors:
|
12
|
+
- Olle de Zwart
|
13
|
+
autorequire:
|
14
|
+
bindir: bin
|
15
|
+
cert_chain: []
|
16
|
+
|
17
|
+
date: 2010-10-31 00:00:00 +02:00
|
18
|
+
default_executable: ruby-code-indenter
|
19
|
+
dependencies:
|
20
|
+
- !ruby/object:Gem::Dependency
|
21
|
+
name: thoughtbot-shoulda
|
22
|
+
prerelease: false
|
23
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
24
|
+
none: false
|
25
|
+
requirements:
|
26
|
+
- - ">="
|
27
|
+
- !ruby/object:Gem::Version
|
28
|
+
segments:
|
29
|
+
- 0
|
30
|
+
version: "0"
|
31
|
+
type: :development
|
32
|
+
version_requirements: *id001
|
33
|
+
description: rekode indents ruby code
|
34
|
+
email: olle@endforward.nl
|
35
|
+
executables:
|
36
|
+
- ruby-code-indenter
|
37
|
+
extensions: []
|
38
|
+
|
39
|
+
extra_rdoc_files:
|
40
|
+
- LICENSE
|
41
|
+
- README.rdoc
|
42
|
+
files:
|
43
|
+
- .document
|
44
|
+
- .gitignore
|
45
|
+
- LICENSE
|
46
|
+
- README.rdoc
|
47
|
+
- Rakefile
|
48
|
+
- VERSION
|
49
|
+
- bin/ruby-code-indenter
|
50
|
+
- lib/rekode.rb
|
51
|
+
- rekode.gemspec
|
52
|
+
- test/helper.rb
|
53
|
+
- test/test_rekode.rb
|
54
|
+
has_rdoc: true
|
55
|
+
homepage: http://github.com/osdezwart/rekode
|
56
|
+
licenses: []
|
57
|
+
|
58
|
+
post_install_message:
|
59
|
+
rdoc_options:
|
60
|
+
- --charset=UTF-8
|
61
|
+
require_paths:
|
62
|
+
- lib
|
63
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
64
|
+
none: false
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
segments:
|
69
|
+
- 0
|
70
|
+
version: "0"
|
71
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
72
|
+
none: false
|
73
|
+
requirements:
|
74
|
+
- - ">="
|
75
|
+
- !ruby/object:Gem::Version
|
76
|
+
segments:
|
77
|
+
- 0
|
78
|
+
version: "0"
|
79
|
+
requirements: []
|
80
|
+
|
81
|
+
rubyforge_project:
|
82
|
+
rubygems_version: 1.3.7
|
83
|
+
signing_key:
|
84
|
+
specification_version: 3
|
85
|
+
summary: rekode indents ruby code
|
86
|
+
test_files:
|
87
|
+
- test/helper.rb
|
88
|
+
- test/test_rekode.rb
|