Method: String#wrap
- Defined in:
- lib/liquidoc.rb
#wrap(options = {}) ⇒ Object
Adapted from Nikhil Gupta nikhgupta.com/code/wrapping-long-lines-in-ruby-for-display-in-source-files/
1237 1238 1239 1240 1241 1242 1243 1244 1245 |
# File 'lib/liquidoc.rb', line 1237 def wrap = {} width = .fetch(:width, 76) # length to wrap at pre = .fetch(:prepend, '') # text to prepend app = .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 |