Class: Vedeu::Wordwrap

Inherits:
Object
  • Object
show all
Defined in:
lib/vedeu/support/wordwrap.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(value, options = {}) ⇒ Wordwrap

Returns a new instance of Wordwrap.



7
8
9
10
# File 'lib/vedeu/support/wordwrap.rb', line 7

def initialize(value, options = {})
  @value   = value
  @options = options || {}
end

Class Method Details

.this(value, options = {}) ⇒ Object



3
4
5
# File 'lib/vedeu/support/wordwrap.rb', line 3

def self.this(value, options = {})
  new(value, options).reformat
end

Instance Method Details

#reformatObject



12
13
14
15
16
# File 'lib/vedeu/support/wordwrap.rb', line 12

def reformat
  return pruned if prune?

  wordwrapped
end

#wordwrappedObject



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/vedeu/support/wordwrap.rb', line 18

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