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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 5595a21634c9c54723c5e357e923e9c87c312a98005bb959dc98d8b69ad58480
4
- data.tar.gz: 8da08b627f45ab3f25dbac74915c3a844b45270c5cfd3b742eb95b0287de0a50
3
+ metadata.gz: f521ec689679b276745de15322743355ff323feed9e23db36ec574f131b00ef0
4
+ data.tar.gz: 233641ed8ba331a68d11f7fdfca2f803a0a8b634099721c054336d1f168fa5ab
5
5
  SHA512:
6
- metadata.gz: ee0fdb300976264a9dd98dc25f1a73d9cd5f5258e1d272c327fd1bb51fe66d9c79d913192f3ac18a541972d5ba8469f2db62b6cea486babef8400a62b831800c
7
- data.tar.gz: 64092e8dd171c15f40b202fd6849aed0b85ef1fa973f4257410a374bb0339ff29d546f54eb3b5021bd2aa223f0caa8f813e2f1d0926c1035d7d5afed3adf042b
6
+ metadata.gz: 55b7fc59f836fcb8f46f92f0ca19028d137265ea568295ef605065106d261bef910cb4ad9af3cfe34502d8c19480e8e8cd635f0d0f52b47a712fdf8af03c7f16
7
+ data.tar.gz: 5b816358febeeedb34a3c185ed56373de820127b9326be8c9d9280e28a1162535872c023ea21eeea3216d84e02079c652ca882115eb41bb1ca085b155ff4885d
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module String::Text
4
- VERSION = "0.6.2"
4
+ VERSION = "0.7.0"
5
5
  end
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/).map &:rstrip
57
- lines.pop while !lines.empty? && !(lines.last =~ /^\s*\S/)
58
- lines.shift while !lines.empty? && !(lines.first =~ /^\s*\S/)
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(lines.first) == 0
70
+ if String::Text.indentation(line) == 0
63
71
  return lines.map { |line| ' ' * (column-1) + line }.join("\n")
64
72
  end
65
73
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: string-text
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.2
4
+ version: 0.7.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Claus Rasmussen