Class: Vedeu::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::Wordwrap

Returns a new instance of Vedeu::Wordwrap.

Parameters:

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

Options Hash (options):

  • ellipsis (String)

    For when using mode ‘:prune`.

  • mode (Symbol)

    One of :default, :prune, :wrap

  • width (Fixnum)

    The width to prune or wrap to.



19
20
21
22
# File 'lib/vedeu/output/wordwrap.rb', line 19

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

Instance Attribute Details

#optionsHash (readonly, protected)

Returns:

  • (Hash)


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

def options
  @options
end

#textString (readonly, protected)

Returns:

  • (String)


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

def text
  @text
end

Class Method Details

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

See Also:

  • {Vedeu{Vedeu::Wordwrap{Vedeu::Wordwrap#initialize}


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

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

Instance Method Details

#contentVedeu::Lines

Returns:



25
26
27
28
29
30
31
32
# File 'lib/vedeu/output/wordwrap.rb', line 25

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:

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


149
150
151
152
153
154
155
# File 'lib/vedeu/output/wordwrap.rb', line 149

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

#ellipsisString (private)

Returns:

  • (String)


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

def ellipsis
  options.fetch(:ellipsis)
end

#ellipsis_string(string) ⇒ String (private)

Parameters:

  • string (String)

Returns:

  • (String)


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

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

  else
    [prune_string(string), ellipsis].join

  end
end

#modeSymbol (private)

Returns:

  • (Symbol)


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

def mode
  options.fetch(:mode)
end

#pruneArray<String>|String

Returns:

  • (Array<String>|String)


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

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)


124
125
126
# File 'lib/vedeu/output/wordwrap.rb', line 124

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

#pruned_widthFixnum (private)

Returns:

  • (Fixnum)


129
130
131
# File 'lib/vedeu/output/wordwrap.rb', line 129

def pruned_width
  width - ellipsis.size
end

#split_linesArray<String> (private)

Returns:

  • (Array<String>)


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

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

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

Parameters:

  • text_as_lines (Array<String>)

Returns:



94
95
96
97
98
99
100
101
102
103
# File 'lib/vedeu/output/wordwrap.rb', line 94

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:

  • (Fixnum)


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

def width
  options.fetch(:width)
end

#wrapString

Returns:

  • (String)


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

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