heredoc_unindent 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/History.txt ADDED
@@ -0,0 +1,6 @@
1
+ === 1.0.0 / 2011-01-16
2
+
3
+ * 1 major enhancement
4
+
5
+ * Birthday!
6
+
data/Manifest.txt ADDED
@@ -0,0 +1,6 @@
1
+ History.txt
2
+ Manifest.txt
3
+ README.rdoc
4
+ Rakefile
5
+ lib/heredoc_unindent.rb
6
+ test/test_heredoc_unindent.rb
data/README.rdoc ADDED
@@ -0,0 +1,90 @@
1
+ = heredoc_unindent
2
+
3
+ * https://github.com/adrianomitre/heredoc_unindent
4
+
5
+ == DESCRIPTION:
6
+
7
+ Removes common margin from indented strings such as the ones produced by
8
+ heredocs, i.e., strips out leading whitespace chars at the beggining of
9
+ each line (but only as much as the line with the smallest margin).
10
+
11
+ == SYNOPSIS:
12
+
13
+ if true
14
+ puts <<-EOS.unindent
15
+ How wonderful it is
16
+ to be able
17
+ to unindent heredocs
18
+ EOS
19
+ end
20
+
21
+ produces
22
+
23
+ How wonderful it is
24
+ to be able
25
+ to unindent heredocs
26
+
27
+ instead of
28
+
29
+ How wonderful it is
30
+ to be able
31
+ to unindent heredocs
32
+
33
+
34
+ == REQUIREMENTS:
35
+
36
+ * None: this gem does not depend on any other gem.
37
+
38
+ == INSTALL:
39
+
40
+ * sudo gem install heredoc_unindent
41
+
42
+ == DEVELOPERS:
43
+
44
+ After checking out the source, run:
45
+
46
+ $ rake newb
47
+
48
+ This task will install any missing dependencies, run the tests/specs,
49
+ and generate the RDoc.
50
+
51
+ == ACKNOWLEDGEMENTS
52
+
53
+ Based on the answer by Rene Saarsoo to the question
54
+ {how do i remove leading whitespace chars from ruby heredoc}[http://stackoverflow.com/questions/3772864/how-do-i-remove-leading-whitespace-chars-from-ruby-heredoc].
55
+
56
+ == ALTERNATIVES
57
+
58
+ Despite minor API differences, extra-features availability or edge-case behaviour, the
59
+ following gems implement the same unindenting functionality and are essentially equivalent to heredoc_unindent:
60
+ * {outdent}[http://rubygems.org/gems/outdent] (formerly {unindentable}[http://rubygems.org/gems/unindentable])
61
+ * {unindent}[http://rubygems.org/gems/unindent]
62
+
63
+ And the following gem, which has a much broader scope, also features a method
64
+ with equivalent functionality:
65
+ * {indentation}[http://rubygems.org/gems/indentation] -- String#reset_indentation method
66
+
67
+ == LICENSE:
68
+
69
+ (The MIT License)
70
+
71
+ Copyright (c) 2011 Adriano Mitre
72
+
73
+ Permission is hereby granted, free of charge, to any person obtaining
74
+ a copy of this software and associated documentation files (the
75
+ 'Software'), to deal in the Software without restriction, including
76
+ without limitation the rights to use, copy, modify, merge, publish,
77
+ distribute, sublicense, and/or sell copies of the Software, and to
78
+ permit persons to whom the Software is furnished to do so, subject to
79
+ the following conditions:
80
+
81
+ The above copyright notice and this permission notice shall be
82
+ included in all copies or substantial portions of the Software.
83
+
84
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
85
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
86
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
87
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
88
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
89
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
90
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/Rakefile ADDED
@@ -0,0 +1,34 @@
1
+ # -*- ruby -*-
2
+
3
+ require 'rubygems'
4
+ require 'hoe'
5
+
6
+ # Hoe.plugin :compiler
7
+ # Hoe.plugin :cucumberfeatures
8
+ # Hoe.plugin :gem_prelude_sucks
9
+ # Hoe.plugin :inline
10
+ # Hoe.plugin :manifest
11
+ # Hoe.plugin :newgem
12
+ # Hoe.plugin :racc
13
+ # Hoe.plugin :rubyforge
14
+ # Hoe.plugin :website
15
+
16
+ Hoe.spec 'heredoc_unindent' do
17
+ # HEY! If you fill these out in ~/.hoe_template/Rakefile.erb then
18
+ # you'll never have to touch them again!
19
+ # (delete this comment too, of course)
20
+
21
+ developer('Adriano Mitre', 'adriano.mitre@gmail.com')
22
+
23
+ self.readme_file = 'README.rdoc'
24
+ self.extra_rdoc_files << 'README.rdoc'
25
+
26
+ # self.rubyforge_name = 'heredoc_unindentx' # if different than 'heredoc_unindent'
27
+ end
28
+
29
+ # vim: syntax=ruby
30
+
31
+ task :tests => [:test] do
32
+ # aliasing :test with :tests for RVM ('rvm tests')
33
+ end
34
+
@@ -0,0 +1,42 @@
1
+ # This module, which is included in String, define the heredoc unindentation method
2
+ # +heredoc_unindent+ and alias it as +unindent+.
3
+ #
4
+ module HeredocUnindent
5
+
6
+ VERSION = '1.0.0'
7
+
8
+ # Removes common margin from indented strings such as the ones produced by
9
+ # heredocs, i.e., strips out leading whitespace chars at the beggining of
10
+ # each line (but only as much as the line with the smallest margin).
11
+ #
12
+ # Unless the optional argument +warn_first_dif_min+ is set to false or nil, a
13
+ # warning is produced when the margin of the first line differs from the minimum.
14
+ #
15
+ def heredoc_unindent(warn_first_dif_min=true)
16
+ min_margin = self.scan(/^\s*/).map(&:size).min
17
+ if warn_first_dif_min
18
+ first_margin = self[/^\s*/].size
19
+ if first_margin != min_margin
20
+ puts "warning: margin of the first line differs from minimum margin"
21
+ end
22
+ end
23
+ re = Regexp.new('^\s{0,' + min_margin.to_s + '}' ) # omitting the lower limit produces warnings and wrong behavior in ruby-1.8.7-p330 and ree-1.8.7-2010.02
24
+ self.gsub(re, '')
25
+ end
26
+ alias unindent heredoc_unindent
27
+
28
+ # Performs HeredocUnindent#heredoc_unindent in place, returning self, or nil if no changes were made
29
+ #
30
+ def heredoc_unindent!(warn_first_dif_min=true)
31
+ orig = self.dup
32
+ self.replace(self.heredoc_unindent(warn_first_dif_min))
33
+ self != orig ? self : nil
34
+ end
35
+ alias unindent! heredoc_unindent!
36
+
37
+ end
38
+
39
+ class String # :nodoc:
40
+ include HeredocUnindent
41
+ end
42
+
@@ -0,0 +1,86 @@
1
+ require "rubygems"
2
+ require "test/unit"
3
+ require File.expand_path("../../lib/heredoc_unindent", __FILE__)
4
+
5
+ class TestHeredocUnindent < Test::Unit::TestCase
6
+
7
+ def prep_sing1
8
+ ugly = <<-EOS
9
+ that
10
+ is
11
+ weird
12
+ EOS
13
+
14
+ pretty = <<EOS
15
+ that
16
+ is
17
+ weird
18
+ EOS
19
+ return ugly, pretty
20
+ end
21
+
22
+ def prep_sing2
23
+ ugly = <<-EOS
24
+ for k in 1 .. 10
25
+ puts k
26
+ end
27
+ EOS
28
+
29
+ pretty = <<EOS
30
+ for k in 1 .. 10
31
+ puts k
32
+ end
33
+ EOS
34
+ return ugly, pretty
35
+ end
36
+
37
+ def prep_sing3
38
+ ugly = <<-EOS
39
+ The first line
40
+
41
+ The third line
42
+ EOS
43
+
44
+ pretty = <<EOS
45
+ The first line
46
+
47
+ The third line
48
+ EOS
49
+ return ugly, pretty
50
+ end
51
+
52
+ def prep_mult
53
+ ugly = <<-BEGIN + " <--- middle --->\n" + <<-END
54
+ This is the beginning:
55
+ BEGIN
56
+ And now it is over!
57
+ END
58
+ pretty = <<EOS
59
+ This is the beginning:
60
+ <--- middle --->
61
+ And now it is over!
62
+ EOS
63
+ return ugly, pretty
64
+ end
65
+
66
+ def test_unindent
67
+ perform_tests(*prep_sing1)
68
+ perform_tests(*prep_sing2)
69
+ perform_tests(*prep_mult)
70
+ end
71
+
72
+ def perform_tests(ugly, pretty)
73
+ ugly_bak = ugly.dup
74
+ assert_equal pretty, ugly.unindent(false)
75
+ assert_equal pretty, ugly.heredoc_unindent(false)
76
+ assert_equal ugly_bak, ugly, '#unindent should have no side-effects'
77
+ assert_equal pretty, ugly.heredoc_unindent!(false)
78
+ assert_equal pretty, ugly
79
+ assert_equal nil, ugly.heredoc_unindent!(false)
80
+ ugly = ugly_bak.dup
81
+ assert_equal pretty, ugly.unindent!(false)
82
+ assert_equal pretty, ugly
83
+ assert_equal nil, ugly.unindent!(false)
84
+ end
85
+
86
+ end
metadata ADDED
@@ -0,0 +1,94 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: heredoc_unindent
3
+ version: !ruby/object:Gem::Version
4
+ hash: 23
5
+ prerelease: false
6
+ segments:
7
+ - 1
8
+ - 0
9
+ - 0
10
+ version: 1.0.0
11
+ platform: ruby
12
+ authors:
13
+ - Adriano Mitre
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-01-17 00:00:00 -02:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: hoe
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ hash: 47
30
+ segments:
31
+ - 2
32
+ - 8
33
+ - 0
34
+ version: 2.8.0
35
+ type: :development
36
+ version_requirements: *id001
37
+ description: |-
38
+ Removes common margin from indented strings such as the ones produced by
39
+ heredocs, i.e., strips out leading whitespace chars at the beggining of
40
+ each line (but only as much as the line with the smallest margin).
41
+ email:
42
+ - adriano.mitre@gmail.com
43
+ executables: []
44
+
45
+ extensions: []
46
+
47
+ extra_rdoc_files:
48
+ - History.txt
49
+ - Manifest.txt
50
+ - README.rdoc
51
+ files:
52
+ - History.txt
53
+ - Manifest.txt
54
+ - README.rdoc
55
+ - Rakefile
56
+ - lib/heredoc_unindent.rb
57
+ - test/test_heredoc_unindent.rb
58
+ has_rdoc: true
59
+ homepage: https://github.com/adrianomitre/heredoc_unindent
60
+ licenses: []
61
+
62
+ post_install_message:
63
+ rdoc_options:
64
+ - --main
65
+ - README.rdoc
66
+ require_paths:
67
+ - lib
68
+ required_ruby_version: !ruby/object:Gem::Requirement
69
+ none: false
70
+ requirements:
71
+ - - ">="
72
+ - !ruby/object:Gem::Version
73
+ hash: 3
74
+ segments:
75
+ - 0
76
+ version: "0"
77
+ required_rubygems_version: !ruby/object:Gem::Requirement
78
+ none: false
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ hash: 3
83
+ segments:
84
+ - 0
85
+ version: "0"
86
+ requirements: []
87
+
88
+ rubyforge_project: heredoc_unindent
89
+ rubygems_version: 1.3.7
90
+ signing_key:
91
+ specification_version: 3
92
+ summary: Removes common margin from indented strings such as the ones produced by heredocs, i.e., strips out leading whitespace chars at the beggining of each line (but only as much as the line with the smallest margin).
93
+ test_files:
94
+ - test/test_heredoc_unindent.rb