Sha256: 2a4e13ab9627cb38f9d2d389d5c18ba89f86ea535281def310f54ed4487ea88c
Contents?: true
Size: 1.46 KB
Versions: 23
Compression:
Stored size: 1.46 KB
Contents
module Vedeu class Wordwrap def self.this(value, options = {}) new(value, options).reformat end def initialize(value, options = {}) @value = value @options = options || {} end def reformat return pruned if prune? wordwrapped end def wordwrapped processed = [] value.split(/\n/).map do |unprocessed| line_length = 0 reformatted = [] unprocessed.split(/\s/).map do |word| word_length = word.length + 1 if (line_length += word_length) >= maximum_width line_length = word_length processed << reformatted reformatted = [] end reformatted << word end processed << reformatted end output(processed) end private attr_reader :value, :options def output(paragraph) paragraph.reduce([]) do |output, line| output << line.join(' ') end.join("\n") end def pruned if value.size <= pruned_width value else [ value.chomp.slice(0..pruned_width), '...' ].join end end def pruned_width maximum_width - 3 end def prune? options.fetch(:prune) end def maximum_width options.fetch(:width) end def options defaults.merge!(@options) end def defaults { width: 70, prune: false } end end end
Version data entries
23 entries across 23 versions & 1 rubygems