heredoc_unindent 1.0.4 → 1.0.5
Sign up to get free protection for your applications and to get access to all the features.
- data.tar.gz.sig +0 -0
- data/History.rdoc +8 -0
- data/README.rdoc +8 -5
- data/Rakefile +1 -1
- data/lib/heredoc_unindent.rb +77 -25
- metadata +2 -2
- metadata.gz.sig +2 -2
data.tar.gz.sig
CHANGED
Binary file
|
data/History.rdoc
CHANGED
data/README.rdoc
CHANGED
@@ -17,22 +17,25 @@ will probably be an issue and hence this gem.
|
|
17
17
|
== SYNOPSIS:
|
18
18
|
|
19
19
|
if true
|
20
|
-
|
20
|
+
s = <<-EOS
|
21
21
|
How sad it is
|
22
22
|
to be unable
|
23
23
|
to unindent heredocs
|
24
24
|
EOS
|
25
|
-
|
25
|
+
t = <<-EOS.unindent
|
26
26
|
How wonderful it is
|
27
27
|
to be able
|
28
28
|
to do it!
|
29
29
|
EOS
|
30
30
|
end
|
31
31
|
|
32
|
-
|
33
|
-
|
32
|
+
s[0..12] #=> " How sad"
|
33
|
+
t[0..12] #=> "How wonderful"
|
34
34
|
|
35
|
-
|
35
|
+
s = " foo" #=> " foo"
|
36
|
+
s.unindent! #=> "foo"
|
37
|
+
s.unindent! #=> nil
|
38
|
+
s #=> "foo"
|
36
39
|
|
37
40
|
== FEATURES:
|
38
41
|
|
data/Rakefile
CHANGED
data/lib/heredoc_unindent.rb
CHANGED
@@ -1,37 +1,89 @@
|
|
1
1
|
module CoreExt
|
2
2
|
module String
|
3
|
-
|
3
|
+
|
4
|
+
# This module, which is automatically included in ::String, define in-place
|
5
|
+
# and out-of-place unindentation methods.
|
4
6
|
#
|
5
|
-
|
6
|
-
# +heredoc_unindent+ and alias it as +unindent+.
|
7
|
-
##
|
7
|
+
module HeredocUnindent
|
8
8
|
|
9
|
-
|
10
|
-
|
11
|
-
#
|
12
|
-
#
|
13
|
-
#
|
14
|
-
#
|
15
|
-
#
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
9
|
+
private
|
10
|
+
|
11
|
+
# Actual implementation of the unindentation mechanism,
|
12
|
+
# both for in and out-of-place processing.
|
13
|
+
#
|
14
|
+
# @param [Boolean] warn_first_not_min
|
15
|
+
# @param [Boolean] in_place
|
16
|
+
# @return [String, nil]
|
17
|
+
#
|
18
|
+
def unindent_base(in_place, warn_first_not_min)
|
19
|
+
margins = self.scan(/^\s*/).map(&:size)
|
20
|
+
margins_min = margins.min
|
21
|
+
if margins.first != margins_min && warn_first_not_min
|
22
|
+
puts "warning: margin of the first line differs from minimum margin"
|
23
23
|
end
|
24
|
-
|
25
|
-
|
24
|
+
return in_place ? nil : self unless margins_min != 0
|
25
|
+
re = Regexp.new('^\s{0,' + margins_min.to_s + '}' ) # omitting the lower limit produces warnings and wrong behavior in ruby-1.8.7-p330 and ree-1.8.7-2010.02
|
26
|
+
in_place ? gsub!(re, '') : gsub(re, '')
|
27
|
+
end
|
28
|
+
|
29
|
+
public
|
30
|
+
|
31
|
+
# Removes common margin from indented strings, such as the ones produced
|
32
|
+
# by indented heredocs. In other words, it strips out leading whitespace
|
33
|
+
# chars at the beggining of each line, but only as much as the line with
|
34
|
+
# the smallest margin.
|
35
|
+
#
|
36
|
+
# Unless the optional argument +warn_first_dif_min+ is set to +false+ (or
|
37
|
+
# +nil+), a warning is produced when the margin of the first line differs
|
38
|
+
# from the minimum.
|
39
|
+
#
|
40
|
+
# @example
|
41
|
+
#
|
42
|
+
# if true
|
43
|
+
# s = <<-EOS
|
44
|
+
# How sad it is to be unable to unindent heredocs
|
45
|
+
# EOS
|
46
|
+
# t = <<-EOS.unindent
|
47
|
+
# How wonderful it is to be able to do it!
|
48
|
+
# EOS
|
49
|
+
# end
|
50
|
+
#
|
51
|
+
# s[0..12] #=> " How sad"
|
52
|
+
# t[0..12] #=> "How wonderful"
|
53
|
+
#
|
54
|
+
# @param [Boolean] warn_first_not_min toggle warning if the margin of the first line differs from minimum margin
|
55
|
+
# @return [::String] unindented string
|
56
|
+
#
|
57
|
+
def heredoc_unindent(warn_first_not_min=true)
|
58
|
+
unindent_base(false, warn_first_not_min)
|
26
59
|
end
|
27
60
|
alias unindent heredoc_unindent
|
28
61
|
|
29
|
-
#
|
62
|
+
# Same as #heredoc_unindent, but works in-place. Returns self, or nil if
|
63
|
+
# no changes were made
|
64
|
+
#
|
65
|
+
# @note Avoid attributing the return value of this method because it
|
66
|
+
# may be nil (see Example 2).
|
67
|
+
#
|
68
|
+
# @example 1 Recommended
|
69
|
+
#
|
70
|
+
# s = " foo" #=> " foo"
|
71
|
+
# s.unindent! #=> "foo"
|
72
|
+
# s.unindent! #=> nil
|
73
|
+
#
|
74
|
+
# @example 2 Disencouraged
|
75
|
+
#
|
76
|
+
# s = <<-EOS.unindent!
|
77
|
+
# the result would be as intended
|
78
|
+
# if this line weren't unindented
|
79
|
+
# EOS
|
80
|
+
# s #=> nil
|
81
|
+
#
|
82
|
+
# @param [Boolean] warn_first_dif_min toggle warning if the margin of the first line differs from minimum margin
|
83
|
+
# @return [::String, NilClass] unindented self, or nil if no changes were made
|
30
84
|
#
|
31
|
-
def heredoc_unindent!(
|
32
|
-
|
33
|
-
self.replace(self.heredoc_unindent(warn_first_dif_min))
|
34
|
-
self != orig ? self : nil
|
85
|
+
def heredoc_unindent!(warn_first_not_min=true)
|
86
|
+
unindent_base(true, warn_first_not_min)
|
35
87
|
end
|
36
88
|
alias unindent! heredoc_unindent!
|
37
89
|
end
|
metadata
CHANGED
metadata.gz.sig
CHANGED
@@ -1,2 +1,2 @@
|
|
1
|
-
|
2
|
-
|
1
|
+
JZi�����y�ZDĕaKA5�-oF �����?V�3hQ�Ź'GB[}����<�D�n��p�Q�Α�rI��,W���-��oW�NPv��b9�
|
2
|
+
Z��̡`� aE��A��=kƖ�!A7Y����/i��Q���
|