outdent 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- data/Gemfile +1 -0
- data/Gemfile.lock +23 -0
- data/LICENSE +10 -0
- data/README.md +77 -0
- data/lib/outdent.rb +26 -0
- data/spec/drop_from_each_line_spec.rb +43 -0
- data/spec/find_minimum_indent_spec.rb +24 -0
- data/spec/outdent_spec.rb +71 -0
- data/spec/spec_helper.rb +8 -0
- metadata +91 -0
data/Gemfile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
gemspec
|
data/Gemfile.lock
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
outdent (0.2.0)
|
5
|
+
|
6
|
+
GEM
|
7
|
+
specs:
|
8
|
+
diff-lcs (1.1.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
|
+
diff-lcs (~> 1.1.2)
|
16
|
+
rspec-mocks (2.4.0)
|
17
|
+
|
18
|
+
PLATFORMS
|
19
|
+
ruby
|
20
|
+
|
21
|
+
DEPENDENCIES
|
22
|
+
outdent!
|
23
|
+
rspec (~> 2.4)
|
data/LICENSE
ADDED
@@ -0,0 +1,10 @@
|
|
1
|
+
Copyright (c) 2009, Sunlight Foundation
|
2
|
+
All rights reserved.
|
3
|
+
|
4
|
+
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
|
5
|
+
|
6
|
+
* Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
|
7
|
+
* Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
|
8
|
+
* Neither the name of Sunlight Foundation nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
|
9
|
+
|
10
|
+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
data/README.md
ADDED
@@ -0,0 +1,77 @@
|
|
1
|
+
# Outdent
|
2
|
+
|
3
|
+
With Outdent, you can write heredocs without worry of wonky indentation.
|
4
|
+
|
5
|
+
## Motivation
|
6
|
+
|
7
|
+
Heredocs are convenient. But the indentation can bite you. For example:
|
8
|
+
|
9
|
+
def hello
|
10
|
+
html = <<-BLOCK
|
11
|
+
<html>
|
12
|
+
<body>
|
13
|
+
<p>Hello</p>
|
14
|
+
</body>
|
15
|
+
</html>
|
16
|
+
BLOCK
|
17
|
+
end
|
18
|
+
|
19
|
+
The problem is that you get extra indentation you probably don't want:
|
20
|
+
|
21
|
+
irb> hello
|
22
|
+
=> " <html>\n <body>\n <p>Hello</p>\n </body>\n </html>\n"
|
23
|
+
|
24
|
+
One solution is to move your heredoc to the left margin:
|
25
|
+
|
26
|
+
def hello
|
27
|
+
html = <<-BLOCK
|
28
|
+
<html>
|
29
|
+
<body>
|
30
|
+
<p>Hello</p>
|
31
|
+
</body>
|
32
|
+
</html>
|
33
|
+
BLOCK
|
34
|
+
end
|
35
|
+
|
36
|
+
So that you get the result you expect:
|
37
|
+
|
38
|
+
> hello
|
39
|
+
=> "<html>\n <body>\n <p>Hello</p>\n </body>\n</html>\n"
|
40
|
+
|
41
|
+
But this doesn't look good in your source. With Outdent, you can write:
|
42
|
+
|
43
|
+
def hello
|
44
|
+
html = <<-BLOCK.outdent
|
45
|
+
<html>
|
46
|
+
<body>
|
47
|
+
<p>Hello</p>
|
48
|
+
</body>
|
49
|
+
</html>
|
50
|
+
BLOCK
|
51
|
+
end
|
52
|
+
|
53
|
+
And get the nice outdented result you want:
|
54
|
+
|
55
|
+
> hello
|
56
|
+
=> "<html>\n <body>\n <p>Hello</p>\n </body>\n</html>\n"
|
57
|
+
|
58
|
+
That's it.
|
59
|
+
|
60
|
+
## The Backstory
|
61
|
+
|
62
|
+
Here are some examples from the Interwebs where people discuss potential solutions:
|
63
|
+
|
64
|
+
* http://oldrcrs.rubypal.com/rcr/show/188
|
65
|
+
* http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-core/19111
|
66
|
+
* http://www.ruby-forum.com/topic/167227
|
67
|
+
* http://www.ruby-forum.com/topic/95472
|
68
|
+
|
69
|
+
## Alternatives
|
70
|
+
|
71
|
+
* http://rubygems.org/gems/unindent
|
72
|
+
|
73
|
+
## History
|
74
|
+
|
75
|
+
* 2011-01-05 - Renamed project from Unindentable to Outdent
|
76
|
+
* 2011-01-04 - Borrowed (stole?) API from [Unindent](http://rubygems.org/gems/unindent) project
|
77
|
+
* 2009-07-09 - First version
|
data/lib/outdent.rb
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
class String
|
2
|
+
|
3
|
+
# Unindent. Removes common indentation from each line of a string.
|
4
|
+
def outdent
|
5
|
+
drop_from_each_line(find_minimum_indent)
|
6
|
+
end
|
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("")
|
18
|
+
end
|
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
|
24
|
+
end
|
25
|
+
|
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
|
@@ -0,0 +1,71 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
2
|
+
|
3
|
+
describe "outdent" do
|
4
|
+
it "should handle the README example" do
|
5
|
+
x = <<-BLOCK.outdent
|
6
|
+
<html>
|
7
|
+
<body>
|
8
|
+
<p>Hello</p>
|
9
|
+
</body>
|
10
|
+
</html>
|
11
|
+
BLOCK
|
12
|
+
x.should == "<html>\n <body>\n <p>Hello</p>\n </body>\n</html>\n"
|
13
|
+
end
|
14
|
+
|
15
|
+
it "should handle ugly heredoc against left margin" do
|
16
|
+
x = <<-BLOCK.outdent
|
17
|
+
a
|
18
|
+
b
|
19
|
+
c
|
20
|
+
BLOCK
|
21
|
+
x.outdent.should == "a\nb\nc\n"
|
22
|
+
end
|
23
|
+
|
24
|
+
it "should handle a basic example" do
|
25
|
+
x = <<-BLOCK.outdent
|
26
|
+
a
|
27
|
+
b
|
28
|
+
c
|
29
|
+
BLOCK
|
30
|
+
x.should == "a\nb\nc\n"
|
31
|
+
end
|
32
|
+
|
33
|
+
it "should handle an example with 2 indent levels" do
|
34
|
+
x = <<-BLOCK.outdent
|
35
|
+
X 1 2
|
36
|
+
yada yada
|
37
|
+
Z Z Z
|
38
|
+
BLOCK
|
39
|
+
x.should == "X 1 2\n yada yada\nZ Z Z\n"
|
40
|
+
end
|
41
|
+
|
42
|
+
it "should handle an example with 3 indent levels" do
|
43
|
+
x = <<-BLOCK.outdent
|
44
|
+
A
|
45
|
+
B
|
46
|
+
C
|
47
|
+
D
|
48
|
+
BLOCK
|
49
|
+
x.should == " A\n B\nC\n D\n"
|
50
|
+
end
|
51
|
+
|
52
|
+
it "should preserve varying indent levels a blank line" do
|
53
|
+
x = <<-BLOCK.outdent
|
54
|
+
The first line
|
55
|
+
The second line
|
56
|
+
|
57
|
+
The fourth line
|
58
|
+
BLOCK
|
59
|
+
x.should == "The first line\n The second line\n \nThe fourth line\n"
|
60
|
+
end
|
61
|
+
|
62
|
+
it "should not let blank lines break the indent" do
|
63
|
+
x = <<-BLOCK.outdent
|
64
|
+
The first line
|
65
|
+
|
66
|
+
The third line
|
67
|
+
BLOCK
|
68
|
+
x.should == "The first line\n\nThe third line\n"
|
69
|
+
end
|
70
|
+
|
71
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,91 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: outdent
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease: false
|
5
|
+
segments:
|
6
|
+
- 0
|
7
|
+
- 2
|
8
|
+
- 0
|
9
|
+
version: 0.2.0
|
10
|
+
platform: ruby
|
11
|
+
authors:
|
12
|
+
- David James
|
13
|
+
autorequire:
|
14
|
+
bindir: bin
|
15
|
+
cert_chain: []
|
16
|
+
|
17
|
+
date: 2011-05-11 00:00:00 -04:00
|
18
|
+
default_executable:
|
19
|
+
dependencies:
|
20
|
+
- !ruby/object:Gem::Dependency
|
21
|
+
name: rspec
|
22
|
+
prerelease: false
|
23
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
24
|
+
none: false
|
25
|
+
requirements:
|
26
|
+
- - ~>
|
27
|
+
- !ruby/object:Gem::Version
|
28
|
+
segments:
|
29
|
+
- 2
|
30
|
+
- 4
|
31
|
+
version: "2.4"
|
32
|
+
type: :development
|
33
|
+
version_requirements: *id001
|
34
|
+
description: Unindent strings, especially heredocs.
|
35
|
+
email:
|
36
|
+
- djames at sunlightfoundation.com
|
37
|
+
executables: []
|
38
|
+
|
39
|
+
extensions: []
|
40
|
+
|
41
|
+
extra_rdoc_files:
|
42
|
+
- LICENSE
|
43
|
+
- README.md
|
44
|
+
files:
|
45
|
+
- Gemfile
|
46
|
+
- Gemfile.lock
|
47
|
+
- LICENSE
|
48
|
+
- README.md
|
49
|
+
- lib/outdent.rb
|
50
|
+
- spec/drop_from_each_line_spec.rb
|
51
|
+
- spec/find_minimum_indent_spec.rb
|
52
|
+
- spec/spec_helper.rb
|
53
|
+
- spec/outdent_spec.rb
|
54
|
+
has_rdoc: true
|
55
|
+
homepage: http://github.com/sunlightlabs/ruby-outdent
|
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
|
+
- 1
|
78
|
+
- 3
|
79
|
+
version: "1.3"
|
80
|
+
requirements: []
|
81
|
+
|
82
|
+
rubyforge_project:
|
83
|
+
rubygems_version: 1.3.7
|
84
|
+
signing_key:
|
85
|
+
specification_version: 3
|
86
|
+
summary: Write heredocs without the unwanted extra indentation.
|
87
|
+
test_files:
|
88
|
+
- spec/drop_from_each_line_spec.rb
|
89
|
+
- spec/find_minimum_indent_spec.rb
|
90
|
+
- spec/spec_helper.rb
|
91
|
+
- spec/outdent_spec.rb
|