Sha256: ef556a4647864c3ed60e73bf9ae87b1d0d3da52ec58ee167547c580d55502e39

Contents?: true

Size: 1.07 KB

Versions: 1

Compression:

Stored size: 1.07 KB

Contents

module Manilla
  #
  # Breaks long logical lines of text into multiple delimited representations.
  # Lines can be folded into a set width of octets excluding line breaks.
  #
  module Folder
    def self.call(text, maxwidth, delimiter, break_on)
      lines = []

      value = text.strip
      value = value.gsub(/\n+/, '')
      value = value.gsub(/\s{2,}/, '')

      while value.size > maxwidth
        count = maxwidth

        while count
          break_index = nil
          break_pos = nil
          line = value[0, count]

          if break_on == :word
            break_index = line.rindex(/\s/)
            break_pos = break_index + 1 if break_index
            line = line[0, break_pos] if break_pos

            count = break_pos || maxwidth
          end

          oct = line.size

          if oct > maxwidth
            count -= oct - maxwidth
          else
            lines << line
            value = value[count, value.size]
            break
          end
        end
      end

      lines << value unless value.strip.empty?

      lines.join(delimiter)
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
manilla-1.0.0 lib/manilla/folder.rb