Sha256: 6c06611e6147ab6185be2b70e4c6666f23f42001c4de1a0d34cc3aa12370ffe5

Contents?: true

Size: 1.03 KB

Versions: 2

Compression:

Stored size: 1.03 KB

Contents

require_relative 'layers/basic_wrapper'
require_relative 'layers/code_comment_stripper'
require_relative 'layers/email_quote_stripper'
require_relative 'layers/hangout_wrapper'
require_relative 'layers/one_letter_gluer'
require_relative 'wrap'

module LovelyRufus
  class TextWrapper
    QUOTES = /^([>\/#])(\1| )*/

    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

    private_attr_reader :wrap

    private

    def chain
      layers = [Layers::CodeCommentStripper, Layers::EmailQuoteStripper,
                Layers::OneLetterGluer, Layers::BasicWrapper,
                Layers::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.3.1 lib/lovely_rufus/text_wrapper.rb
lovely_rufus-0.3.0 lib/lovely_rufus/text_wrapper.rb