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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
|
# File 'lib/cornucopia/util/pretty_formatter.rb', line 17
def pretty_print
search_pos = 0
do_pretty_print = @unknown_string =~ /Parameters:[ \t]*\{.*?\}/
formatted_class_pos = class_pos = -1
while !do_pretty_print && class_pos && class_pos == formatted_class_pos
search_pos += formatted_class_pos + 1
if @print_html_safe
class_pos = @unknown_string[search_pos..-1] =~ /\#\<[^ \t\n]+/
formatted_class_pos = (@unknown_string[search_pos..-1] =~ /\#\<[^ \t\n]+[ \t]*\n/)
else
class_pos = @unknown_string[search_pos..-1] =~ /\#\<[^ \t\n]+/
formatted_class_pos = (@unknown_string[search_pos..-1] =~ /\#\<[^ \t\n]+[ \t]*\n/)
end
do_pretty_print ||= (class_pos && class_pos != formatted_class_pos)
end
if do_pretty_print
@loop_pos = [-1, -1]
@loop_count = [0, 0]
@indent_level = 0
@current_state = :unknown
@state_stack = [:unknown]
@current_pos = 0
@start_pos = 0
@unknown_string_len = @unknown_string.length
@formatted_string = "".dup
while @current_pos < @unknown_string_len
break if in_infinite_loop?
case @current_state
when :unknown
search_pos = @current_pos
params_pos = @unknown_string[search_pos..-1] =~ /Parameters:[ \t]*\{.*?\}/
params_pos += search_pos if params_pos
formatted_class_pos = class_pos = -1
while (class_pos && class_pos == formatted_class_pos)
search_pos += formatted_class_pos + 1
if @print_html_safe
class_pos = @unknown_string[search_pos..-1] =~ /\#\<[^ \t\n]+/
formatted_class_pos = (@unknown_string[search_pos..-1] =~ /\#\<[^ \t\n]+[ \t]*\n/)
else
class_pos = @unknown_string[search_pos..-1] =~ /\#\<[^ \t\n]+/
formatted_class_pos = (@unknown_string[search_pos..-1] =~ /\#\<[^ \t\n]+[ \t]*\n/)
end
end
class_pos += search_pos if class_pos
formatted_class_pos += search_pos if formatted_class_pos
if class_pos &&
class_pos != formatted_class_pos &&
(!params_pos || class_pos < params_pos)
@current_pos = class_pos
if @start_pos < @current_pos || @start_pos > 0
output_line(@unknown_string[@start_pos..@current_pos - 1])
else
@indent_level -= 1
@state_stack << :bottom_level
end
start_class
else
if params_pos
@current_pos = params_pos
@state_stack << :parameters
if @start_pos < @current_pos || @start_pos > 0
output_line(@unknown_string[@start_pos..@current_pos - 1])
else
@state_stack << :bottom_level
@indent_level -= 1
end
@formatted_string.rstrip!
@state_stack << :parameters_name
@current_state = :value
@start_pos = @current_pos
@indent_level += 1
else
output_end
end
end
when :value_end
case @state_stack[-1]
when :unknown
@current_state = :unknown
when :parameters
if @current_pos + 1 < @unknown_string_len
@formatted_string << "\n"
end
@indent_level -= 1
@state_stack.pop
@current_state = :unknown
when :parameters_name
group_value_end :parameters_start, "}"
when :class_name
group_value_end :variable,
">",
end_append_text: @indent_level == 0 ? "\n" : "",
append_text: " ",
group_end_same_line: true,
add_value: true
@indent_level += 1
when :variable
if (position_matches_string(@current_pos - 1, ":") &&
((!@print_html_safe &&
0 == (@unknown_string[@current_pos..-1] =~ /^[ \t\r\n]*({.*?}|\#<.*?>|\[.*?\]|\".*?\"|[^\n,\"]+)[ \t\r\n]*[,\>]/)) ||
(@print_html_safe &&
0 == (@unknown_string[@current_pos..-1] =~ /^[ \t\r\n]*({.*?}|\#<\;.*?>\;|\[.*?\]|"\;.*?"\;|[^\n,\"]+)[ \t\r\n]*(,|>\;)/))
))
@state_stack.pop
@state_stack << :group_implied
@state_stack << :hash_key_symbol_post
elsif (!position_matches_string(@current_pos - 1, ":") &&
((!@print_html_safe &&
0 == (@unknown_string[@current_pos..-1] =~ /^[ \t\r\n]*=>[ \t\r\n]*({.*?}|\#<.*?>|\[.*?\]|\".*?\"|[^\n,\"]+)[ \t\r\n]*[,\>]/)) ||
(@print_html_safe &&
0 == (@unknown_string[@current_pos..-1] =~ /^[ \t\r\n]*=>\;[ \t\r\n]*({.*?}|\#<\;.*?>\;|\[.*?\]|"\;.*?"\;|[^\n,\"&]+)[ \t\r\n]*(,|>\;)/))
))
@state_stack.pop
@state_stack << :group_implied
@state_stack << :hash_key_string
else
group_value_end :variable_equal,
">",
append_text: " ",
group_end_same_line: !@variable_set,
end_append_text: @indent_level == 0 ? "\n" : ""
end
when :variable_value
group_value_end :variable_comma, ">",
same_line: true,
end_append_text: @indent_level == 0 ? "\n" : ""
when :hash_key_symbol_post
group_value_end :hash_value,
@state_stack[-2] == :group_implied ? ">" : "}",
append_text: " ",
add_value: true
when :hash_key, :hash_key_symbol, :hash_key_string
group_value_end :hash_rocket,
@state_stack[-2] == :group_implied ? ">" : "}",
append_text: " "
when :hash_value
group_value_end :hash_comma,
@state_stack[-1] == :group_implied ? ">" : "}",
same_line: true
when :array
group_value_end :array_comma, "]"
when :group_implied
@current_pos += 1
end
@current_pos -= 1
when :hash_rocket
if @unknown_string[@current_pos] == ","
group_separator ",",
:hash_value,
@state_stack[-2] == :group_implied ? ">" : "}",
append_text: " "
else
group_separator "=>",
:hash_value,
@state_stack[-2] == :group_implied ? ">" : "}",
append_text: " "
end
when :variable_equal
@variable_set = true
if @unknown_string[@current_pos] == ","
group_separator ",",
:variable_value, ">",
end_append_text: @indent_level == 0 ? "\n" : ""
else
group_separator "=",
:variable_value, ">",
end_append_text: @indent_level == 0 ? "\n" : "",
append_text: " "
end
when :variable_comma
group_separator ",",
:variable, ">",
end_append_text: @indent_level == 0 ? "\n" : ""
when :array_comma
group_separator ",", :array, "]"
when :hash_comma
group_separator ",",
:hash_key,
@state_stack[-1] == :group_implied ? ">" : "}"
if @state_stack[-1] == :group_implied
@state_stack.pop
end
when :parameters_start
start_grouping :hash_key
when :whitespace
case @unknown_string[@current_pos]
when " ", "\r", "\n", "\t"
@start_pos = @current_pos + 1
else
@start_pos = @current_pos @current_pos -= 1
@current_state = @state_stack.pop
end
when :string
if position_matches_string(@current_pos, "\\")
@current_pos += 1
elsif position_matches_string(@current_pos, "\"")
@current_pos += translate_value("\"").length
value_end
end
when :value
case @unknown_string[@current_pos]
when "{"
if @start_pos == @current_pos
start_grouping :hash_key
else
output_end
end
when "["
if @start_pos == @current_pos
start_grouping :array
else
output_end
end
when "\""
if position_matches_string(@current_pos, "\"")
start_quote
end
when "#"
if @start_pos == @current_pos &&
@current_pos < @unknown_string_len - 2 &&
position_matches_string(@current_pos + 1, "<")
start_class
end
when "="
case @state_stack[-1]
when :variable,
:hash_key,
:hash_key_string,
:hash_key_symbol,
:hash_key_symbol_post
value_end
end
when ":"
case @state_stack[-1]
when :hash_key
if @current_pos == @start_pos
@state_stack.pop
@state_stack << :hash_key_symbol
else
@state_stack.pop
@state_stack << :hash_key_symbol_post
@current_pos += 1
value_end
end
when :parameters_name
@current_pos += 1
value_end
when :variable
unless @current_pos == @start_pos
@current_pos += 1
value_end
end
end
when "&", ">"
if position_matches_string(@current_pos, ">")
case @state_stack[-1]
when :class_name,
:variable,
:variable_equal,
:variable_value,
:variable_comma
value_end
when :hash_key,
:hash_key_symbol_post,
:hash_key_symbol,
:hash_key_string,
:hash_rocket,
:hash_value,
:hash_comma
if @state_stack[-2] == :group_implied
value_end
end
end
elsif position_matches_string(@current_pos, "\"")
start_quote
end
when "}"
case @state_stack[-1]
when :hash_key,
:hash_key_symbol_post,
:hash_key_symbol,
:hash_key_string,
:hash_rocket,
:hash_value,
:hash_comma
value_end
end
when "]"
case @state_stack[-1]
when :array,
:array_comma
value_end
end
when ","
case @state_stack[-1]
when :hash_value,
:array,
:variable_value,
:variable,
:variable_equal,
:hash_key,
:hash_key_string,
:hash_key_symbol,
:hash_key_symbol_post,
:hash_rocket,
:hash_comma,
:variable_comma,
:array_comma
value_end
end
when " ", "\r", "\n", "\t"
case @state_stack[-1]
when :variable_value,
:array,
:hash_value
@current_pos += 1
@current_pos -= 1
else
value_end
end
end
end
@current_pos += 1
end
if @start_pos < @current_pos
output_end
end
@formatted_string.rstrip!
@formatted_string = @formatted_string.html_safe if @print_html_safe
@formatted_string
else
@unknown_string
end
end
|