unindentable 0.0.4 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/Gemfile.lock +9 -9
- data/README.md +4 -0
- data/lib/unindentable.rb +20 -28
- data/spec/drop_from_each_line_spec.rb +43 -0
- data/spec/find_minimum_indent_spec.rb +24 -0
- data/spec/{unindentable_spec.rb → unindent_spec.rb} +10 -12
- metadata +14 -9
data/Gemfile.lock
CHANGED
@@ -1,23 +1,23 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
unindentable (0.0
|
4
|
+
unindentable (0.1.0)
|
5
5
|
|
6
6
|
GEM
|
7
7
|
specs:
|
8
8
|
diff-lcs (1.1.2)
|
9
|
-
rspec (2.
|
10
|
-
rspec-core (~> 2.
|
11
|
-
rspec-expectations (~> 2.
|
12
|
-
rspec-mocks (~> 2.
|
13
|
-
rspec-core (2.
|
14
|
-
rspec-expectations (2.
|
9
|
+
rspec (2.4.0)
|
10
|
+
rspec-core (~> 2.4.0)
|
11
|
+
rspec-expectations (~> 2.4.0)
|
12
|
+
rspec-mocks (~> 2.4.0)
|
13
|
+
rspec-core (2.4.0)
|
14
|
+
rspec-expectations (2.4.0)
|
15
15
|
diff-lcs (~> 1.1.2)
|
16
|
-
rspec-mocks (2.
|
16
|
+
rspec-mocks (2.4.0)
|
17
17
|
|
18
18
|
PLATFORMS
|
19
19
|
ruby
|
20
20
|
|
21
21
|
DEPENDENCIES
|
22
|
-
rspec (~> 2.
|
22
|
+
rspec (~> 2.4)
|
23
23
|
unindentable!
|
data/README.md
CHANGED
@@ -66,3 +66,7 @@ Here are some examples from the Interwebs where people discuss potential solutio
|
|
66
66
|
* http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-core/19111
|
67
67
|
* http://www.ruby-forum.com/topic/167227
|
68
68
|
* http://www.ruby-forum.com/topic/95472
|
69
|
+
|
70
|
+
## Alternatives
|
71
|
+
|
72
|
+
* http://rubygems.org/gems/unindent
|
data/lib/unindentable.rb
CHANGED
@@ -1,34 +1,26 @@
|
|
1
|
-
|
1
|
+
class String
|
2
2
|
|
3
3
|
# Removes common indentation from each line of a string
|
4
|
-
def unindent
|
5
|
-
|
4
|
+
def unindent
|
5
|
+
drop_from_each_line(find_minimum_indent)
|
6
6
|
end
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
end
|
17
|
-
end
|
18
|
-
indents.min
|
7
|
+
|
8
|
+
# Drops n characters from the start of each line, but will not drop a line
|
9
|
+
# entirely; in other words, it will not drop a newline.
|
10
|
+
def drop_from_each_line(n)
|
11
|
+
self.lines.map do |line|
|
12
|
+
k = 0
|
13
|
+
line.chars.drop_while do |x|
|
14
|
+
k += 1
|
15
|
+
k <= n && x != "\n"
|
16
|
+
end.join("")
|
17
|
+
end.join("")
|
19
18
|
end
|
20
|
-
|
21
|
-
#
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
new_string << if line == "\n"
|
26
|
-
"\n"
|
27
|
-
else
|
28
|
-
line.slice(n, line.length)
|
29
|
-
end
|
30
|
-
end
|
31
|
-
new_string
|
19
|
+
|
20
|
+
# Returns the indent level (number of spaces at the start of a string) with
|
21
|
+
# multiple lines
|
22
|
+
def find_minimum_indent
|
23
|
+
self.lines.map { |s| s.index(/[^\s]/) unless s.empty? }.compact.min
|
32
24
|
end
|
33
|
-
|
25
|
+
|
34
26
|
end
|
@@ -0,0 +1,43 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
2
|
+
|
3
|
+
S = <<-BLOCK
|
4
|
+
X
|
5
|
+
y
|
6
|
+
ZZ
|
7
|
+
BLOCK
|
8
|
+
|
9
|
+
describe "drop_from_each_line" do
|
10
|
+
describe "example with 2 space indent, 2 indent levels" do
|
11
|
+
it "-" do
|
12
|
+
S.should == " X\n y\n ZZ\n"
|
13
|
+
end
|
14
|
+
|
15
|
+
it "0" do
|
16
|
+
S.drop_from_each_line(0).should == " X\n y\n ZZ\n"
|
17
|
+
end
|
18
|
+
|
19
|
+
it "1" do
|
20
|
+
S.drop_from_each_line(1).should == " X\n y\n ZZ\n"
|
21
|
+
end
|
22
|
+
|
23
|
+
it "2" do
|
24
|
+
S.drop_from_each_line(2).should == "X\n y\nZZ\n"
|
25
|
+
end
|
26
|
+
|
27
|
+
it "3" do
|
28
|
+
S.drop_from_each_line(3).should == "\n y\nZ\n"
|
29
|
+
end
|
30
|
+
|
31
|
+
it "4" do
|
32
|
+
S.drop_from_each_line(4).should == "\ny\n\n"
|
33
|
+
end
|
34
|
+
|
35
|
+
it "5" do
|
36
|
+
S.drop_from_each_line(5).should == "\n\n\n"
|
37
|
+
end
|
38
|
+
|
39
|
+
it "6" do
|
40
|
+
S.drop_from_each_line(6).should == "\n\n\n"
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
2
|
+
|
3
|
+
describe "find_minimum_indent" do
|
4
|
+
|
5
|
+
it "should handle an example with 3 indent levels" do
|
6
|
+
s = <<-BLOCK
|
7
|
+
A
|
8
|
+
B
|
9
|
+
C
|
10
|
+
D
|
11
|
+
BLOCK
|
12
|
+
s.find_minimum_indent.should == 6
|
13
|
+
end
|
14
|
+
|
15
|
+
it "should not let blank lines break the indent" do
|
16
|
+
s = <<-BLOCK
|
17
|
+
The first line
|
18
|
+
|
19
|
+
The third line
|
20
|
+
BLOCK
|
21
|
+
s.find_minimum_indent.should == 6
|
22
|
+
end
|
23
|
+
|
24
|
+
end
|
@@ -1,30 +1,28 @@
|
|
1
1
|
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
2
2
|
|
3
|
-
describe "
|
4
|
-
include Unindentable
|
5
|
-
|
3
|
+
describe "unindent" do
|
6
4
|
it "should handle the README example" do
|
7
|
-
|
5
|
+
x = <<-BLOCK.unindent
|
8
6
|
<html>
|
9
7
|
<body>
|
10
8
|
<p>Hello</p>
|
11
9
|
</body>
|
12
10
|
</html>
|
13
11
|
BLOCK
|
14
|
-
|
12
|
+
x.should == "<html>\n <body>\n <p>Hello</p>\n </body>\n</html>\n"
|
15
13
|
end
|
16
14
|
|
17
15
|
it "should handle ugly heredoc against left margin" do
|
18
|
-
x =
|
16
|
+
x = <<-BLOCK.unindent
|
19
17
|
a
|
20
18
|
b
|
21
19
|
c
|
22
20
|
BLOCK
|
23
|
-
x.should == "a\nb\nc\n"
|
21
|
+
x.unindent.should == "a\nb\nc\n"
|
24
22
|
end
|
25
23
|
|
26
24
|
it "should handle a basic example" do
|
27
|
-
x =
|
25
|
+
x = <<-BLOCK.unindent
|
28
26
|
a
|
29
27
|
b
|
30
28
|
c
|
@@ -33,7 +31,7 @@ c
|
|
33
31
|
end
|
34
32
|
|
35
33
|
it "should handle an example with 2 indent levels" do
|
36
|
-
x =
|
34
|
+
x = <<-BLOCK.unindent
|
37
35
|
X 1 2
|
38
36
|
yada yada
|
39
37
|
Z Z Z
|
@@ -42,7 +40,7 @@ c
|
|
42
40
|
end
|
43
41
|
|
44
42
|
it "should handle an example with 3 indent levels" do
|
45
|
-
x =
|
43
|
+
x = <<-BLOCK.unindent
|
46
44
|
A
|
47
45
|
B
|
48
46
|
C
|
@@ -52,7 +50,7 @@ c
|
|
52
50
|
end
|
53
51
|
|
54
52
|
it "should preserve varying indent levels a blank line" do
|
55
|
-
x =
|
53
|
+
x = <<-BLOCK.unindent
|
56
54
|
The first line
|
57
55
|
The second line
|
58
56
|
|
@@ -62,7 +60,7 @@ c
|
|
62
60
|
end
|
63
61
|
|
64
62
|
it "should not let blank lines break the indent" do
|
65
|
-
x =
|
63
|
+
x = <<-BLOCK.unindent
|
66
64
|
The first line
|
67
65
|
|
68
66
|
The third line
|
metadata
CHANGED
@@ -4,9 +4,9 @@ version: !ruby/object:Gem::Version
|
|
4
4
|
prerelease: false
|
5
5
|
segments:
|
6
6
|
- 0
|
7
|
+
- 1
|
7
8
|
- 0
|
8
|
-
|
9
|
-
version: 0.0.4
|
9
|
+
version: 0.1.0
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- David James
|
@@ -27,8 +27,8 @@ dependencies:
|
|
27
27
|
- !ruby/object:Gem::Version
|
28
28
|
segments:
|
29
29
|
- 2
|
30
|
-
-
|
31
|
-
version: "2.
|
30
|
+
- 4
|
31
|
+
version: "2.4"
|
32
32
|
type: :development
|
33
33
|
version_requirements: *id001
|
34
34
|
description: Unindent strings, especially heredocs.
|
@@ -47,8 +47,10 @@ files:
|
|
47
47
|
- LICENSE
|
48
48
|
- README.md
|
49
49
|
- lib/unindentable.rb
|
50
|
-
- spec/
|
50
|
+
- spec/drop_from_each_line_spec.rb
|
51
|
+
- spec/find_minimum_indent_spec.rb
|
51
52
|
- spec/spec_helper.rb
|
53
|
+
- spec/unindent_spec.rb
|
52
54
|
has_rdoc: true
|
53
55
|
homepage: http://github.com/sunlightlabs/ruby-unindentable
|
54
56
|
licenses: []
|
@@ -72,15 +74,18 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
72
74
|
- - ">="
|
73
75
|
- !ruby/object:Gem::Version
|
74
76
|
segments:
|
75
|
-
-
|
76
|
-
|
77
|
+
- 1
|
78
|
+
- 3
|
79
|
+
version: "1.3"
|
77
80
|
requirements: []
|
78
81
|
|
79
82
|
rubyforge_project:
|
80
83
|
rubygems_version: 1.3.7
|
81
84
|
signing_key:
|
82
85
|
specification_version: 3
|
83
|
-
summary:
|
86
|
+
summary: Write heredocs without the unwanted extra indentation.
|
84
87
|
test_files:
|
85
|
-
- spec/
|
88
|
+
- spec/drop_from_each_line_spec.rb
|
89
|
+
- spec/find_minimum_indent_spec.rb
|
86
90
|
- spec/spec_helper.rb
|
91
|
+
- spec/unindent_spec.rb
|