Sha256: 29d6428ee7e66901c1157b242512482c6ace6512dee3677e5f7aceec0dd3f86c
Contents?: true
Size: 1.39 KB
Versions: 4
Compression:
Stored size: 1.39 KB
Contents
# encoding: utf-8 module Cri::CoreExtensions module String # @todo Document def to_paragraphs lines = self.scan(/([^\n]+\n|[^\n]*$)/).map { |s| s[0].strip } paragraphs = [ [] ] lines.each do |line| if line.empty? paragraphs << [] else paragraphs.last << line end end paragraphs.reject { |p| p.empty? }.map { |p| p.join(' ') } end # Word-wraps and indents the string. # # +width+:: The maximal width of each line. This also includes indentation, # i.e. the actual maximal width of the text is width-indentation. # # +indentation+:: The number of spaces to indent each wrapped line. def wrap_and_indent(width, indentation) # Split into paragraphs paragraphs = self.to_paragraphs # Wrap and indent each paragraph paragraphs.map do |paragraph| # Initialize lines = [] line = '' # Split into words paragraph.split(/\s/).each do |word| # Begin new line if it's too long if (line + ' ' + word).length >= width lines << line line = '' end # Add word to line line += (line == '' ? '' : ' ' ) + word end lines << line # Join lines lines.map { |l| ' '*indentation + l }.join("\n") end.join("\n\n") end end end
Version data entries
4 entries across 4 versions & 1 rubygems
Version | Path |
---|---|
cri-2.0b1 | lib/cri/core_ext/string.rb |
cri-2.0a3 | lib/cri/core_ext/string.rb |
cri-2.0a2 | lib/cri/core_ext/string.rb |
cri-2.0a1 | lib/cri/core_ext/string.rb |