Class: Vedeu::Wordwrap

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

Overview

Wrap or prune text.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(text, options = {}) ⇒ Vedeu::Wordwrap

Parameters:

  • text (String)
  • options (Hash) (defaults to: {})

Options Hash (options):

  • ellipsis (String)
  • width (Fixnum)


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

def initialize(text, options = {})
  @text    = text
  @options = defaults.merge!(options)
end

Instance Attribute Details

#optionsObject (readonly, private)

Returns the value of attribute options.



81
82
83
# File 'lib/vedeu/output/wordwrap.rb', line 81

def options
  @options
end

#textObject (readonly, private)

Returns the value of attribute text.



81
82
83
# File 'lib/vedeu/output/wordwrap.rb', line 81

def text
  @text
end

Instance Method Details

#as_linesVedeu::Lines Also known as: prune_as_lines, wrap_as_lines

Returns:



64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/vedeu/output/wordwrap.rb', line 64

def as_lines
  if __callee__ == :prune_as_lines
    to_line_objects(prune)

  elsif __callee__ == :wrap_as_lines
    to_line_objects(wrap)

  else
    to_line_objects(split_lines)

  end
end

#defaultsHash (private)

Returns:

  • (Hash)


146
147
148
149
150
151
# File 'lib/vedeu/output/wordwrap.rb', line 146

def defaults
  {
    ellipsis: '...',
    width:    70,
  }
end

#ellipsisString (private)

Returns:

  • (String)


136
137
138
# File 'lib/vedeu/output/wordwrap.rb', line 136

def ellipsis
  options.fetch(:ellipsis)
end

#ellipsis_string(string) ⇒ String (private)

Parameters:

  • string (String)

Returns:

  • (String)


114
115
116
117
118
119
120
121
122
# File 'lib/vedeu/output/wordwrap.rb', line 114

def ellipsis_string(string)
  if string.size < ellipsis.size
    prune_string(string)

  else
    [prune_string(string), ellipsis].join

  end
end

#outputString (private)

Note:

This may be unused. (GL 2015-02-20)

Returns:

  • (String)


106
107
108
109
110
# File 'lib/vedeu/output/wordwrap.rb', line 106

def output
  processed.reduce([]) do |output, line|
    output << line.join(' ')
  end.join("\n")
end

#pruneArray<String>|String

Returns:

  • (Array<String>|String)


18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/vedeu/output/wordwrap.rb', line 18

def prune
  return text if text.size <= pruned_width

  processed = []

  if split_lines.size > 1
    processed = split_lines.reduce([]) do |acc, line|
      acc << ellipsis_string(line)
    end

  else
    processed = ellipsis_string(text)

  end

  processed
end

#prune_string(string) ⇒ String (private)

Parameters:

  • string (String)

Returns:

  • (String)


126
127
128
# File 'lib/vedeu/output/wordwrap.rb', line 126

def prune_string(string)
  string.chomp.slice(0..pruned_width)
end

#pruned_widthFixnum (private)

Returns:



131
132
133
# File 'lib/vedeu/output/wordwrap.rb', line 131

def pruned_width
  width - ellipsis.size
end

#split_linesArray<String> (private)

Returns:

  • (Array<String>)


98
99
100
# File 'lib/vedeu/output/wordwrap.rb', line 98

def split_lines
  text.split(/\n/)
end

#to_line_objects(text_as_lines) ⇒ Vedeu::Lines (private)

Parameters:

  • text_as_lines (Array<String>)

Returns:



86
87
88
89
90
91
92
93
94
95
# File 'lib/vedeu/output/wordwrap.rb', line 86

def to_line_objects(text_as_lines)
  line_objects = Array(text_as_lines).map do |text_line|
    stream        = Vedeu::Stream.new({ value: text_line })
    line          = Vedeu::Line.new
    stream.parent = line
    line.add(stream)
    line
  end
  Vedeu::Lines.new(line_objects)
end

#widthFixnum (private)

Returns:



141
142
143
# File 'lib/vedeu/output/wordwrap.rb', line 141

def width
  options.fetch(:width)
end

#wrapString

Returns:

  • (String)


37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/vedeu/output/wordwrap.rb', line 37

def wrap
  processed = []
  text.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) >= width
        line_length = word_length
        processed   << reformatted
        reformatted = []
      end

      reformatted << word
    end

    processed << reformatted
  end

  processed.reduce([]) do |output, line|
    output << line.join(' ')
  end
end