Method: String#wrap

Defined in:
lib/liquidoc.rb

#wrap(options = {}) ⇒ Object



1237
1238
1239
1240
1241
1242
1243
1244
1245
# File 'lib/liquidoc.rb', line 1237

def wrap options = {}
  width = options.fetch(:width, 76) # length to wrap at
  pre = options.fetch(:prepend, '') # text to prepend
  app = options.fetch(:append, '') # text to append
  chars = pre.size + app.size
  self.strip.split("\n").collect do |line|
    line.length + chars.size > width ? line.gsub(/(.{1,#{(width - chars)}})(\s+|$)/, "#{pre}\\1#{app}\n") : "#{pre}#{line}#{app}\n"
  end.map(&:rstrip).join("\n")
end