-
-
Notifications
You must be signed in to change notification settings - Fork 909
/
word_wrap.rb
211 lines (164 loc) · 4.55 KB
/
word_wrap.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
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
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
module Shoulda
module Matchers
# @private
module WordWrap
TERMINAL_WIDTH = 72
def word_wrap(document, options = {})
Document.new(document, **options).wrap
end
end
extend WordWrap
# @private
class Document
def initialize(document, indent: 0)
@document = document
@indent = indent
end
def wrap
wrapped_paragraphs.map { |lines| lines.join("\n") }.join("\n\n")
end
protected
attr_reader :document, :indent
private
def paragraphs
document.split(/\n{2,}/)
end
def wrapped_paragraphs
paragraphs.map do |paragraph|
Paragraph.new(paragraph, indent: indent).wrap
end
end
end
# @private
class Text < ::String
LIST_ITEM_REGEXP = /\A((?:[a-z0-9]+(?:\)|\.)|\*) )/.freeze
def indented?
self =~ /\A +/
end
def list_item?
self =~ LIST_ITEM_REGEXP
end
def match_as_list_item
match(LIST_ITEM_REGEXP)
end
end
# @private
class Paragraph
def initialize(paragraph, indent: 0)
@paragraph = Text.new(paragraph)
@indent = indent
end
def wrap
if paragraph.indented?
lines
elsif paragraph.list_item?
wrap_list_item
else
wrap_generic_paragraph
end
end
protected
attr_reader :paragraph, :indent
private
def wrap_list_item
wrap_lines(combine_list_item_lines(lines))
end
def lines
paragraph.split("\n").map { |line| Text.new(line) }
end
def combine_list_item_lines(lines)
lines.inject([]) do |combined_lines, line|
if line.list_item?
combined_lines << line
else
combined_lines.last << (" #{line}").squeeze(' ')
end
combined_lines
end
end
def wrap_lines(lines)
lines.map { |line| Line.new(line, indent: indent).wrap }
end
def wrap_generic_paragraph
Line.new(combine_paragraph_into_one_line, indent: indent).wrap
end
def combine_paragraph_into_one_line
paragraph.gsub(/\n/, ' ')
end
end
# @private
class Line
OFFSETS = { left: -1, right: +1 }.freeze
def initialize(line, indent: 0)
@indent = indent
@original_line = @line_to_wrap = Text.new(line)
@indentation = ' ' * indent
@indentation_read = false
end
def wrap
if line_to_wrap.indented?
[line_to_wrap]
else
lines = []
loop do
@previous_line_to_wrap = line_to_wrap
new_line = (indentation || '') + line_to_wrap
result = wrap_line(new_line)
lines << normalize_whitespace(result[:fitted_line])
unless @indentation_read
@indentation = read_indentation
@indentation_read = true
end
@line_to_wrap = result[:leftover]
if line_to_wrap.to_s.empty? || previous_line_to_wrap == line_to_wrap
break
end
end
lines
end
end
protected
attr_reader :indent, :original_line, :line_to_wrap, :indentation,
:previous_line_to_wrap
private
def read_indentation
initial_indentation = ' ' * indent
match = line_to_wrap.match_as_list_item
if match
initial_indentation + (' ' * match[1].length)
else
initial_indentation
end
end
def wrap_line(line)
index = nil
if line.length > Shoulda::Matchers::WordWrap::TERMINAL_WIDTH
index = determine_where_to_break_line(line, direction: :left)
if index == -1
index = determine_where_to_break_line(line, direction: :right)
end
end
if index.nil? || index == -1
fitted_line = line
leftover = ''
else
fitted_line = line[0..index].rstrip
leftover = line[index + 1..]
end
{ fitted_line: fitted_line, leftover: leftover }
end
def determine_where_to_break_line(line, args)
direction = args.fetch(:direction)
index = Shoulda::Matchers::WordWrap::TERMINAL_WIDTH
offset = OFFSETS.fetch(direction)
while line[index] !~ /\s/ && (0...line.length).cover?(index)
index += offset
end
index
end
def normalize_whitespace(string)
indentation + string.strip.squeeze(' ')
end
end
end
end