Class: Vedeu::Output::Wordwrap

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

Overview

Wrap or prune text.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Vedeu::Output::Wordwrap.

Parameters:

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

Options Hash (options):



22
23
24
25
# File 'lib/vedeu/output/wordwrap.rb', line 22

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

Instance Attribute Details

#optionsHash (readonly, protected)

Returns:

  • (Hash)


91
92
93
# File 'lib/vedeu/output/wordwrap.rb', line 91

def options
  @options
end

#textString (readonly, protected)

Returns:

  • (String)


87
88
89
# File 'lib/vedeu/output/wordwrap.rb', line 87

def text
  @text
end

Class Method Details

.for(text, options = {}) ⇒ Object

See Also:



10
11
12
# File 'lib/vedeu/output/wordwrap.rb', line 10

def self.for(text, options = {})
  new(text, options).content
end

Instance Method Details

#contentVedeu::Views::Lines

Returns:



28
29
30
31
32
33
34
35
# File 'lib/vedeu/output/wordwrap.rb', line 28

def content
  case mode
  when :prune then to_line_objects(prune)
  when :wrap  then to_line_objects(wrap)
  else
    to_line_objects(split_lines)
  end
end

#defaultsHash<Symbol => Fixnum, String, Symbol> (private)

Returns the default options/attributes for this class.

Returns:

  • (Hash<Symbol => Fixnum, String, Symbol>)


168
169
170
171
172
173
174
# File 'lib/vedeu/output/wordwrap.rb', line 168

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

#ellipsisString (private)

For when using mode ‘:prune`, by default, provides ’…‘.

Returns:

  • (String)


145
146
147
# File 'lib/vedeu/output/wordwrap.rb', line 145

def ellipsis
  options.fetch(:ellipsis)
end

#ellipsis_string(string) ⇒ String (private)

Parameters:

  • string (String)

Returns:

  • (String)


117
118
119
120
121
122
123
124
125
# File 'lib/vedeu/output/wordwrap.rb', line 117

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

  else
    "#{prune_string(string)}#{ellipsis}"

  end
end

#modeSymbol (private)

Returns the word wrapping mode. One of :default, :prune or :wrap;

:default = Renders the content as is.
:prune   = Discards the remainder of the content line after width.
:wrap    = Forces the content on to a new line after width.

Returns:

  • (Symbol)


154
155
156
# File 'lib/vedeu/output/wordwrap.rb', line 154

def mode
  options.fetch(:mode)
end

#pruneArray<String>|String

Returns:

  • (Array<String>|String)


38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/vedeu/output/wordwrap.rb', line 38

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)

Returns the string pruned.

Parameters:

  • string (String)

Returns:

  • (String)


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

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

#pruned_widthFixnum (private)

Returns the width of the string minus the ellipsis.

Returns:

  • (Fixnum)


138
139
140
# File 'lib/vedeu/output/wordwrap.rb', line 138

def pruned_width
  width - ellipsis.size
end

#split_linesArray<String> (private)

Returns the text as an array of lines, split on ‘n’.

Returns:

  • (Array<String>)


111
112
113
# File 'lib/vedeu/output/wordwrap.rb', line 111

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

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

Parameters:

  • text_as_lines (Array<String>)

Returns:



97
98
99
100
101
102
103
104
105
106
# File 'lib/vedeu/output/wordwrap.rb', line 97

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

#widthFixnum (private)

Returns the width to prune or wrap to.

Returns:

  • (Fixnum)


161
162
163
# File 'lib/vedeu/output/wordwrap.rb', line 161

def width
  options.fetch(:width)
end

#wrapString

Returns:

  • (String)


57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/vedeu/output/wordwrap.rb', line 57

def wrap
  processed = []
  split_lines.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