string-text 0.6.2 → 0.7.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/string-text/version.rb +1 -1
- data/lib/string-text.rb +14 -6
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f521ec689679b276745de15322743355ff323feed9e23db36ec574f131b00ef0
|
4
|
+
data.tar.gz: 233641ed8ba331a68d11f7fdfca2f803a0a8b634099721c054336d1f168fa5ab
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 55b7fc59f836fcb8f46f92f0ca19028d137265ea568295ef605065106d261bef910cb4ad9af3cfe34502d8c19480e8e8cd635f0d0f52b47a712fdf8af03c7f16
|
7
|
+
data.tar.gz: 5b816358febeeedb34a3c185ed56373de820127b9326be8c9d9280e28a1162535872c023ea21eeea3216d84e02079c652ca882115eb41bb1ca085b155ff4885d
|
data/lib/string-text/version.rb
CHANGED
data/lib/string-text.rb
CHANGED
@@ -12,7 +12,10 @@ module String::Text
|
|
12
12
|
# uses the indent of the least indented non-empty line as the indent of the
|
13
13
|
# block that is then aligned as a whole to the given column; lines starting
|
14
14
|
# at column one are realigned to the indent of the previous line. Initial
|
15
|
-
# and final empty lines are ignored
|
15
|
+
# and final empty lines are ignored
|
16
|
+
#
|
17
|
+
# If :bol is false then the first line won't be indented or outdented. If
|
18
|
+
# :empty is false (the default), initial and final blank lines are removed
|
16
19
|
#
|
17
20
|
# FIXME: make the special rule for lines starting at column one an option
|
18
21
|
#
|
@@ -48,18 +51,23 @@ module String::Text
|
|
48
51
|
#
|
49
52
|
# If :bol is false then the first line won't be indented or outdented
|
50
53
|
#
|
51
|
-
def align(column = 1, bol: true)
|
54
|
+
def align(column = 1, bol: true, empty: false)
|
52
55
|
column > 0 or raise ArgumentError "Illegal column: #{column}"
|
53
56
|
initial = " " * (column - 1)
|
54
57
|
|
55
58
|
# Remove initial and final empty lines
|
56
|
-
lines = self.split(/\n
|
57
|
-
|
58
|
-
|
59
|
+
lines = self.split(/\n/, -1).map &:rstrip
|
60
|
+
if !empty
|
61
|
+
lines.pop while !lines.empty? && !(lines.last =~ /^\s*\S/)
|
62
|
+
lines.shift while !lines.empty? && !(lines.first =~ /^\s*\S/)
|
63
|
+
line = lines.first # First non-empty line
|
64
|
+
else
|
65
|
+
line = lines.find { |line| line =~ /\S/ }
|
66
|
+
end
|
59
67
|
return "" if lines.empty?
|
60
68
|
|
61
69
|
# Only align to given column if first line is not indented
|
62
|
-
if String::Text.indentation(
|
70
|
+
if String::Text.indentation(line) == 0
|
63
71
|
return lines.map { |line| ' ' * (column-1) + line }.join("\n")
|
64
72
|
end
|
65
73
|
|