unindentable 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/LICENSE +10 -0
- data/README.md +68 -0
- data/lib/unindentable.rb +24 -0
- data/spec/unindentable_spec.rb +66 -0
- metadata +60 -0
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,68 @@
|
|
1
|
+
# Unindentable
|
2
|
+
|
3
|
+
With Unindentable, 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 Unindentable, you can write:
|
42
|
+
|
43
|
+
include Unindentable
|
44
|
+
def hello
|
45
|
+
html = unindent <<-BLOCK
|
46
|
+
<html>
|
47
|
+
<body>
|
48
|
+
<p>Hello</p>
|
49
|
+
</body>
|
50
|
+
</html>
|
51
|
+
BLOCK
|
52
|
+
end
|
53
|
+
|
54
|
+
And get the nice unindented result you want:
|
55
|
+
|
56
|
+
> hello
|
57
|
+
=> "<html>\n <body>\n <p>Hello</p>\n </body>\n</html>\n"
|
58
|
+
|
59
|
+
That's it.
|
60
|
+
|
61
|
+
## The Backstory
|
62
|
+
|
63
|
+
Here are some examples from the Interwebs where people discuss potential solutions:
|
64
|
+
|
65
|
+
* http://oldrcrs.rubypal.com/rcr/show/188
|
66
|
+
* http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-core/19111
|
67
|
+
* http://www.ruby-forum.com/topic/167227
|
68
|
+
* http://www.ruby-forum.com/topic/95472
|
data/lib/unindentable.rb
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
module Unindentable
|
2
|
+
|
3
|
+
# Removes common indentation from each line of a string
|
4
|
+
def unindent(string)
|
5
|
+
left_trim(string, find_minimum_indent(string))
|
6
|
+
end
|
7
|
+
|
8
|
+
protected
|
9
|
+
|
10
|
+
# Returns the indent level of a string (with multiple lines)
|
11
|
+
def find_minimum_indent(string)
|
12
|
+
indents = []
|
13
|
+
string.each_line { |l| indents << l.index(/[^ ]/) }
|
14
|
+
indents.min
|
15
|
+
end
|
16
|
+
|
17
|
+
# Trims the leftmost n characters from each line of a string
|
18
|
+
def left_trim(string, n)
|
19
|
+
new_string = ""
|
20
|
+
string.each_line { |l| new_string << l.slice(n, l.length) }
|
21
|
+
new_string
|
22
|
+
end
|
23
|
+
|
24
|
+
end
|
@@ -0,0 +1,66 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'spec'
|
3
|
+
require File.expand_path(File.dirname(__FILE__) + '/../lib/unindentable')
|
4
|
+
|
5
|
+
describe "Unindentable" do
|
6
|
+
include Unindentable
|
7
|
+
|
8
|
+
it "should handle the README example" do
|
9
|
+
html = unindent <<-BLOCK
|
10
|
+
<html>
|
11
|
+
<body>
|
12
|
+
<p>Hello</p>
|
13
|
+
</body>
|
14
|
+
</html>
|
15
|
+
BLOCK
|
16
|
+
html.should == "<html>\n <body>\n <p>Hello</p>\n </body>\n</html>\n"
|
17
|
+
end
|
18
|
+
|
19
|
+
it "should handle ugly heredoc against left margin" do
|
20
|
+
x = unindent <<-BLOCK
|
21
|
+
a
|
22
|
+
b
|
23
|
+
c
|
24
|
+
BLOCK
|
25
|
+
x.should == "a\nb\nc\n"
|
26
|
+
end
|
27
|
+
|
28
|
+
it "should handle a basic example" do
|
29
|
+
x = unindent <<-BLOCK
|
30
|
+
a
|
31
|
+
b
|
32
|
+
c
|
33
|
+
BLOCK
|
34
|
+
x.should == "a\nb\nc\n"
|
35
|
+
end
|
36
|
+
|
37
|
+
it "should handle an example with 2 indent levels" do
|
38
|
+
x = unindent <<-BLOCK
|
39
|
+
X 1 2
|
40
|
+
yada yada
|
41
|
+
Z Z Z
|
42
|
+
BLOCK
|
43
|
+
x.should == "X 1 2\n yada yada\nZ Z Z\n"
|
44
|
+
end
|
45
|
+
|
46
|
+
it "should handle an example with 3 indent levels" do
|
47
|
+
x = unindent <<-BLOCK
|
48
|
+
A
|
49
|
+
B
|
50
|
+
C
|
51
|
+
D
|
52
|
+
BLOCK
|
53
|
+
x.should == " A\n B\nC\n D\n"
|
54
|
+
end
|
55
|
+
|
56
|
+
it "should preserve varying indent levels a blank line" do
|
57
|
+
x = unindent <<-BLOCK
|
58
|
+
The first line
|
59
|
+
The second line
|
60
|
+
|
61
|
+
The fourth line
|
62
|
+
BLOCK
|
63
|
+
x.should == "The first line\n The second line\n \nThe fourth line\n"
|
64
|
+
end
|
65
|
+
|
66
|
+
end
|
metadata
ADDED
@@ -0,0 +1,60 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: unindentable
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- David James
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-07-09 00:00:00 -04:00
|
13
|
+
default_executable:
|
14
|
+
dependencies: []
|
15
|
+
|
16
|
+
description: Unindent strings, especially heredocs.
|
17
|
+
email:
|
18
|
+
- djames at sunlightfoundation.com
|
19
|
+
executables: []
|
20
|
+
|
21
|
+
extensions: []
|
22
|
+
|
23
|
+
extra_rdoc_files:
|
24
|
+
- LICENSE
|
25
|
+
- README.md
|
26
|
+
files:
|
27
|
+
- LICENSE
|
28
|
+
- README.md
|
29
|
+
- lib/unindentable.rb
|
30
|
+
- spec/unindentable_spec.rb
|
31
|
+
has_rdoc: true
|
32
|
+
homepage: http://github.com/sunlightlabs/ruby-unindentable/tree
|
33
|
+
licenses: []
|
34
|
+
|
35
|
+
post_install_message:
|
36
|
+
rdoc_options:
|
37
|
+
- --charset=UTF-8
|
38
|
+
require_paths:
|
39
|
+
- lib
|
40
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
41
|
+
requirements:
|
42
|
+
- - ">="
|
43
|
+
- !ruby/object:Gem::Version
|
44
|
+
version: "0"
|
45
|
+
version:
|
46
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
47
|
+
requirements:
|
48
|
+
- - ">="
|
49
|
+
- !ruby/object:Gem::Version
|
50
|
+
version: "0"
|
51
|
+
version:
|
52
|
+
requirements: []
|
53
|
+
|
54
|
+
rubyforge_project:
|
55
|
+
rubygems_version: 1.3.5
|
56
|
+
signing_key:
|
57
|
+
specification_version: 3
|
58
|
+
summary: With Unindentable, you can write heredocs without worry of wonky indentation.
|
59
|
+
test_files:
|
60
|
+
- spec/unindentable_spec.rb
|