Class: Text::Reform::BreakAt

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

Instance Method Summary collapse

Constructor Details

#initialize(hyphen) ⇒ BreakAt

Returns a new instance of BreakAt.



1470
1471
1472
# File 'lib/text/reform.rb', line 1470

def initialize hyphen
  @hyphen = hyphen
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.



1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
# File 'lib/text/reform.rb', line 1480

def break(str, initial_max_length, total_width)
  max = total_width - @hyphen.length
  if max <= 0
    ret = [str[0, 1], str[1, -1]]
  elsif str =~ /(.{1,#{max}}#@hyphen)(.*)/s
    ret = [ $1, $2 ]
  elsif str.length > total_width
    sep = initial_max_length-@hyphen.length
    ret = [
      str[0, sep]+@hyphen,
      str[sep..-1]
    ]
  else
    ret = [ '', str ]
  end

  return '', str if ret[0] =~ /\A\s*\Z/
    return ret
end