Class: Vedeu::Wordwrap
- Inherits:
-
Object
- Object
- Vedeu::Wordwrap
- Defined in:
- lib/vedeu/output/wordwrap.rb
Overview
Wrap or prune text.
Instance Attribute Summary collapse
-
#options ⇒ Object
readonly
private
Returns the value of attribute options.
-
#text ⇒ Object
readonly
private
Returns the value of attribute text.
Instance Method Summary collapse
- #as_lines ⇒ Vedeu::Lines (also: #prune_as_lines, #wrap_as_lines)
- #defaults ⇒ Hash private
- #ellipsis ⇒ String private
- #ellipsis_string(string) ⇒ String private
- #initialize(text, options = {}) ⇒ Vedeu::Wordwrap constructor
- #output ⇒ String private
- #prune ⇒ Array<String>|String
- #prune_string(string) ⇒ String private
- #pruned_width ⇒ Fixnum private
- #split_lines ⇒ Array<String> private
- #to_line_objects(text_as_lines) ⇒ Vedeu::Lines private
- #width ⇒ Fixnum private
- #wrap ⇒ String
Constructor Details
#initialize(text, options = {}) ⇒ Vedeu::Wordwrap
12 13 14 15 |
# File 'lib/vedeu/output/wordwrap.rb', line 12 def initialize(text, = {}) @text = text @options = defaults.merge!() end |
Instance Attribute Details
#options ⇒ Object (readonly, private)
Returns the value of attribute options.
81 82 83 |
# File 'lib/vedeu/output/wordwrap.rb', line 81 def @options end |
#text ⇒ Object (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_lines ⇒ Vedeu::Lines Also known as: prune_as_lines, wrap_as_lines
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 |
#defaults ⇒ Hash (private)
146 147 148 149 150 151 |
# File 'lib/vedeu/output/wordwrap.rb', line 146 def defaults { ellipsis: '...', width: 70, } end |
#ellipsis ⇒ String (private)
136 137 138 |
# File 'lib/vedeu/output/wordwrap.rb', line 136 def ellipsis .fetch(:ellipsis) end |
#ellipsis_string(string) ⇒ String (private)
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 |
#output ⇒ String (private)
Note:
This may be unused. (GL 2015-02-20)
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 |
#prune ⇒ 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)
126 127 128 |
# File 'lib/vedeu/output/wordwrap.rb', line 126 def prune_string(string) string.chomp.slice(0..pruned_width) end |
#pruned_width ⇒ Fixnum (private)
131 132 133 |
# File 'lib/vedeu/output/wordwrap.rb', line 131 def pruned_width width - ellipsis.size end |
#split_lines ⇒ Array<String> (private)
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)
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 |
#width ⇒ Fixnum (private)
141 142 143 |
# File 'lib/vedeu/output/wordwrap.rb', line 141 def width .fetch(:width) end |
#wrap ⇒ 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 |