Sha256: 059cad7eebf55e4ed80eb2a26e7fe3e401494fa25920ef8b960f6dc2373832fa

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 = %r{^([>/#])(\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-1.0.0 lib/lovely_rufus/text_wrapper.rb
lovely_rufus-0.3.2 lib/lovely_rufus/text_wrapper.rb