Sha256: 494af1ab97715edc1e1fb7f3d3cf16344d598ce19981f16fe773455a7761e2e8

Contents?: true

Size: 858 Bytes

Versions: 2

Compression:

Stored size: 858 Bytes

Contents

require_relative 'basic_wrapper'
require_relative 'hangout_wrapper'
require_relative 'one_letter_gluer'
require_relative 'quote_stripper'
require_relative 'wrap'

module LovelyRufus
  class TextWrapper
    def self.wrap(text, width: 72)
      new(Wrap[text, width: width]).call
    end

    def initialize(wrap)
      @wrap = wrap
    end

    def call
      paras.map do |para|
        chain.call(Wrap[para, width: wrap.width]).text.tr(NBSP, ' ')
      end.join("\n")
    end

    attr_reader :wrap
    private     :wrap

    private

    def chain
      layers = [QuoteStripper, OneLetterGluer, BasicWrapper, HangoutWrapper]
      identity = -> (wrap) { wrap }
      layers.reverse.reduce(identity) { |inner, outer| outer.new(inner) }
    end

    def paras
      wrap.text.split(/\n#{QUOTES}?\n/).reject { |par| par[/^(#{QUOTES}| )$/] }
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
lovely_rufus-0.2.1 lib/lovely_rufus/text_wrapper.rb
lovely_rufus-0.2.0 lib/lovely_rufus/text_wrapper.rb