Class: Verse::Wrapping

Inherits:
Object
  • Object
show all
Defined in:
lib/verse/wrapping.rb

Overview

A class responsible for text wrapping

Constant Summary collapse

DEFAULT_WIDTH =
80.freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(text, options = {}) ⇒ Wrapping

Initialize a Wrapping

Parameters:

  • text (String)

    the text to be wrapped

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

    @option options [Symbol] :padding the desired spacing



17
18
19
20
21
# File 'lib/verse/wrapping.rb', line 17

def initialize(text, options = {})
  @text    = text
  @line_width = options.fetch(:line_width) { DEFAULT_WIDTH }
  @sanitizer = Sanitizer.new
end

Class Method Details

.wrap(text, wrap_at, options = {}) ⇒ Object

Wrap a text into lines no longer than wrap_at



26
27
28
# File 'lib/verse/wrapping.rb', line 26

def self.wrap(text, wrap_at, options = {})
  new(text, options).wrap(wrap_at)
end

Instance Method Details

#format_paragraph(paragraph, wrap_at) ⇒ Array[String]

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Format paragraph to be maximum of wrap_at length

Parameters:

  • paragraph (String)

    the paragraph to format

  • wrap_at (Integer)

    the maximum length to wrap the paragraph

Returns:

  • (Array[String])

    the wrapped lines



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/verse/wrapping.rb', line 62

def format_paragraph(paragraph, wrap_at)
  cleared_para = @sanitizer.replace(paragraph)
  lines = []
  line = ''
  word  = ''
  word_length = 0
  line_length = 0
  char_length = 0 # visible char length
  text_length = display_width(cleared_para)
  total_length = 0
  UnicodeUtils.each_grapheme(cleared_para) do |char|
    char_length = display_width(char)
    total_length += char_length
    if line_length + word_length + char_length <= wrap_at
      if char == SPACE || total_length == text_length
        line << word + char
        line_length += word_length + char_length
        word = ''
        word_length = 0
      else
        word << char
        word_length += char_length
      end
      next
    end

    if char == SPACE # ends with space
      lines << line
      line = ''
      line_length = 0
      word = word + char
      word_length = word_length + char_length
    elsif word_length + char_length <= wrap_at
      lines << line
      line = word + char
      line_length = word_length + char_length
      word = ''
      word_length = 0
    else # hyphenate word - too long to fit a line
      lines << word
      line_length = 0
      word = char
      word_length = char_length
    end
  end
  lines << line unless line.empty?
  lines << word unless word.empty?
  lines
end

#wrap(wrap_at = DEFAULT_WIDTH) ⇒ Object

Wrap a text into lines no longer than wrap_at length. Preserves existing lines and existing word boundaries.

Examples:

wrapping = Verse::Wrapping.new "Some longish text"

wrapping.wrap(8)
# => >Some
     >longish
     >text


42
43
44
45
46
47
48
49
# File 'lib/verse/wrapping.rb', line 42

def wrap(wrap_at = DEFAULT_WIDTH)
  if text.length < wrap_at.to_i || wrap_at.to_i.zero?
    return text
  end
  text.split(NEWLINE, -1).map do |paragraph|
    format_paragraph(paragraph, wrap_at)
  end * NEWLINE
end