Class: Text::Reform::BreakWith

Inherits:
Object
  • Object
show all
Defined in:
lib/text/reform.rb

Instance Method Summary collapse

Constructor Details

#initialize(hyphen) ⇒ BreakWith

Returns a new instance of BreakWith.



1443
1444
1445
1446
# File 'lib/text/reform.rb', line 1443

def initialize hyphen
  @hyphen = hyphen
  @hylen = hyphen.length
end

Instance Method Details

#break(str, initial_max_length, total_width) ⇒ Object

Break by inserting a hyphen string.

initial_max_length

The maximum size of the first part of the word that will remain on the first line.

total_width

The total width that can be appended to this first line.



1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
# File 'lib/text/reform.rb', line 1454

def break(str, initial_max_length, total_width)
  if total_width <= @hylen
    ret = [str[0...1], str[1..-1]]
  else
    ret = [str[0...(initial_max_length-@hylen)], str[(initial_max_length-@hylen)..-1]]
  end

  if ret.first =~ /\A\s*\Z/
    return ['', str]
  else
    return [ret.first + @hyphen, ret.last]
  end
end