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)


83
84
85
# File 'lib/vedeu/output/wordwrap.rb', line 83

def options
  @options
end

#textString (readonly, protected)

Returns:

  • (String)


79
80
81
# File 'lib/vedeu/output/wordwrap.rb', line 79

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>)


159
160
161
162
163
164
165
# File 'lib/vedeu/output/wordwrap.rb', line 159

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

#ellipsisString (private)

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

Returns:

  • (String)


133
134
135
# File 'lib/vedeu/output/wordwrap.rb', line 133

def ellipsis
  options[:ellipsis]
end

#ellipsis_string(string) ⇒ String (private)

Parameters:

  • string (String)

Returns:

  • (String)


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

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

  "#{prune_string(string)}#{ellipsis}".freeze
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)


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

def mode
  options[:mode]
end

#pruneArray<String>|String

Returns:

  • (Array<String>|String)


38
39
40
41
42
43
44
45
46
47
48
# File 'lib/vedeu/output/wordwrap.rb', line 38

def prune
  return text if text.size <= pruned_width

  if split_lines.size > 1
    split_lines.reduce([]) { |a, e| a << ellipsis_string(e) }

  else
    ellipsis_string(text)

  end
end

#prune_string(string) ⇒ String (private)

Returns the string pruned.

Parameters:

  • string (String)

Returns:

  • (String)


119
120
121
# File 'lib/vedeu/output/wordwrap.rb', line 119

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)


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

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>)


103
104
105
# File 'lib/vedeu/output/wordwrap.rb', line 103

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

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

Parameters:

  • text_as_lines (Array<String>)

Returns:



89
90
91
92
93
94
95
96
97
98
# File 'lib/vedeu/output/wordwrap.rb', line 89

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)


152
153
154
# File 'lib/vedeu/output/wordwrap.rb', line 152

def width
  options[:width]
end

#wrapString

Returns:

  • (String)


51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/vedeu/output/wordwrap.rb', line 51

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([]) { |a, e| a << e.join(' ') }
end