Class: LovelyRufus::Wrapper

Inherits:
Object
  • Object
show all
Defined in:
lib/lovely-rufus/wrapper.rb

Constant Summary collapse

NBSP =
' '

Instance Method Summary collapse

Constructor Details

#initialize(text) ⇒ Wrapper

Returns a new instance of Wrapper.



7
8
9
# File 'lib/lovely-rufus/wrapper.rb', line 7

def initialize text
  @paras = text.split(/\n[\/#> ]*\n/).map(&:strip)
end

Instance Method Details

#wrapped(max_width = 72) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/lovely-rufus/wrapper.rb', line 11

def wrapped max_width = 72
  return '' if paras.all?(&:empty?)

  paras.map do |para|
    best = wrap_para_to_width para, max_width
    (max_width - 1).downto 1 do |width|
      shorter = wrap_para_to_width para, width
      break if shorter.lines.count           > best.lines.count
      break if shorter.lines.map(&:size).max > best.lines.map(&:size).max
      best = shorter
    end
    best
  end.join "\n\n"
end